Skip to content
LakeBench
ProblemsCommunityPricing
Sign inStart practicing
Back
  1. Home
  2. Interview prep
  3. COALESCE vs NULLIF

SQL · NULL Handling & Data Types

COALESCE vs NULLIF

Easysql-27
coalescenullifnull

Question

What is the difference between COALESCE and NULLIF?

Solution

COALESCE() returns the first non-NULL value.

COALESCE(phone, email, 'No contact')

means:

> Use phone; if NULL, use email; if that is NULL, use "No contact".

NULLIF(a,b) returns NULL when a = b.

NULLIF(quantity, 0)

is useful for avoiding division by zero:

sales / NULLIF(quantity, 0)

🎯 Put this concept into practice

Solidify this answer with real hands-on interview drills in the browser studio.

Open related drill →
PreviousNext