SQL GROUP BY and HAVING practice: 8 exercises with answers

Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)

Eight exercises on summarising data: COUNT and COUNT(DISTINCT), one row per group with GROUP BY, sorting by an aggregate, and filtering groups with HAVING. On the shelter's pets table.

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 SQL GROUP BY and HAVING 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

    How many pets are in the shelter? Count all of them.

    From the lesson Count, sum, average · Paws & Whiskers Shelter

    Write your query here…
    Show the answer

    Answer (PostgreSQL and SQLite)

    SELECT COUNT(*) FROM pets;

    Expected result (SQLite): 1 row

    COUNT(*)
    20
  2. 2

    How many different species live at the shelter?

    From the lesson Count, sum, average · Paws & Whiskers Shelter

    Write your query here…
    Show the answer

    Answer (PostgreSQL and SQLite)

    SELECT COUNT(DISTINCT species) FROM pets;

    Expected result (SQLite): 1 row

    COUNT(DISTINCT species)
    4
  3. 3

    Count pets per species. Show species and the count.

    From the lesson GROUP BY · Paws & Whiskers Shelter

    Write your query here…
    Show the answer

    Answer (PostgreSQL and SQLite)

    SELECT species, COUNT(*) FROM pets GROUP BY species;

    Expected result (SQLite): 4 rows

    speciesCOUNT(*)
    bird2
    cat7
    dog8
    rabbit3
  4. 4

    Show each species and its average age as avg_age.

    From the lesson GROUP BY · Paws & Whiskers Shelter

    Write your query here…
    Show the answer

    Answer (PostgreSQL and SQLite)

    SELECT species, AVG(age) AS avg_age FROM pets GROUP BY species;

    Expected result (SQLite): 4 rows

    speciesavg_age
    bird4
    cat4.857142857142857
    dog5.125
    rabbit2
  5. 5

    Count pets per species, most common first. Sort by the count itself.

    From the lesson GROUP BY · Paws & Whiskers Shelter

    Write your query here…
    Show the answer

    Answer (PostgreSQL and SQLite)

    SELECT species, COUNT(*) FROM pets GROUP BY species ORDER BY COUNT(*) DESC;

    Expected result (SQLite): 4 rows

    speciesCOUNT(*)
    dog8
    cat7
    rabbit3
    bird2
  6. 6

    Which species have more than 3 pets? Show species and count.

    From the lesson HAVING · Paws & Whiskers Shelter

    Write your query here…
    Show the answer

    Answer (PostgreSQL and SQLite)

    SELECT species, COUNT(*) FROM pets GROUP BY species HAVING COUNT(*) > 3;

    Expected result (SQLite): 2 rows

    speciesCOUNT(*)
    cat7
    dog8
  7. 7

    Which species have an average weight above 5 kg? Show just the species.

    From the lesson HAVING · Paws & Whiskers Shelter

    Write your query here…
    Show the answer

    Answer (PostgreSQL and SQLite)

    SELECT species FROM pets GROUP BY species HAVING AVG(weight_kg) > 5;

    Expected result (both engines): 1 row

    species
    dog
  8. 8

    Among pets not adopted, which species have at least 2 waiting? Show species and count.

    From the lesson HAVING · Paws & Whiskers Shelter

    Write your query here…
    Show the answer

    Answer (PostgreSQL and SQLite)

    SELECT species, COUNT(*) FROM pets WHERE adopted = false GROUP BY species HAVING COUNT(*) >= 2;

    Expected result (SQLite): 2 rows

    speciesCOUNT(*)
    cat4
    dog6

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.