Example 1
Sort scores ascending with NULLs at the end
Eve (78), Alice (85), and Carol (92) have scores and sort ascending by value. Bob and Dave have NULL scores. In MySQL and SQL Server, (score IS NULL) returns 0 for non-null rows and 1 for null rows — sorting that expression first pushes the NULLs to the bottom. In PostgreSQL and SQLite, NULLS LAST is a direct clause modifier on the ORDER BY column.
CREATE TABLE players (name VARCHAR(50), score INT);
INSERT INTO
players
VALUES
('Alice', 85),
('Bob', NULL),
('Carol', 92),
('Dave', NULL),
('Eve', 78);SELECT
name,
score
FROM
players
ORDER BY
(score IS NULL) ASC,
score ASC,
name ASC;| name | score |
|---|---|
| Eve | 78 |
| Alice | 85 |
| Carol | 92 |
| Bob | NULL |
| Dave | NULL |
CREATE TABLE players (name VARCHAR(50), score INT);
INSERT INTO
players
VALUES
('Alice', 85),
('Bob', NULL),
('Carol', 92),
('Dave', NULL),
('Eve', 78);SELECT
name,
score
FROM
players
ORDER BY
CASE
WHEN score IS NULL THEN 1
ELSE 0
END ASC,
score ASC,
name ASC;| name | score |
|---|---|
| Eve | 78 |
| Alice | 85 |
| Carol | 92 |
| Bob | NULL |
| Dave | NULL |
CREATE TABLE players (name VARCHAR(50), score INT);
INSERT INTO
players
VALUES
('Alice', 85),
('Bob', NULL),
('Carol', 92),
('Dave', NULL),
('Eve', 78);SELECT
name,
score
FROM
players
ORDER BY
score ASC NULLS LAST,
name ASC;| name | score |
|---|---|
| Eve | 78 |
| Alice | 85 |
| Carol | 92 |
| Bob | NULL |
| Dave | NULL |
PostgreSQL and SQLite use native NULLS LAST syntax. MySQL/MariaDB prefix with (score IS NULL) ASC — which evaluates to 0 for non-null and 1 for null, pushing nulls last. SQL Server uses an equivalent CASE expression.