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 |
ORDER BY gradechooses the column to sort by.DESC(descending) means from large to small. For text, from Z to A.ASC(ascending) means from small to large. For text, from A to Z.ASCis the default direction, soORDER BY nameis the same asORDER BY name ASC.ORDER BYcomes afterFROM, and afterWHEREwhen there is one:SELECT * FROM students WHERE city = 'London' ORDER BY name;.
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 |
grade DESCis used first. Maya (92) stays at the top.name ASCis used only for rows whosegradeis exactly the same. Andi and Dewi both have 90, so Andi (letter A) now comes before Dewi (letter D).- Each key has its own direction.
grade DESC, name ASCis different fromgrade DESC, name DESC. - All the keys go in one
ORDER BY, separated by commas.
💡 SQLite, MySQL and PostgreSQL differ here
- NULL (empty, no value yet). SQLite and MySQL treat NULL as smaller than every value. So a row with NULL comes first with
ASCand last withDESC. PostgreSQL does the opposite. To be sure where NULL goes, writeORDER BY grade DESC NULLS LAST(NULL at the bottom). SQLite (3.30 and later) and PostgreSQL accept it, MySQL does not have it yet. - Upper and lower case. SQLite compares text by the code of each character (the number that stands for it in the computer), and every capital letter comes before every lowercase letter. So a student named
andi(all lowercase) is placed afterRizky. MySQL, with its default settings, treats upper and lower case as the same. In SQLite, writeORDER BY name COLLATE NOCASEwhen you want dictionary order.
⚠️ A doubled ORDER BY, and a DESC that hits the wrong column
- Writing ORDER BY twice. SQLite refuses
ORDER BY grade DESC ORDER BY namewithnear "ORDER": syntax error. A query has only oneORDER BY. Further keys are separated by commas. - Thinking DESC applies to every column.
ORDER BY grade, name DESCmeansgrade ASC, name DESC. A direction applies only to the column written right before it. - The wrong column name.
ORDER BY scoreis answered withno such column: score. The column is calledgrade. - Leaving out the second key. When values can tie and the order matters (for a ranking, or a list split into pages), add a second key.
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.