Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Ordering
5. Ascending and descending orders
Limiting the output
Eliminating duplicate results
Aggregation
Grouping
HAVING: filtering and ordering groups
Let's practice

Instruction

Good! As you can see, the lowest salary appeared first, and the highest salary last. This ascending order of results is performed in T-SQL by default. If you want to be precise and make things clear, however, you can use the keyword ASC (short for ascending) after the column name:

SELECT
  *
FROM Order
ORDER BY TotalSum ASC;

Adding the keyword ASC will change nothing, but it will show your intentions more clearly to anyone reviewing your code.

We can also reverse the order and make the greatest values appear first:

SELECT
  *
FROM Order
ORDER BY TotalSum DESC;

As you can see, we've added the DESC keyword after the column name, which is short for descending order. As a result, the greatest values in the column TotalSum will appear first.

When using ORDER BY, it's good practice to specify whether the order is ASC or DESC (even if ASC is used by SQL Server by default), since it makes the query more readable.

Exercise

Select all rows from the Employee table and sort them in descending order by last name.

Stuck? Here's a hint!

Type:

SELECT
  *
FROM Employee
ORDER BY LastName DESC;