TExcelTAD
Home › MySQL › MySQL ORDER BY

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:

sql
SELECT columns
FROM table_name
ORDER BY column1 [ASC | DESC], column2 [ASC | DESC], ...;
mysql · try it yourself
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:

mysql · try it yourself
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:

mysql · try it yourself
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.
mysql · try it yourself
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):

mysql · try it yourself
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. The col IS NULL trick is the usual MySQL replacement.

Test yourself

What is the default sort direction of ORDER BY?

  1. DESC
  2. ASC
  3. Random
  4. Insertion order

In an ascending MySQL sort, where do NULL values appear?

  1. First
  2. Last
  3. They are removed
  4. In random positions

Without ORDER BY, the order of rows in a result is …

  1. always the primary key order
  2. always insertion order
  3. not guaranteed
  4. alphabetical