BBampaa

Lessons › SQL › Chapter 9: Data Cleaning

SQL lesson

IS NULL and COALESCE() for missing data

First find the empty cells: WHERE discount IS NULL shows which rows are missing a value. Then fill them for your result: COALESCE(discount, 0) shows 0 where it's empty.

Try this lesson free →
About 5–10 minutes · no sign-up needed

Example

Find and fill the gaps

SELECT *
FROM customers
WHERE phone IS NULL

Result:

customer_idfull_namephone
C102Bob Tanaka
C104David Park

Step by step

  1. IS NULL finds the empty cells.
    SELECT * FROM customers WHERE phone IS NULL
  2. Fills the empty cells: 0 for numbers, a word like 'Unknown' for text.
    SELECT full_name, COALESCE(phone, 'No phone') AS phone FROM customers

Practice

Try each one in your head first, then open the answer.

1. Using customers, show the customers with no phone.
SELECT * FROM customers WHERE phone IS NULL
2. Using inventory, find the products whose stock_count is missing.
SELECT * FROM inventory WHERE stock_count IS NULL

Learn it by doing. This lesson is a short hands-on level on Bampaa: see it work, then build and write it yourself, with instant feedback.

Try this lesson free →
About 5–10 minutes · no sign-up needed