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.

  • ASC means ascending order, like A to Z or lowest to highest
  • DESC means descending order, like Z to A or highest to lowest
Tip: If you leave off 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
We’ll use this table for the examples so you can see exactly how sorting changes the output.

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
Beginner rule: if the display order matters, use 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
Writing 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 department alphabetically
  • then sort by salary highest to lowest within each department
name department salary
Chris HR 56000
Dana IT 85000
Ben IT 78000
Ana Sales 62000
Evan Sales 59000
Think of this like sorting in layers: first one rule, then another rule inside each group.

Text vs number sorting

Sorting numbers and text follows different logic:

Numbers
Sorted by value, like 1, 2, 3 or 56000, 59000, 62000.
Text
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.

Sorting text is based on string rules, not on what feels logically “most important.”

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.

GROUP BY summarizes the data. ORDER BY controls how those summaries are displayed.

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 DESC when 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
SQL does not guarantee result order unless you explicitly use ORDER BY.

Practice questions

1. Sort employees by salary ascending.
2. Sort employees by salary descending.
3. Sort employees by department, then by salary highest to lowest.
4. Sort employees alphabetically by name.
5. Group employees by department and sort the grouped results by count.

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.

Quick memory trick: ASC = smallest to largest, DESC = largest to smallest.

Related SQL tutorials

SQL Hub
Start here for all SQL topics and tutorials.
WHERE Clause
Learn how to filter rows before sorting them.
GROUP BY
Learn how to group rows before sorting grouped summaries.
Aggregate Functions
Understand COUNT, SUM, AVG, MIN, and MAX before sorting summarized results.