Full answer
ORDER BY accepts a list. The database sorts by the first key, and only uses the second key to break ties within equal values of the first, and so on.
Each key gets its own direction. ORDER BY dept_id, salary DESC means "department ascending, then salary descending". A common slip is to think DESC applies to every column before it. It does not; it applies only to the key it follows.
CREATE TABLE departments (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE employees (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
dept_id INTEGER REFERENCES departments(id),
manager_id INTEGER REFERENCES employees(id),
salary INTEGER,
hire_date TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
(1,'Asha', 1,NULL,120000,'2019-03-01'),
(2,'Ravi', 1,1, 95000,'2020-07-15'),
(3,'Mei', 1,1, 95000,'2021-01-10'),
(4,'Tom', 2,1, 70000,'2022-05-20'),
(5,'Sara', 2,4, 62000,'2023-02-01'),
(6,'Diego',3,1, 55000,'2021-11-30'),
(7,'Lena', NULL,4, NULL, '2024-06-03'),
(8,'Omar', 3,6, 48000,'2024-09-09');SELECT dept_id, name, salary
FROM employees
ORDER BY dept_id, salary DESC, name;| dept_id | name | salary |
|---|---|---|
| NULL | Lena | NULL |
| 1 | Asha | 120000 |
| 1 | Mei | 95000 |
| 1 | Ravi | 95000 |
| 2 | Tom | 70000 |
| 2 | Sara | 62000 |
| 3 | Diego | 55000 |
| 3 | Omar | 48000 |
Ravi and Mei both earn 95000 in Engineering. Without the last key, name, the database may return them in either order, and that order can change between runs or after an index is added. A unique final key makes the output fully determined, which matters for pagination and for tests that compare output.
Lena has no department. On SQLite, NULL sorts before every other value in ascending order, so her row appears first. PostgreSQL does the opposite and puts NULLs last in ascending order. If the position of missing values matters, say so explicitly with NULLS FIRST or NULLS LAST, which both engines accept.
CREATE TABLE departments (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE employees (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
dept_id INTEGER REFERENCES departments(id),
manager_id INTEGER REFERENCES employees(id),
salary INTEGER,
hire_date TEXT
);
INSERT INTO departments VALUES (1,'Engineering'),(2,'Sales'),(3,'Support'),(4,'Legal');
INSERT INTO employees VALUES
(1,'Asha', 1,NULL,120000,'2019-03-01'),
(2,'Ravi', 1,1, 95000,'2020-07-15'),
(3,'Mei', 1,1, 95000,'2021-01-10'),
(4,'Tom', 2,1, 70000,'2022-05-20'),
(5,'Sara', 2,4, 62000,'2023-02-01'),
(6,'Diego',3,1, 55000,'2021-11-30'),
(7,'Lena', NULL,4, NULL, '2024-06-03'),
(8,'Omar', 3,6, 48000,'2024-09-09');
SELECT dept_id, name, salary
FROM employees
ORDER BY dept_id, salary DESC, name;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- Each ORDER BY key has its own ASC or DESC
- Later keys only break ties in earlier keys
- End with a unique key for a stable, repeatable order
- NULL placement differs by engine; NULLS FIRST or LAST makes it explicit
Common mistakes
- Thinking DESC applies to all listed columns
- Relying on ties coming back in insert order
Follow-ups
- How would you put employees with no department at the end on every engine?
- Why does a stable order matter for OFFSET pagination?
Tips
- Mention the tie-breaker without being asked; it shows you have debugged flaky tests