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.

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

    List every pet's name and age, youngest first.

    From the lesson Sort and limit · Paws & Whiskers Shelter

    Write your query here…
    Show the answer

    Answer (PostgreSQL and SQLite)

    SELECT name, age FROM pets ORDER BY age;

    Expected result (SQLite): first 8 of 20 rows

    nameage
    Waffles1
    Pepper1
    Bean1
    Mochi2
    Maple2
    Kiwi2
    Juniper2
    Biscuit3
  2. 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 answer

    Answer (PostgreSQL and SQLite)

    SELECT name, age FROM pets ORDER BY age DESC;

    Expected result (SQLite): first 8 of 20 rows

    nameage
    Tofu11
    Gizmo10
    Bruno9
    Socks8
    Pickles7
    Ziggy6
    Luna5
    Rocket5
  3. 3

    List the names of all cats, alphabetically.

    From the lesson Sort and limit · Paws & Whiskers Shelter

    Write your query here…
    Show 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. 4

    Show the names of the 3 youngest pets.

    From the lesson Sort and limit · Paws & Whiskers Shelter

    Write your query here…
    Show 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. 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 answer

    Answer (PostgreSQL and SQLite)

    SELECT name, weight_kg FROM pets ORDER BY weight_kg DESC LIMIT 1;

    Expected result (both engines): 1 row

    nameweight_kg
    Bruno30.1
  6. 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 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

    speciesnameweight_kg
    birdZiggy0.1
    birdKiwi0.1
    catSocks7.4
    catPumpkin5.2
    catTofu4.6
    catMochi4.1
    catOlive3.9
    catLuna3.8
  7. 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 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

    nameweight_kg
    Rocket19.8
    Biscuit11.2
    Gizmo9.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.

More practice sets

Check your level

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