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.
The tables
- pets (id, name, species, breed, age, weight_kg, adopted, arrived_on)
- adoption_requests (id, pet_name, email, phone, requested_on, budget)
- 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 answerHide the answer
Answer (PostgreSQL and SQLite)
SELECT COUNT(*) FROM pets;Expected result (SQLite): 1 row
COUNT(*) 20 - 2
How many different species live at the shelter?
From the lesson Count, sum, average · Paws & Whiskers Shelter
Write your query here…Show the answerHide the answer
Answer (PostgreSQL and SQLite)
SELECT COUNT(DISTINCT species) FROM pets;Expected result (SQLite): 1 row
COUNT(DISTINCT species) 4 - 3
Count pets per species. Show species and the count.
From the lesson GROUP BY · Paws & Whiskers Shelter
Write your query here…Show the answerHide the answer
Answer (PostgreSQL and SQLite)
SELECT species, COUNT(*) FROM pets GROUP BY species;Expected result (SQLite): 4 rows
species COUNT(*) bird 2 cat 7 dog 8 rabbit 3 - 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 answerHide the answer
Answer (PostgreSQL and SQLite)
SELECT species, AVG(age) AS avg_age FROM pets GROUP BY species;Expected result (SQLite): 4 rows
species avg_age bird 4 cat 4.857142857142857 dog 5.125 rabbit 2 - 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 answerHide the answer
Answer (PostgreSQL and SQLite)
SELECT species, COUNT(*) FROM pets GROUP BY species ORDER BY COUNT(*) DESC;Expected result (SQLite): 4 rows
species COUNT(*) dog 8 cat 7 rabbit 3 bird 2 - 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 answerHide the answer
Answer (PostgreSQL and SQLite)
SELECT species, COUNT(*) FROM pets GROUP BY species HAVING COUNT(*) > 3;Expected result (SQLite): 2 rows
species COUNT(*) cat 7 dog 8 - 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 answerHide 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
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 answerHide 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
species COUNT(*) cat 4 dog 6
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.
- Count, sum, average7 more exercises · sqlstreak Pro
- GROUP BY4 more exercises · sqlstreak Pro
- HAVING1 more exercise · sqlstreak Pro
More practice sets
Check your level
The free SQL test takes about 10 minutes and tells you which lesson to start with.