ORDER BY: Sort Results
Sort the result highest-first (DESC) or lowest-first (ASC), and by more than one column.
The Orange Cap table lists batters from most runs to fewest. Without sorting, a database returns rows in whatever order is fastest for it — which is not guaranteed to be meaningful. ORDER BY puts the result in the order you want.
Definition
ORDER BY sorts the final result set by one or more columns or expressions. ASC sorts lowest → highest (A → Z, oldest → newest) and is the default; DESC sorts highest → lowest. It is the only way to guarantee the order of rows in a result.
Syntax
sql
SELECT column_list
FROM table_name
ORDER BY column1 [ASC | DESC], column2 [ASC | DESC];| Part | Meaning |
|---|---|
ORDER BY column1 | Main sort column |
ASC | Ascending: 1 → 9, A → Z (default if you write nothing) |
DESC | Descending: 9 → 1, Z → A |
, column2 | Tie-breaker: used only when rows have the same column1 value |
Example — highest first (DESC)
sql
SELECT *
FROM players
ORDER BY runs DESC;| id | name | team | country | runs |
|---|---|---|---|---|
| 3 | Jos Buttler | RR | England | 850 |
| 1 | Virat Kohli | RCB | India | 750 |
| 5 | David Warner | DC | Australia | 700 |
| 2 | Rohit Sharma | MI | India | 500 |
| 4 | MS Dhoni | CSK | India | 350 |
| 6 | Jasprit Bumrah | MI | India | 20 |
Execution order: 1️⃣ FROM → 2️⃣ SELECT * → 3️⃣ ORDER BY runs DESC. Like the Orange Cap race!
Example — lowest first (ASC)
sql
SELECT *
FROM players
ORDER BY runs ASC;| id | name | team | country | runs |
|---|---|---|---|---|
| 6 | Jasprit Bumrah | MI | India | 20 |
| 4 | MS Dhoni | CSK | India | 350 |
| 2 | Rohit Sharma | MI | India | 500 |
| 5 | David Warner | DC | Australia | 700 |
| 1 | Virat Kohli | RCB | India | 750 |
| 3 | Jos Buttler | RR | England | 850 |
ORDER BY runs without ASC gives exactly the same result, because ASC is the default.
Example — two columns
sql
SELECT *
FROM players
ORDER BY team ASC, runs DESC;| id | name | team | country | runs |
|---|---|---|---|---|
| 4 | MS Dhoni | CSK | India | 350 |
| 5 | David Warner | DC | Australia | 700 |
| 2 | Rohit Sharma | MI | India | 500 |
| 6 | Jasprit Bumrah | MI | India | 20 |
| 1 | Virat Kohli | RCB | India | 750 |
| 3 | Jos Buttler | RR | England | 850 |
How it works: rows are first sorted by team alphabetically (CSK, DC, MI, RCB, RR). Only where the team is the same — the two MI players — does the second column decide: Rohit (500) comes before Bumrah (20) because runs is DESC. Each column has its own direction.
Sorting with NULLs
In SQL Server, NULL is treated as the lowest value: NULLs appear first with ASC and last with DESC. In the innings table, Dhoni's 31 March innings has runs = NULL:
sql
SELECT TOP 4 player, runs
FROM innings
ORDER BY runs ASC;| player | runs |
|---|---|
| MS Dhoni | NULL |
| Rohit Sharma | 0 |
| MS Dhoni | 26 |
| Rohit Sharma | 45 |
Sorting by an alias
Because ORDER BY runs after SELECT, it can use a column alias:
sql
SELECT name, runs * 2 AS double_runs
FROM players
ORDER BY double_runs DESC;Common mistakes
- ❌ Assuming rows come back in insert order or id order. ✅ No ORDER BY = no guaranteed order.
- ❌
ORDER BY team, runs DESCand expecting both descending. ✅DESCapplies only to the column right before it — writeteam DESC, runs DESC. - ❌
ORDER BY 3 DESC(column position) — it works, but breaks silently when the SELECT list changes. ✅ Use column names.
SQL Server vs MySQL / PostgreSQL
| SQL Server | MySQL | PostgreSQL | |
|---|---|---|---|
| NULLs with ASC | first | first | last |
| Control NULL position | not supported directly | not supported | NULLS FIRST / NULLS LAST |
Interview tip
Q: Without ORDER BY, will SQL Server always return rows in primary-key order?
A: No. It often looks like that on small tables, but the order depends on indexes, parallelism and the execution plan. The only guarantee of order is an ORDER BY on the outermost query.
Key points to remember
- ORDER BY sorts the final result; it is the only way to guarantee row order.
- ASC = lowest to highest (the default); DESC = highest to lowest.
- With several columns, the second column only breaks ties in the first; each column has its own ASC/DESC.
- ORDER BY runs after SELECT, so it can use column aliases.
- In SQL Server NULL sorts as the lowest value: first with ASC, last with DESC.
- No ORDER BY means no guaranteed order — never rely on 'natural' table order.
Try it yourself
Orange Cap: most runs first
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
By team, then runs inside each team
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
NULLs come first with ASC
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Practice: sort the names
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Revision Flashcards
Tap a card to flip it and see the answer.
