SQL ORDER BY and LIMIT practice: 7 exercises with answers
Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)
Seven exercises on sorting and top-N questions: ascending and descending order, sorting by two columns, LIMIT for the first few rows and OFFSET for the next page.
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 ORDER BY, LIMIT and OFFSET 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
List every pet's name and age, youngest first.
From the lesson Sort and limit · Paws & Whiskers Shelter
Write your query here…Show the answerHide the answer
Answer (PostgreSQL and SQLite)
SELECT name, age FROM pets ORDER BY age;Expected result (SQLite): first 8 of 20 rows
name age Waffles 1 Pepper 1 Bean 1 Mochi 2 Maple 2 Kiwi 2 Juniper 2 Biscuit 3 - 2
List every pet's name and age, oldest first.
From the lesson Sort and limit · Paws & Whiskers Shelter
Write your query here…Show the answerHide the answer
Answer (PostgreSQL and SQLite)
SELECT name, age FROM pets ORDER BY age DESC;Expected result (SQLite): first 8 of 20 rows
name age Tofu 11 Gizmo 10 Bruno 9 Socks 8 Pickles 7 Ziggy 6 Luna 5 Rocket 5 - 3
List the names of all cats, alphabetically.
From the lesson Sort and limit · Paws & Whiskers Shelter
Write your query here…Show the answerHide the answer
Answer (PostgreSQL and SQLite)
SELECT name FROM pets WHERE species = 'cat' ORDER BY name;Expected result (both engines): 7 rows
name Luna Mochi Olive Pepper Pumpkin Socks Tofu - 4
Show the names of the 3 youngest pets.
From the lesson Sort and limit · Paws & Whiskers Shelter
Write your query here…Show the answerHide the answer
Answer (PostgreSQL and SQLite)
SELECT name FROM pets ORDER BY age LIMIT 3;Expected result (SQLite): 3 rows
name Waffles Pepper Bean - 5
Who's the heaviest pet? Show its name and weight_kg.
From the lesson Sort and limit · Paws & Whiskers Shelter
Write your query here…Show the answerHide the answer
Answer (PostgreSQL and SQLite)
SELECT name, weight_kg FROM pets ORDER BY weight_kg DESC LIMIT 1;Expected result (both engines): 1 row
name weight_kg Bruno 30.1 - 6
List every pet's species, name and weight_kg, sorted by species and, within each species, heaviest first.
From the lesson Tie-breakers and pages · Paws & Whiskers Shelter
Write your query here…Show the answerHide the answer
Answer (PostgreSQL and SQLite)
SELECT species, name, weight_kg FROM pets ORDER BY species, weight_kg DESC;Expected result (both engines): first 8 of 20 rows
species name weight_kg bird Ziggy 0.1 bird Kiwi 0.1 cat Socks 7.4 cat Pumpkin 5.2 cat Tofu 4.6 cat Mochi 4.1 cat Olive 3.9 cat Luna 3.8 - 7
Show the name and weight_kg of the 4th, 5th and 6th heaviest pets.
From the lesson Tie-breakers and pages · Paws & Whiskers Shelter
Write your query here…Show the answerHide the answer
Answer (PostgreSQL and SQLite)
SELECT name, weight_kg FROM pets ORDER BY weight_kg DESC LIMIT 3 OFFSET 3;Expected result (both engines): 3 rows
name weight_kg Rocket 19.8 Biscuit 11.2 Gizmo 9.1
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.
- Sort and limit2 more exercises · free, no account needed
- Tie-breakers and pages1 more exercise · free, no account needed
More practice sets
Check your level
The free SQL test takes about 10 minutes and tells you which lesson to start with.