MySQL ORDER BY
Without ORDER BY, a database returns rows in whatever order is convenient for it. That order often looks stable (for example primary-key order), but nothing guarantees it. If the order matters, ask for it:
SELECT columns
FROM table_name
ORDER BY column1 [ASC | DESC], column2 [ASC | DESC], ...;SELECT name, unit_price
FROM products
WHERE category_id = 5
ORDER BY unit_price;+----------------------+------------+
| name | unit_price |
+----------------------+------------+
| Sea Salt Crisps | 1.99 |
| Dark Chocolate Bar | 2.49 |
| Roasted Almonds 200g | 5.49 |
+----------------------+------------+
ASC (ascending, smallest first) is the default. DESC sorts descending:
SELECT name, unit_price
FROM products
ORDER BY unit_price DESC
LIMIT 5;+--------------------+------------+
| name | unit_price |
+--------------------+------------+
| Espresso Beans 1kg | 24.50 |
| Chocolate Cake | 18.00 |
| Laundry Pods (30) | 11.90 |
| Aged Gruyere 250g | 7.95 |
| Earl Grey Tea | 6.75 |
+--------------------+------------+
(LIMIT 5 keeps only the first five rows of the sorted result; it has its own lesson.)
Sorting by several columns
When two rows tie on the first column, the second column decides, and so on. Each column has its own direction. Here the two London customers tie on country and city, so last_name breaks the tie:
SELECT country, city, last_name
FROM customers
WHERE country IN ('UK', 'Germany', 'France')
ORDER BY country, city DESC, last_name;+---------+------------+-----------+
| country | city | last_name |
+---------+------------+-----------+
| France | Paris | Martin |
| France | Lyon | Moreau |
| Germany | Munich | Fischer |
| Germany | Berlin | Weber |
| UK | Manchester | Khan |
| UK | London | Brown |
| UK | London | Carter |
+---------+------------+-----------+
What you can sort by
- Any column of the table, even one that isn't selected.
- An expression, such as
unit_price * units_in_stock. - A column alias from the select list.
- A column position (
ORDER BY 2). This works but is fragile: change the select list and the sort silently changes.
SELECT name, unit_price * units_in_stock AS stock_value
FROM products
ORDER BY stock_value DESC
LIMIT 3;+----------------------+-------------+
| name | stock_value |
+----------------------+-------------+
| Espresso Beans 1kg | 980.00 |
| Earl Grey Tea | 573.75 |
| Roasted Almonds 200g | 356.85 |
+----------------------+-------------+
Text order and NULLs
Text sorts alphabetically according to the column's collation, case-insensitively by default. NULL counts as smaller than any value, so it comes first in ascending order and last in descending order. To push NULLs to the end of an ascending sort, sort on col IS NULL first (it is 0 for real values and 1 for NULL):
SELECT first_name, city
FROM customers
WHERE customer_id IN (6, 8, 9, 10)
ORDER BY city IS NULL, city;+------------+------------+
| first_name | city |
+------------+------------+
| Farah | Manchester |
| Hana | Osaka |
| Julia | Prague |
| Ivan | NULL |
+------------+------------+
Note: Standard SQL has
NULLS FIRST/NULLS LAST, and PostgreSQL supports it, but MySQL does not. Thecol IS NULLtrick is the usual MySQL replacement.
Test yourself
What is the default sort direction of ORDER BY?
- DESC
- ASC
- Random
- Insertion order
In an ascending MySQL sort, where do NULL values appear?
- First
- Last
- They are removed
- In random positions
Without ORDER BY, the order of rows in a result is …
- always the primary key order
- always insertion order
- not guaranteed
- alphabetical