Stop 04 · Functions & ExpressionsTopics · 7/8

COALESCE and NULLIF

Part of the SQL Roadmap

In short

NULLs break reports: blank cells in a dashboard, totals that vanish, division errors at 2 am. COALESCE fills gaps with a sensible default, and NULLIF creates a NULL on purpose to dodge errors.

COALESCE returns the first non-NULL value from a list, which is useful for defaults. NULLIF returns NULL when two values are equal, which helps avoid divide-by-zero errors.

The jaadu sits at an old office desk holding a landline phone, its finger moving down a short handwritten list of numbers.

Think of it like this

Emergency contact list

The college office needs to reach a student's family. Try the father's number. No number recorded? Try the mother's. Still nothing? Call the local guardian. COALESCE walks down a list the same way and returns the first value that's actually there.

Watch it happen

COALESCE(alt_phone, phone, 'not given')

NULL
0
98450 12345
1
not given
2

1/5COALESCE checks its arguments left to right.

The big ideas

What you need to know

COALESCE returns the first non-NULL

Any number of arguments. COALESCE(discount, 0) treats a missing discount as zero.

NULLIF(a, b)

Returns NULL when a equals b, otherwise a. Its most common use is NULLIF(denominator, 0).

Use them together

COALESCE(revenue / NULLIF(orders, 0), 0) avoids the error and shows 0 instead of a blank.

Defaults change meaning

Replacing NULL with 0 tells readers "zero" when the truth is "unknown". Choose defaults thoughtfully.

See it in code

Walk through the code

sql
1SELECT2  customer_name,3  COALESCE(alt_phone, phone, 'not given')   AS contact_no,4  COALESCE(discount, 0)                     AS discount_rs,5  amount - COALESCE(discount, 0)            AS payable6FROM deliveries;7 8SELECT9  city,10  revenue,11  order_count,12  ROUND(revenue / NULLIF(order_count, 0), 2)  AS avg_order_value13FROM city_summary;

1/3Try the alternate number, then the main one, then a fixed label.

COALESCE and NULLIF are standard SQL and work in PostgreSQL, MySQL and SQL Server. MySQL also has IFNULL and SQL Server ISNULL, but those take only two arguments.

Save yourself the pain

Common mistakes

Oops

revenue / order_count when some cities have 0 orders.

Do this instead

PostgreSQL and SQL Server raise a division by zero error. Use revenue / NULLIF(order_count, 0).

Oops

COALESCE(phone, 0) on a text column.

Do this instead

All arguments must share a type. Use COALESCE(phone, 'not given') or a text '0'.

Your mission

Fill gaps, dodge errors

~20 min

Quick check

What does COALESCE(NULL, NULL, 'Kolkata', 'Delhi') return?

Go deeper

  1. 1Replace missing phone numbers with 'Not given' using COALESCE.
  2. 2Write revenue / NULLIF(orders, 0) to make a safe division.
  3. 3Think about whether a default value could mislead someone reading the report.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge