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.

Answers are checked on the engine you pick.
The tables

Paws & Whiskers Shelter

  • pets (id, name, species, breed, age, weight_kg, adopted, arrived_on)
  • adoption_requests (id, pet_name, email, phone, requested_on, budget)
  1. 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 answer

    Answer (PostgreSQL and SQLite)

    SELECT name, species FROM pets WHERE breed IS NULL;

    Expected result (both engines): 6 rows

    namespecies
    Picklesdog
    Lunacat
    Cloverrabbit
    Tofucat
    Beandog
    Pumpkincat
  2. 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 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

    namebreed
    BiscuitBeagle
    PepperBengal
    RocketBorder Collie
    BrunoBoxer
    KiwiBudgie
    ZiggyCockatiel
    NoodleDachshund
    MapleGolden Retriever
  3. 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 answer

    Answer (PostgreSQL and SQLite)

    SELECT COUNT(breed) FROM pets;

    Expected result (SQLite): 1 row

    COUNT(breed)
    14
  4. 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 answer

    Answer (PostgreSQL and SQLite)

    SELECT name, COALESCE(breed, 'mixed') AS breed FROM pets;

    Expected result (both engines): first 8 of 20 rows

    namebreed
    BiscuitBeagle
    MochiSiamese
    Picklesmixed
    Lunamixed
    WafflesHolland Lop
    BrunoBoxer
    PepperBengal
    NoodleDachshund
  5. 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 answer

    Answer (PostgreSQL and SQLite)

    SELECT id, NULLIF(phone, '') AS phone FROM adoption_requests;

    Expected result (both engines): first 8 of 12 rows

    idphone
    1555-0101
    2NULL
    3NULL
    4555-0104
    5555-0105
    6NULL
    7555-0107
    8NULL
  6. 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 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

    idcontact
    1555-0101
    2no phone
    3no phone
    4555-0104
    5555-0105
    6no phone
    7555-0107
    8no 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.

More practice sets

Check your level

The free SQL test takes about 10 minutes and tells you which lesson to start with.