SQL NULL practice: IS NULL, COALESCE and NULLIF exercises
Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)
Six exercises on missing data: finding NULLs, what COUNT does with them, replacing them with COALESCE and turning empty text into NULL with NULLIF. They use the shelter's pets and its messy adoption_requests import.
Write each query, press Check answer and get told right away whether your result matches. Stuck? Take a hint, or open the answer. New to the topic? Read the NULL in SQL: IS NULL, COALESCE and the traps guide first.
The tables
- pets (id, name, species, breed, age, weight_kg, adopted, arrived_on)
- adoption_requests (id, pet_name, email, phone, requested_on, budget)
- 1
Which pets are mixed breed (their breed is NULL)? Show name and species.
From the lesson Patterns and NULL · Paws & Whiskers Shelter
Write your query here…Show the answerHide the answer
Answer (PostgreSQL and SQLite)
SELECT name, species FROM pets WHERE breed IS NULL;Expected result (both engines): 6 rows
name species Pickles dog Luna cat Clover rabbit Tofu cat Bean dog Pumpkin cat - 2
List pets with a known breed, sorted by breed. Show name and breed.
From the lesson Patterns and NULL · Paws & Whiskers Shelter
Write your query here…Show the answerHide the answer
Answer (PostgreSQL and SQLite)
SELECT name, breed FROM pets WHERE breed IS NOT NULL ORDER BY breed;Expected result (both engines): first 8 of 14 rows
name breed Biscuit Beagle Pepper Bengal Rocket Border Collie Bruno Boxer Kiwi Budgie Ziggy Cockatiel Noodle Dachshund Maple Golden Retriever - 3
How many pets have a known breed? Count the breed column.
From the lesson Count, sum, average · Paws & Whiskers Shelter
Write your query here…Show the answerHide the answer
Answer (PostgreSQL and SQLite)
SELECT COUNT(breed) FROM pets;Expected result (SQLite): 1 row
COUNT(breed) 14 - 4
Show every pet's name and breed, with
'mixed'instead of NULL when the breed is missing. Name that column breed.From the lesson Empty, NULL and defaults · Paws & Whiskers Shelter
Write your query here…Show the answerHide the answer
Answer (PostgreSQL and SQLite)
SELECT name, COALESCE(breed, 'mixed') AS breed FROM pets;Expected result (both engines): first 8 of 20 rows
name breed Biscuit Beagle Mochi Siamese Pickles mixed Luna mixed Waffles Holland Lop Bruno Boxer Pepper Bengal Noodle Dachshund - 5
Show id and phone, with empty phones turned into NULL. Name the cleaned column phone.
From the lesson Empty, NULL and defaults · Paws & Whiskers Shelter
Write your query here…Show the answerHide the answer
Answer (PostgreSQL and SQLite)
SELECT id, NULLIF(phone, '') AS phone FROM adoption_requests;Expected result (both engines): first 8 of 12 rows
id phone 1 555-0101 2 NULL 3 NULL 4 555-0104 5 555-0105 6 NULL 7 555-0107 8 NULL - 6
Show id and contact: the phone number, or
'no phone'when it's missing either way (NULL or empty).From the lesson Empty, NULL and defaults · Paws & Whiskers Shelter
Write your query here…Show the answerHide the answer
Answer (PostgreSQL and SQLite)
SELECT id, COALESCE(NULLIF(phone, ''), 'no phone') AS contact FROM adoption_requests;Expected result (both engines): first 8 of 12 rows
id contact 1 555-0101 2 no phone 3 no phone 4 555-0104 5 555-0105 6 no phone 7 555-0107 8 no phone
The lessons have more like these
Each lesson teaches the idea step by step first, then gives you exercises with instant feedback and a daily streak.
- Patterns and NULL4 more exercises · free, no account needed
- Count, sum, average8 more exercises · sqlstreak Pro
- Empty, NULL and defaults2 more exercises · sqlstreak Pro
More practice sets
Check your level
The free SQL test takes about 10 minutes and tells you which lesson to start with.