← Back to SQL, page 1

sql-0006 · Medium mid

Where do NULL values appear when you sort, and how do you control it?

Engines disagree. SQLite and MySQL treat NULL as smaller than any value, so NULLs come first in ascending order. PostgreSQL and Oracle treat NULL as larger, so NULLs come last in ascending order. Write NULLS FIRST or NULLS LAST to make the order explicit; SQLite has supported it since 3.30.

Full answer

A NULL has no value, so the standard leaves it to each engine to decide whether NULLs sort high or low. The difference shows up as soon as you move a query between databases.

Here is the same query on SQLite, sorting salaries ascending:

sql
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');
sql
SELECT name, salary FROM employees ORDER BY salary, name LIMIT 3;
namesalary
LenaNULL
Omar48000
Diego55000

And on PostgreSQL:

sql
SELECT name, salary FROM employees ORDER BY salary DESC, name LIMIT 3;
namesalary
LenaNULL
Asha120000
Mei95000

Lena's unknown salary is first on SQLite and last on PostgreSQL. In descending order each engine flips its choice.

The portable fix is to say what you want:

sql
SELECT name, salary FROM employees ORDER BY salary DESC NULLS LAST, name LIMIT 3;
namesalary
Asha120000
Mei95000
Ravi95000

If an engine lacks NULLS LAST (older MySQL versions, for example), the usual workaround is to sort on a flag first: ORDER BY salary IS NULL, salary. The expression salary IS NULL is 0 for known values and 1 for NULL, so NULLs go to the end.

This matters for top-N reports. ORDER BY salary DESC LIMIT 3 on PostgreSQL puts the NULL salary at the very top, which is rarely what a "highest paid" report wants.

sql
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 name, salary FROM employees ORDER BY salary, name LIMIT 3;

SELECT name, salary FROM employees ORDER BY salary DESC NULLS LAST, name LIMIT 3;

Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.

Key points

  • NULL sort position is engine-specific
  • SQLite and MySQL: NULLs first ascending; PostgreSQL and Oracle: NULLs last ascending
  • NULLS FIRST and NULLS LAST make the intent explicit
  • ORDER BY col IS NULL, col is a portable fallback

Common mistakes

  • Writing a top-N query with DESC on PostgreSQL and getting NULL as the top value
  • Assuming tests on SQLite prove the order in production PostgreSQL

Follow-ups

  • How would you sort NULLs last on an engine without NULLS LAST?
  • Do indexes store NULLs, and does that affect sorted reads?

Tips

  • Name at least two engines and which way each sorts NULLs
  • Offer the explicit NULLS LAST version as the answer you would ship