Quick answer
ORDER BY sorts the rows returned by a query.
If you do not use ORDER BY, SQL does not guarantee the order of the results.
ASCmeans ascending order, like A to Z or lowest to highestDESCmeans descending order, like Z to A or highest to lowest
ASC or DESC, SQL usually defaults to ascending order.
In this guide
Sample table
Imagine this employees table:
| employee_id | name | department | salary | city |
|---|---|---|---|---|
| 1 | Ana | Sales | 62000 | Chicago |
| 2 | Ben | IT | 78000 | Dallas |
| 3 | Chris | HR | 56000 | Chicago |
| 4 | Dana | IT | 85000 | Denver |
| 5 | Evan | Sales | 59000 | Austin |
Why ORDER BY matters
Without ORDER BY, the database can return rows in whatever order is convenient internally. That order may look stable at first, but you should not rely on it.
In real work, sorting matters because you often want:
- highest salary first
- alphabetical customer lists
- newest records first
- top-performing categories at the top of a report
ORDER BY. Never assume SQL will “just know” the order you want.
Ascending order
Ascending order goes from smallest to largest for numbers, and usually A to Z for text.
SELECT name, salary FROM employees ORDER BY salary ASC;
This sorts salaries from lowest to highest.
| name | salary |
|---|---|
| Chris | 56000 |
| Evan | 59000 |
| Ana | 62000 |
| Ben | 78000 |
| Dana | 85000 |
ASC is optional in many databases because ascending is the default, but it can make your intent clearer.
Descending order
Descending order goes from largest to smallest for numbers, and usually Z to A for text.
SELECT name, salary FROM employees ORDER BY salary DESC;
This sorts salaries from highest to lowest.
| name | salary |
|---|---|
| Dana | 85000 |
| Ben | 78000 |
| Ana | 62000 |
| Evan | 59000 |
| Chris | 56000 |
DESC is very common in dashboards, leaderboards, and “top N” style queries.
Sort by multiple columns
You can sort by more than one column. SQL applies the sort from left to right.
SELECT name, department, salary FROM employees ORDER BY department ASC, salary DESC;
This means:
- first sort by
departmentalphabetically - then sort by
salaryhighest to lowest within each department
| name | department | salary |
|---|---|---|
| Chris | HR | 56000 |
| Dana | IT | 85000 |
| Ben | IT | 78000 |
| Ana | Sales | 62000 |
| Evan | Sales | 59000 |
Text vs number sorting
Sorting numbers and text follows different logic:
Sorted by value, like 1, 2, 3 or 56000, 59000, 62000.
Sorted alphabetically, like A to Z or Z to A.
For example:
SELECT name, department FROM employees ORDER BY name ASC;
That would sort names alphabetically: Ana, Ben, Chris, Dana, Evan.
Use ORDER BY with GROUP BY
ORDER BY is often used after GROUP BY so you can sort summaries.
SELECT department, COUNT(*) AS employee_count FROM employees GROUP BY department ORDER BY employee_count DESC;
This sorts departments by employee count from highest to lowest.
How NULL values behave
NULL values can behave differently depending on the database engine, but they are usually treated as missing values rather than normal text or numbers.
In many systems:
- NULL values may appear first in descending order
- NULL values may appear last in ascending order
NULL does not mean zero. It means “no value.”
Common mistakes
- forgetting to use
DESCwhen you want highest values first - assuming results will stay in a specific order without
ORDER BY - sorting by the wrong column
- not understanding that multi-column sorts are applied from left to right
- thinking the database will automatically know the “best” order
ORDER BY.
Practice questions
Bottom line
ORDER BY controls how query results are sorted. That makes it one of the most important finishing steps in a SQL query.
Once you understand ascending, descending, and multi-column sorting, your query results become much easier to read and much more useful.
Related SQL tutorials
Start here for all SQL topics and tutorials.
Learn how to filter rows before sorting them.
Learn how to group rows before sorting grouped summaries.
Understand COUNT, SUM, AVG, MIN, and MAX before sorting summarized results.