HTML CSS Bootstrap JavaScript jQuery MySQL PHP Data Mining

MySQL ORDER BY

By default, MySQL returns rows in no guaranteed order — the sequence depends on how the data is stored internally. The ORDER BY clause lets you control that order and sort results by one or more columns, either ascending or descending.


Syntax

SELECT column1, column2
FROM table_name
ORDER BY column1 [ASC | DESC], column2 [ASC | DESC];
  • ASC — ascending order (A → Z, 0 → 9, oldest → newest). This is the default.
  • DESC — descending order (Z → A, 9 → 0, newest → oldest).

Sample Table

idnamecityage
1Rahim UddinDhaka22
2Sara BegumChittagong20
3Karim AliSylhet23
4Nila AkterDhaka21
5Rafi HossainRajshahi24

Sort by a Single Column — Ascending

-- Sort by age, youngest first (ASC is the default)
SELECT name, age FROM students
ORDER BY age ASC;
nameage
Sara Begum20
Nila Akter21
Rahim Uddin22
Karim Ali23
Rafi Hossain24

Sort by a Single Column — Descending

-- Sort by age, oldest first
SELECT name, age FROM students
ORDER BY age DESC;
nameage
Rafi Hossain24
Karim Ali23
Rahim Uddin22
Nila Akter21
Sara Begum20

Sort by Text Column

Text columns are sorted alphabetically:

-- Sort by name A to Z
SELECT name, city FROM students
ORDER BY name ASC;

Sort by Multiple Columns

When you sort by multiple columns, MySQL sorts by the first column first. Rows that share the same value in the first column are then sorted by the second column.

-- Sort by city A to Z, then by age youngest first within each city
SELECT name, city, age FROM students
ORDER BY city ASC, age ASC;
namecityage
Sara BegumChittagong20
Nila AkterDhaka21
Rahim UddinDhaka22
Rafi HossainRajshahi24
Karim AliSylhet23

Each sort column can have its own direction:

-- Sort by city A to Z, then by age oldest first within each city
SELECT name, city, age FROM students
ORDER BY city ASC, age DESC;

ORDER BY with WHERE

ORDER BY always comes after WHERE:

SELECT name, age FROM students
WHERE age > 20
ORDER BY age DESC;

NULL Values in Sorting

When a column contains NULL values, MySQL treats NULL as the lowest possible value. This means:

  • With ASC, rows with NULL appear first.
  • With DESC, rows with NULL appear last.
-- NULL ages appear first in ascending order
SELECT name, age FROM students
ORDER BY age ASC;
Tip: To push NULL rows to the end in ascending order, you can use: ORDER BY age IS NULL ASC, age ASC. This sorts non-null rows first, then null rows at the end.

Key Points to Remember

  • ORDER BY sorts the result set — it does not change the data in the table.
  • ASC (ascending) is the default — you can omit it if you want ascending order.
  • DESC must be written explicitly when you want descending order.
  • You can sort by multiple columns — each with its own direction.
  • ORDER BY comes after WHERE and before LIMIT in the query.
  • NULL values sort as the lowest value — first in ASC, last in DESC.
What's next? The next lesson covers LIMIT — how to restrict the number of rows returned by a query, which is essential for pagination.