Learn SQL
-- filtering.order_by

ORDER BY — Sorting Results in SQL

Control the row order of your result

--concept

ORDER BY sorts the final result by one or more columns, ascending (ASC, the default) or descending (DESC). Without it, SQL makes no promises about row order — ever. Sort by salary DESC for “top earners first”, or chain columns like ORDER BY department ASC, salary DESC to sort within groups.

--how it works
1

Sort keys come first

ORDER BY salary names the column (or expression) that decides the order.

2

Pick the direction

ASC = low → high (the default), DESC = high → low. One keyword, whole different story.

3

Chain keys for tie-breaks

ORDER BY department ASC, salary DESC — departments A→Z, salaries high→low inside each.

4

Sort runs last

After filtering and grouping, on the final result — that is why aliases from SELECT are usable in ORDER BY.

5

No ORDER BY, no promise

Without it, row order is whatever the engine feels like — "usually insertion order" is a bug waiting to happen.

--visualization
Cynthia91,000Alice85,000Farid73,000Elena58,000Bob52,000David47,000ORDER BY salary DESC
Stage 1 / 8
ORDER BY salary DESC — SQL guarantees result order only when you ask for it.
--example
query.sql
1SELECT name, department, salary
2FROM employees
3ORDER BY salary DESC;
Result
namedepartmentsalary
CynthiaEngineering91000
AliceEngineering85000
FaridEngineering73000
ElenaSales58000
BobSales52000
DavidMarketing47000

Highest salary first. Flip to ASC in the visualization to watch the bars re-sort in the opposite direction — same query, one keyword changed.

--variants

Descending — top first

Highest values lead; the leaderboard pattern.

query.sql
1SELECT name, salary
2FROM employees
3ORDER BY salary DESC;
Result
namesalary
Cynthia91000
Alice85000
Farid73000
Elena58000
Bob52000
David47000
1Six rows arrive, then leave sorted by salary, biggest first.
2DESC is the explicit spelling; ASC is what happens without a keyword.
3Combine with LIMIT for "top N".

Ascending — default

Lowest first; also what you get with no keyword.

query.sql
1SELECT name, salary
2FROM employees
3ORDER BY salary ASC;
Result
namesalary
David47000
Bob52000
Elena58000
Farid73000
Alice85000
Cynthia91000
1The mirror image of the DESC case.
2ASC is optional — ORDER BY salary means the same.
3Text sorts alphabetically; dates chronologically.

Multi-column sort

Later keys break ties from earlier ones.

query.sql
1SELECT name, department, salary
2FROM employees
3ORDER BY department ASC, salary DESC;
Result
namedepartmentsalary
CynthiaEngineering91000
AliceEngineering85000
FaridEngineering73000
DavidMarketing47000
ElenaSales58000
BobSales52000
1Departments sort A→Z; inside each, salaries sort high→low.
2No ties occurred, but if two Engineering salaries matched, insertion order would decide.
3Each key gets its own direction — mix ASC and DESC freely.

Sort by expression

ORDER BY takes any expression, not just bare columns.

query.sql
1SELECT name, salary
2FROM employees
3ORDER BY salary * -1;
Result
namesalary
Cynthia91000
Alice85000
Farid73000
Elena58000
Bob52000
David47000
1Negating the salary turns ascending order into descending.
2A party trick here, but expressions shine for computed sort keys (length(name), EXTRACT(month FROM d)).
3ORDER BY is the only clause allowed to use SELECT aliases before they exist.
--key facts
No default orderwithout ORDER BY, row order is whatever the engine feels like
Multi-columnties in the first key fall through to the second, then third…
Costsorting is real work — on huge tables pair it with a good WHERE
--use cases
Leaderboards and "top N" anything — always ORDER BY … DESC LIMIT n
Report readability: chronological, alphabetical or grouped output
Paging: stable multi-column sorts make OFFSET pages deterministic
Latest-first feeds: ORDER BY created_at DESC
--under the hood
Sorting is real work: O(n log n) unless an index already delivers the order — a common reason a simple query is slow.
ORDER BY on an indexed column can skip the sort entirely; the engine walks the index in order.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.