Sorting with ORDER BY
Progress Tracking
Log in to save this lesson and continue from where you left off.
Controlling the Order of Results
Without ORDER BY, SQL returns rows in no guaranteed order. You might get them in the order they were inserted, or by primary key, or seemingly random. If you care about the order, you need to specify it.
Your First ORDER BY
SELECT *
FROM orders
ORDER BY total_order_cost;| id | first_name | last_name | department | salary | phone_number | joining_date |
|---|---|---|---|---|---|---|
| 1 | Sarah | Mitchell | HR | 95000 | 555-0101 | 2021-03-15 |
| 2 | Michael | Chen | HR | 88000 | 555-0102 | 2022-06-01 |
| 3 | Emily | Rodriguez | HR | 82500 | 2021-09-20 | |
| 4 | David | Park | HR | 80000 | 555-0104 | 2023-01-10 |
| 5 | Lisa | Thompson | HR | 65000 | 2021-04-05 |
Sort `techcorp_workforce` by salary from lowest to highest.
DESC is what you want for "top N" queries: top earners, most recent orders, highest scores.
Sorting Text
Text sorts alphabetically:
SELECT
first_name,
last_name,
department
FROM techcorp_workforce
ORDER BY last_name ASC;This returns employees in alphabetical order by last name.
Sorting Dates
Dates are sorted chronologically. Use DESC for the most recent first.
Sort employees to show the most recently hired first.
The most recently hired employees appear first.
Sorting by Multiple Columns
You can sort by multiple columns. SQL sorts by the first column, then breaks ties with the second column, and so on.
Sort departments alphabetically, and within each department show the highest earners first.
Each column can have its own ASC or DESC. You might want department A-Z, but salary highest first within each department.
Combining ORDER BY with WHERE
ORDER BY comes after WHERE. Write a query that filters orders, then sorts by cost from highest to lowest:
| id | cust_id | order_date | order_details | total_order_cost |
|---|---|---|---|---|
| 1 | 3 | 2019-03-04 | Coat | 100 |
| 2 | 3 | 2019-03-01 | Shoes | 80 |
| 3 | 3 | 2019-03-07 | Skirt | 30 |
| 4 | 7 | 2019-02-01 | Coat | 25 |
| 5 | 7 | 2019-03-10 | Shoes | 80 |
Find all orders and sort them by total cost from highest to lowest.
Sorting by Column Position
You can sort by column number instead of name:
SELECT
first_name,
last_name,
salary
FROM techcorp_workforce
ORDER BY 3 DESC; -- Sorts by the 3rd column (salary)This is shorter but less readable. Use it sparingly, mainly in quick ad-hoc queries.
The number refers to the column's position in your SELECT list, not the table schema. Column positions can break if someone adds or removes columns from the SELECT list. Named columns are safer for saved queries.
Sorting by Alias
You can sort by a column alias, which is useful when sorting by a calculated column:
Sort employees by the length of their first name (longest first). Include the length as a column.
NULLs in Sorting
Where do NULLs appear when sorting? It depends on the database:
- PostgreSQL —
NULLs last inASC, first inDESC - MySQL —
NULLs first inASC, last inDESC - SQL Server —
NULLs first inASC, last inDESC - Oracle —
NULLs last inASC, first inDESC
PostgreSQL lets you control this explicitly:
ORDER BY column_name ASC NULLS FIRST
ORDER BY column_name DESC NULLS LASTIf NULL ordering matters for your query, check your database's default behavior or use NULLS FIRST / NULLS LAST explicitly.
Key Takeaways
ORDER BYcontrols the sort order of your results.ASC(ascending) is the default. UseDESCfor descending.- Sort by multiple columns to break ties — each column gets its own direction.
ORDER BYcomes afterWHEREin the query.NULLbehavior in sorting varies by database — useNULLS FIRST/NULLS LASTwhen it matters.
What's Next
Now that you can sort your results, you'll often want to return only the top N rows. That's where LIMIT comes in, which you'll learn in the next lesson.