MySQL ORDER BY
Rows in a table have no guaranteed order. ORDER BY sorts the result of a query by one or more columns or expressions, in ascending or descending order. Use it whenever order matters: leaderboards, newest-first lists, alphabetical directories, reports.
1. Syntax
SELECT ... FROM ... [WHERE ...]
ORDER BY expr1 [ASC | DESC], expr2 [ASC | DESC], ...;
ASC(ascending, the default): smallest to largest, A to Z, oldest to newest.DESC: largest to smallest, Z to A, newest first.- ORDER BY is processed after SELECT, so it can use column aliases.
2. Ascending and Descending
SELECT name, salary FROM staff ORDER BY salary;
Result: 7 rows
| name | salary |
|---|---|
| arun | 58000 |
| Prem | 61000 |
| Lokesh | 67000 |
| Om | 72000 |
| Nihal | 84000 |
| Kavya | 91000 |
| Mira | 98000 |
SELECT name, salary FROM staff ORDER BY salary DESC;
Result: 7 rows
| name | salary |
|---|---|
| Mira | 98000 |
| Kavya | 91000 |
| Nihal | 84000 |
| Om | 72000 |
| Lokesh | 67000 |
| Prem | 61000 |
| arun | 58000 |
3. Sorting by Several Columns
SELECT dept, name, salary FROM staff ORDER BY dept ASC, salary DESC;
Result: 7 rows
| dept | name | salary |
|---|---|---|
| Finance | Kavya | 91000 |
| Finance | Lokesh | 67000 |
| IT | Mira | 98000 |
| IT | Nihal | 84000 |
| IT | Om | 72000 |
| Sales | Prem | 61000 |
| Sales | arun | 58000 |
4. Sorting by Expressions and Aliases
SELECT name, salary, ROUND(salary * 0.12) AS monthly_tax
FROM staff
ORDER BY monthly_tax DESC
LIMIT 3;
Result: 3 rows
| name | salary | monthly_tax |
|---|---|---|
| Mira | 98000 | 11760 |
| Kavya | 91000 | 10920 |
| Nihal | 84000 | 10080 |
SELECT name FROM staff ORDER BY CHAR_LENGTH(name) DESC, name; -- longest names first
Result: 7 rows
| name |
|---|
| Lokesh |
| Kavya |
| Nihal |
| arun |
| Mira |
| Prem |
| Om |
ORDER BY 2 DESC), but it is fragile: reordering the SELECT list silently changes the sort. Prefer names or aliases.5. Where NULLs Go
MySQL treats NULL as smaller than any value: first in ASC order, last in DESC order.
SELECT name, joined FROM staff ORDER BY joined;
Result: 7 rows
| name | joined |
|---|---|
| Prem | NULL |
| Kavya | 2019-08-30 |
| Mira | 2021-04-12 |
| Om | 2022-11-21 |
| Lokesh | 2023-01-09 |
| Nihal | 2024-06-03 |
| arun | 2025-02-14 |
MySQL has no NULLS LAST keyword. Sort on IS NULL first to push NULLs to the end:
SELECT name, joined FROM staff ORDER BY joined IS NULL, joined;
Result: 7 rows
| name | joined |
|---|---|
| Kavya | 2019-08-30 |
| Mira | 2021-04-12 |
| Om | 2022-11-21 |
| Lokesh | 2023-01-09 |
| Nihal | 2024-06-03 |
| arun | 2025-02-14 |
| Prem | NULL |
6. Custom Sort Orders with FIELD and CASE
Ratings are text, so alphabetical order (high, low, medium) is meaningless. FIELD() returns a value's position in a list:
SELECT name, rating FROM staff ORDER BY FIELD(rating, 'high', 'medium', 'low'), name;
Result: 7 rows
| name | rating |
|---|---|
| Nihal | NULL |
| Kavya | high |
| Mira | high |
| arun | medium |
| Lokesh | medium |
| Om | medium |
| Prem | low |
FIELD returns 0 for values not in the list (including NULL), which sorts them first. CASE gives full control:
SELECT name, rating
FROM staff
ORDER BY CASE rating WHEN 'high' THEN 1 WHEN 'medium' THEN 2 WHEN 'low' THEN 3 ELSE 4 END, name;
Result: 7 rows
| name | rating |
|---|---|
| Kavya | high |
| Mira | high |
| arun | medium |
| Lokesh | medium |
| Om | medium |
| Prem | low |
| Nihal | NULL |
7. Sorting Text: Case and Collation
With the default case-insensitive collation, arun sorts among the A's, not after all capital letters:
SELECT name FROM staff ORDER BY name;
Result: 7 rows
| name |
|---|
| arun |
| Kavya |
| Lokesh |
| Mira |
| Nihal |
| Om |
| Prem |
A binary collation compares raw character codes, where uppercase letters come before lowercase:
SELECT name FROM staff ORDER BY name COLLATE utf8mb4_bin;
Result: 7 rows
| name |
|---|
| Kavya |
| Lokesh |
| Mira |
| Nihal |
| Om |
| Prem |
| arun |
8. Random Order
SELECT name FROM staff ORDER BY RAND() LIMIT 2; -- different every run
Result: 2 rows (random: differs on every run)
| name |
|---|
| Mira |
| Nihal |
ORDER BY RAND() generates a random number for every row and sorts them all, which is slow on big tables. Pick random IDs in the application instead.9. ORDER BY and Indexes
If an index matches the sort, MySQL reads rows in order and can stop early with LIMIT. Otherwise it must sort them (Using filesort in EXPLAIN):
EXPLAIN SELECT name, salary FROM staff ORDER BY salary DESC LIMIT 3;
CREATE INDEX idx_salary ON staff (salary);
EXPLAIN SELECT name, salary FROM staff FORCE INDEX (idx_salary) ORDER BY salary DESC LIMIT 3;
Without an index: filesort (selected EXPLAIN columns)
| type | key | Extra |
|---|---|---|
| ALL | NULL | Using filesort |
With an index: read in order (selected EXPLAIN columns)
| type | key | Extra |
|---|---|---|
| index | idx_salary | Backward index scan |
FORCE INDEX just shows the difference. On big tables the optimizer chooses the index on its own.10. Practice Exercises
Exercise 1: List staff by joining date, newest first, with unknown dates at the end.
SELECT name, joined FROM staff ORDER BY joined IS NULL, joined DESC;
Result: 7 rows
| name | joined |
|---|---|
| arun | 2025-02-14 |
| Nihal | 2024-06-03 |
| Lokesh | 2023-01-09 |
| Om | 2022-11-21 |
| Mira | 2021-04-12 |
| Kavya | 2019-08-30 |
| Prem | NULL |
Exercise 2: Show the highest-paid person in each department first, then the rest of that department by name.
SELECT dept, name, salary,
salary = (SELECT MAX(salary) FROM staff s2 WHERE s2.dept = s.dept) AS is_top
FROM staff s
ORDER BY dept, is_top DESC, name;
Result: 7 rows
| dept | name | salary | is_top |
|---|---|---|---|
| Finance | Kavya | 91000 | 1 |
| Finance | Lokesh | 67000 | 0 |
| IT | Mira | 98000 | 1 |
| IT | Nihal | 84000 | 0 |
| IT | Om | 72000 | 0 |
| Sales | Prem | 61000 | 1 |
| Sales | arun | 58000 | 0 |
11. Summary
ORDER BY col [ASC|DESC]sorts results; ASC is the default.- Several columns sort by the first, then break ties with the next.
- Sort by expressions or aliases; avoid column numbers.
- NULLs sort first in ASC; use
ORDER BY col IS NULL, colto put them last. - FIELD() and CASE give custom orders; collations control text order; indexes avoid filesorts.
Reference: ORDER BY Optimization – MySQL Reference Manual
https://dev.mysql.com/doc/refman/9.7/en/order-by-optimization.html