← Back to SQL, page 3

sql-0046 · Hard senior

Write a query that returns the larger of two nullable columns per row. What do SQLite's max(a, b) and PostgreSQL's GREATEST(a, b) do with NULL?

SQLite's two-argument max(a, b) returns NULL if any argument is NULL. PostgreSQL's GREATEST(a, b) ignores NULLs and returns NULL only when all arguments are NULL. MySQL's GREATEST returns NULL if any argument is NULL. For portable results, use CASE with explicit NULL checks.

Full answer

Each student sat a test twice, and the report shows the better score. Some students missed one sitting.

On SQLite, the scalar (multi-argument) form of max is used:

sql
CREATE TABLE attempts (student TEXT, first_try INTEGER, second_try INTEGER);
INSERT INTO attempts VALUES ('Ana',70,85),('Ben',90,NULL),('Chen',NULL,60),('Dara',NULL,NULL);
sql
SELECT student, first_try, second_try, max(first_try, second_try) AS best
FROM attempts ORDER BY student;
studentfirst_trysecond_trybest
Ana708585
Ben90NULLNULL
ChenNULL60NULL
DaraNULLNULLNULL

Ben missed the retest and Chen missed the first test. SQLite's max(a, b) returns NULL as soon as any argument is NULL, so both lost their score.

PostgreSQL's GREATEST skips NULLs:

sql
SELECT student, GREATEST(first_try, second_try) AS best
FROM attempts ORDER BY student;
studentbest
Ana85
Ben90
Chen60
DaraNULL

MySQL's GREATEST behaves like SQLite here and returns NULL when any argument is NULL; that comes from MySQL documentation and was not run here.

A version that gives the PostgreSQL behaviour on any engine:

sql
SELECT student,
       CASE
         WHEN first_try  IS NULL THEN second_try
         WHEN second_try IS NULL THEN first_try
         WHEN first_try >= second_try THEN first_try
         ELSE second_try
       END AS best
FROM attempts ORDER BY student;
studentbest
Ana85
Ben90
Chen60
DaraNULL

The CASE checks NULL first, then compares. It returns NULL only when both scores are missing, which is the honest answer for Dara. Using COALESCE(first_try, 0) would also produce numbers, but it would turn "both missing" into a score of 0, which is a different statement.

sql
CREATE TABLE attempts (student TEXT, first_try INTEGER, second_try INTEGER);
INSERT INTO attempts VALUES ('Ana',70,85),('Ben',90,NULL),('Chen',NULL,60),('Dara',NULL,NULL);

SELECT student, first_try, second_try, max(first_try, second_try) AS best
FROM attempts ORDER BY student;

SELECT student,
       CASE
         WHEN first_try  IS NULL THEN second_try
         WHEN second_try IS NULL THEN first_try
         WHEN first_try >= second_try THEN first_try
         ELSE second_try
       END AS best
FROM attempts ORDER BY student;

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

Key points

  • SQLite max(a, b) and MySQL GREATEST return NULL if any argument is NULL
  • PostgreSQL GREATEST ignores NULLs
  • A CASE with explicit IS NULL checks is portable
  • COALESCE with 0 changes 'unknown' into 'zero'

Common mistakes

  • Assuming GREATEST behaves the same on PostgreSQL and MySQL
  • Using COALESCE(x, 0) where 0 is a meaningful value

Follow-ups

  • How does this differ from the aggregate MAX(col)?
  • How would you take the best of three nullable attempts?

Tips

  • Mention that aggregate MAX ignores NULLs while scalar max may not