SQL Beginner Challenge 12: Handling NULL Values with COALESCE

Difficulty: Beginner
Estimated time: 10–15 minutes
SQL concepts: NULL, COALESCE, default values
Goal: Replace missing values with safe, readable defaults.

The Scenario

While reviewing a product report, a stakeholder points out an issue:

“Some fields are empty. Can we show something meaningful instead of blank values?”

In real-world databases, NULL values are common.
COALESCE helps you replace them with a default value directly in SQL.

Database Table

products

column_nametypedescription
product_idINTEGERUnique product ID
nameTEXTProduct name
categoryTEXTProduct category
priceDECIMALProduct price
supplier_notesTEXTOptional notes from supplier (can be NULL)

Sample Data

product_idnamecategorypricesupplier_notes
101Wireless MouseAccessories24.99NULL
102Mechanical KeyboardAccessories89.00Best seller
10327-inch MonitorDisplays229.99NULL
104USB-C HubAccessories34.50Limited stock
105Laptop StandWorkspace39.99NULL

Your Task

Write a SQL query that returns:

  • Product name
  • supplier_notes

…but if supplier_notes is NULL, show the text:

“No notes available”

Expected Output

namesupplier_notes
Wireless MouseNo notes available
Mechanical KeyboardBest seller
27-inch MonitorNo notes available
USB-C HubLimited stock
Laptop StandNo notes available

Constraints

  • Use the COALESCE function
  • Do not modify the underlying data
  • Do not use SELECT *

Hint (Optional)

COALESCE returns the first non-NULL value in the list:

COALESCE(column, fallback_value)

SELECT
name,
COALESCE(supplier_notes, 'No notes available') AS supplier_notes
FROM products;

Explanation

  • If supplier_notes has a value, it is returned.
  • If it’s NULL, the fallback text is used instead.
  • This keeps reports readable and avoids empty fields.

Common Mistakes

  1. Checking for NULL with = supplier_notes = NULL -- ❌ always false
  2. Forgetting column aliases
  3. Trying to update data instead of handling it in the query

Optional Extension (Mini Bonus)

Try:

  1. Replace missing prices with 0.
  2. Use COALESCE with numeric columns.
  3. Combine COALESCE with CASE for more complex defaults.

Next Challenge

Beginner Challenge #13: Filtering NULL Values with IS NULL and IS NOT NULL

🔗 View reference solution on GitHub
(After you’ve tried the challenge)

Want more practical SQL challenges?
Subscribe to the Solve With SQL newsletter and get new problems delivered to your inbox.