Sorting Data — SQL

ORDER BY column sorts the rows of a query's result by that column. The direction is ASC, from small to large (A to Z), or DESC, from large to small (Z to A)…

Run SELECT * FROM students; and the rows come out in id order: 1, 2, 3, 4, 5. It looks as if the database sorted them for you. Not necessarily. The rows are in that order only by chance, and it can change tomorrow.

When the order matters, you have to ask for it yourself, with ORDER BY. ORDER BY arranges the rows of a SELECT result by one or more columns, from small to large or from large to small. It works like a class ranking board. The student with the highest grade is at the top. When two students have the same grade (a tie), the one whose name comes first in the alphabet goes first. So there is a first ordering by grade, then a second ordering by name for the ties.

Without ORDER BY, the order is not guaranteed

The query below shows the students table that the Lab and the exercise use:

SELECT * FROM students;
id name grade city
1 Dewi 90 London
2 Rizky 85 Manchester
3 Maya 92 Leeds
4 Andi 90 London
5 Budi 88 Manchester

SQLite gives the rows in the order it stored them, so the result looks sorted by id. But that happens by chance, it is not a guarantee. A database may give rows in any order, and the order can change after the data changes or the table grows. If the order matters, write ORDER BY.

ASC and DESC

The query below sorts the students by the grade column, largest first:

SELECT * FROM students
ORDER BY grade DESC;
id name grade city
3 Maya 92 Leeds
1 Dewi 90 London
4 Andi 90 London
5 Budi 88 Manchester
2 Rizky 85 Manchester

Two keys: when the values tie

Guess first: in the result of ORDER BY grade DESC above, Dewi comes before Andi, although both have a grade of 90. Why is Dewi first?

Only because Dewi was stored first. The query does not say how to order rows that tie. So another database, or the same SQLite after the table changes, may put Andi first.

A column you sort by is called a key (this is not the primary key). To decide the order of the rows that tie as well, add a second key, separated by a comma:

SELECT * FROM students
ORDER BY grade DESC, name ASC;
id name grade city
3 Maya 92 Leeds
4 Andi 90 London
1 Dewi 90 London
5 Budi 88 Manchester
2 Rizky 85 Manchester

💡 SQLite, MySQL and PostgreSQL differ here

⚠️ A doubled ORDER BY, and a DESC that hits the wrong column

TL;DR: ORDER BY column sorts the rows of a query's result by that column. The direction is ASC, from small to large (A to Z), or DESC, from large to small (Z to A). ASC is the default when you write no direction. Without ORDER BY, the order of the rows is not guaranteed. When rows can tie (have the same value), add a second column as a second key: a column that orders the tied rows. Write it in the same ORDER BY, separated by a comma, with its own direction: ORDER BY grade DESC, name ASC. A query has only one ORDER BY.

⚡ The order isn't guaranteed

Meaning: The result of SELECT * FROM students; usually comes out in id order. But without ORDER BY, that order happens only by chance, it is not a guarantee. When the order matters, you have to ask for it yourself, with ORDER BY.

⚡ Large to small

Write:

SELECT * FROM students
ORDER BY grade DESC;

Meaning: ORDER BY grade DESC sorts the rows by the grade column, from large to small. So Maya (92) is at the top and Rizky (85) is at the bottom. The opposite of DESC is ASC, from small to large. ASC is the default direction, so you can leave it out.

⚡ A second key for ties

Write:

SELECT * FROM students
ORDER BY grade DESC, name ASC;
Guess first: with only ORDER BY grade DESC, without name ASC, Dewi comes before Andi, although both have a grade of 90. Why?

Only because Dewi was stored first. The query does not say how to order rows that tie (rows with the same value), so the order of Dewi and Andi is not decided.

Meaning: A column you sort by is called a key (not the primary key). name ASC is the second key: it is used only for rows whose first key (grade) is equal. So Andi (letter A) now comes before Dewi (letter D).

⚡ One ORDER BY

Meaning: All the keys go in one ORDER BY, separated by commas. A direction like DESC applies only to the column written right before it: ORDER BY grade, name DESC means grade ASC, name DESC.

What you will learn