Skip to content
Justice Tech School

COALESCE and NULLIF

COALESCE returns the first non-NULL value. NULLIF turns a value into NULL, which avoids divide by zero.

Example
SELECT name, COALESCE(score, 0) AS score_or_zero FROM students WHERE id IN (7, 8);
SELECT 10 / NULLIF(0, 0) AS safe;

Result

safe
NULL

Exercise

First non-null.

(NULL, 5)

Quiz Test

What does NULLIF(a, b) give when equal?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.