SQL window functions 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 window functions: numbering and ranking rows, ranking within a group, running totals, the previous row with LAG, totals beside every row and splitting rows into quartiles.

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 window functions guide first.

Answers are checked on the engine you pick.
The tables

Rocket Pizza

  • customers (id, name, city, joined_on)
  • menu_items (id, name, category, price, vegetarian)
  • orders (id, customer_id, ordered_on, status)
  • order_items (order_id, item_id, quantity)
  • staff (id, name, role, city, manager_id, hired_on)

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

    Number the menu items from most expensive (1) to cheapest. Show name, price and the number as price_order.

    From the lesson Ranking with OVER · Rocket Pizza

    Write your query here…
    Show the answer

    Answer (PostgreSQL and SQLite)

    SELECT name, price, ROW_NUMBER() OVER (ORDER BY price DESC) AS price_order FROM menu_items;

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

    namepriceprice_order
    BBQ Meteor15.51
    Four Cheese Orbit142
    Pepperoni Rocket13.53
    Veggie Nebula134
    Margherita115
    Wings8.56
    Caesar Salad77
    Tiramisu6.58
  2. 2

    Rank menu items by price within each category (most expensive = 1). Show category, name, price and price_rank.

    From the lesson Ranking with OVER · Rocket Pizza

    Write your query here…
    Show the answer

    Answer (PostgreSQL and SQLite)

    SELECT category, name, price, RANK() OVER (PARTITION BY category ORDER BY price DESC) AS price_rank FROM menu_items;

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

    categorynamepriceprice_rank
    dessertTiramisu6.51
    drinkLemon Soda31
    drinkCola2.52
    pizzaBBQ Meteor15.51
    pizzaFour Cheese Orbit142
    pizzaPepperoni Rocket13.53
    pizzaVeggie Nebula134
    pizzaMargherita115
  3. 3

    Show a running total of menu prices from cheapest to most expensive: name, price and running_total.

    From the lesson Running totals · Rocket Pizza

    Write your query here…
    Show the answer

    Answer (PostgreSQL and SQLite)

    SELECT name, price, SUM(price) OVER (ORDER BY price) AS running_total FROM menu_items;

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

    namepricerunning_total
    Cola2.52.5
    Lemon Soda35.5
    Garlic Knots510.5
    Tiramisu6.517
    Caesar Salad724
    Wings8.532.5
    Margherita1143.5
    Veggie Nebula1356.5
  4. 4

    List the menu items from cheapest to most expensive with the price of the item just before as previous_price. Show name, price and previous_price.

    From the lesson Running totals · Rocket Pizza

    Write your query here…
    Show the answer

    Answer (PostgreSQL and SQLite)

    SELECT name, price, LAG(price) OVER (ORDER BY price) AS previous_price FROM menu_items;

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

    namepriceprevious_price
    Cola2.5NULL
    Lemon Soda32.5
    Garlic Knots53
    Tiramisu6.55
    Caesar Salad76.5
    Wings8.57
    Margherita118.5
    Veggie Nebula1311
  5. 5

    For every order, show customer_id, id, ordered_on and the date of that customer's previous order as previous_order (NULL for their first). Order each customer's orders by date, then id.

    From the lesson Running totals · Rocket Pizza

    Write your query here…
    Show the answer

    Answer (PostgreSQL and SQLite)

    SELECT customer_id, id, ordered_on, LAG(ordered_on) OVER (PARTITION BY customer_id ORDER BY ordered_on, id) AS previous_order FROM orders;

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

    customer_ididordered_onprevious_order
    112026-03-01NULL
    132026-03-032026-03-01
    192026-03-102026-03-03
    1162026-03-222026-03-10
    222026-03-01NULL
    272026-03-072026-03-01
    2132026-03-152026-03-07
    342026-03-04NULL
  6. 6

    Show every menu item's name, price and avg_price: the average price of the whole menu, on every row.

    From the lesson Frames and shares · Rocket Pizza

    Write your query here…
    Show the answer

    Answer (PostgreSQL and SQLite)

    SELECT name, price, AVG(price) OVER () AS avg_price FROM menu_items;

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

    namepriceavg_price
    Margherita119.045454545454545
    Pepperoni Rocket13.59.045454545454545
    Four Cheese Orbit149.045454545454545
    BBQ Meteor15.59.045454545454545
    Veggie Nebula139.045454545454545
    Garlic Knots59.045454545454545
    Wings8.59.045454545454545
    Caesar Salad79.045454545454545
  7. 7

    Count the pets of each species as pets, and show the number of all pets as all_pets on every row.

    From the lesson Frames and shares · Paws & Whiskers Shelter

    Write your query here…
    Show the answer

    Answer (PostgreSQL and SQLite)

    SELECT species, COUNT(*) AS pets, SUM(COUNT(*)) OVER () AS all_pets FROM pets GROUP BY species;

    Expected result (SQLite): 4 rows

    speciespetsall_pets
    bird220
    cat720
    dog820
    rabbit320
  8. 8

    Split pets into weight quartiles: show name, weight_kg and quartile (1 = lightest).

    From the lesson Buckets and subtotals · Paws & Whiskers Shelter

    Write your query here…
    Show the answer

    Answer (PostgreSQL and SQLite)

    SELECT name, weight_kg, NTILE(4) OVER (ORDER BY weight_kg) AS quartile FROM pets;

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

    nameweight_kgquartile
    Ziggy0.11
    Kiwi0.11
    Waffles1.61
    Clover2.11
    Pepper3.21
    Juniper3.42
    Luna3.82
    Olive3.92

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.