Solve “Coalesce null salaries to zero”

Free. Runs in your browser, no install.

SQL · NULL Handling & Data Types

COALESCE vs NULLIF

Easycoalescenullifnull

Interview question

What is the difference between COALESCE and NULLIF?

How to answer

The short answer

COALESCE() returns the first non-NULL value.

SQL
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.

SQL
NULLIF(quantity, 0)

is useful for avoiding division by zero:

SQL
sales / NULLIF(quantity, 0)

Lock it in with a real drill

You remember an answer longer once you have used it. Try a hands-on problem on this concept.

Open related drill