Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Ordering
3. Sort the rows – ORDER BY
Limiting the output
Eliminating duplicate results
Aggregation
Grouping
HAVING: filtering and ordering groups
Let's practice

Instruction

Good, let's get down to work!

You're already pretty skilled when it comes to filtering rows – but have you wondered how they are sorted in the result of a T-SQL query? Well, the answer is simple – by default, rows are not sorted at all. The sequence in which rows appear is arbitrary, and every database can behave differently. You can even perform the same T-SQL instruction a few times and get a different order each time – unless you ask the database to sort the rows, of course.

SELECT
  *
FROM Order
ORDER BY CustomerId;

In the above example, we've added a new piece: ORDER BY. After this expression, you can simply specify a column on which the data will be sorted.

In this case, we give the column name CustomerId, so all orders will be sorted by CustomerIds.

Exercise

Try it yourself. Select all columns from the Employee table and sort them according to salary.

Stuck? Here's a hint!

Type:

SELECT
 *
FROM Employee
ORDER BY Salary;