Sub topic 13
Where NULL Values Appear in Sorted Output
Prerequisites
This chapter assumes you know that a table contains rows and columns, and that you can write basic SELECT and ORDER BY statements. You should also know that NULL means a missing or unknown value. It is not the same as zero, an empty string, or the text 'NULL'.
Why NULL Ordering Matters
Suppose some employees do not have a recorded manager:
SELECT first_name, manager_id
FROM employees
ORDER BY manager_id ASC;The database must decide where rows with manager_id IS NULL appear. SQL does not require every database to make the same default choice. Therefore, a query can produce different row orders after moving between database systems.
The ASC keyword sorts from low to high. The DESC keyword sorts from high to low. The position of NULL depends on the database and sometimes on the direction.
| Database | `ASC` default | `DESC` default |
|---|---|---|
| PostgreSQL | NULLs last | NULLs first |
| MySQL | NULLs first | NULLs last |
| SQLite | NULLs first | NULLs last |
| SQL Server | NULLs first | NULLs last |
Requesting a Specific NULL Position
Some databases support NULLS FIRST and NULLS LAST directly:
SELECT first_name, manager_id
FROM employees
ORDER BY manager_id ASC NULLS LAST;This puts known manager IDs in ascending order and places employees without a manager at the end. The following query puts missing values first:
SELECT first_name, manager_id
FROM employees
ORDER BY manager_id ASC NULLS FIRST;These options make your intent clear and prevent the default behavior from controlling the result. PostgreSQL, Oracle, and modern SQLite versions support this syntax. MySQL and SQL Server do not support it in the same form.
A Portable Sorting Technique
When your database does not support NULLS FIRST or NULLS LAST, sort by a condition first. In MySQL, SQL Server, and other systems that support boolean or conditional expressions, this pattern places missing values last:
SELECT first_name, manager_id
FROM employees
ORDER BY
CASE WHEN manager_id IS NULL THEN 1 ELSE 0 END,
manager_id ASC;The CASE expression returns 0 for known manager IDs and 1 for missing IDs. Because 0 sorts before 1, known values appear first. The second sort key orders the known IDs.
To place missing values first, reverse the returned numbers:
SELECT first_name, manager_id
FROM employees
ORDER BY
CASE WHEN manager_id IS NULL THEN 0 ELSE 1 END,
manager_id ASC;Combining NULL Ordering with Other Columns
You can use explicit NULL handling alongside ordinary multi-column sorting:
SELECT first_name, department, manager_id
FROM employees
ORDER BY
department ASC,
manager_id ASC NULLS LAST;The database first sorts departments alphabetically. Within each department, it sorts manager IDs and puts missing manager IDs at the end.
Exercise
Write a query that returns firstname and managerid, sorts known manager IDs from highest to lowest, and places employees without a manager at the end in PostgreSQL.
Show answer
SELECT firstname, managerid FROM employees ORDER BY manager_id DESC NULLS LAST;