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.
The tables
- 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)
- pets (id, name, species, breed, age, weight_kg, adopted, arrived_on)
- adoption_requests (id, pet_name, email, phone, requested_on, budget)
- 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 answerHide 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
name price price_order BBQ Meteor 15.5 1 Four Cheese Orbit 14 2 Pepperoni Rocket 13.5 3 Veggie Nebula 13 4 Margherita 11 5 Wings 8.5 6 Caesar Salad 7 7 Tiramisu 6.5 8 - 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 answerHide 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
category name price price_rank dessert Tiramisu 6.5 1 drink Lemon Soda 3 1 drink Cola 2.5 2 pizza BBQ Meteor 15.5 1 pizza Four Cheese Orbit 14 2 pizza Pepperoni Rocket 13.5 3 pizza Veggie Nebula 13 4 pizza Margherita 11 5 - 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 answerHide 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
name price running_total Cola 2.5 2.5 Lemon Soda 3 5.5 Garlic Knots 5 10.5 Tiramisu 6.5 17 Caesar Salad 7 24 Wings 8.5 32.5 Margherita 11 43.5 Veggie Nebula 13 56.5 - 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 answerHide 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
name price previous_price Cola 2.5 NULL Lemon Soda 3 2.5 Garlic Knots 5 3 Tiramisu 6.5 5 Caesar Salad 7 6.5 Wings 8.5 7 Margherita 11 8.5 Veggie Nebula 13 11 - 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 answerHide 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_id id ordered_on previous_order 1 1 2026-03-01 NULL 1 3 2026-03-03 2026-03-01 1 9 2026-03-10 2026-03-03 1 16 2026-03-22 2026-03-10 2 2 2026-03-01 NULL 2 7 2026-03-07 2026-03-01 2 13 2026-03-15 2026-03-07 3 4 2026-03-04 NULL - 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 answerHide 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
name price avg_price Margherita 11 9.045454545454545 Pepperoni Rocket 13.5 9.045454545454545 Four Cheese Orbit 14 9.045454545454545 BBQ Meteor 15.5 9.045454545454545 Veggie Nebula 13 9.045454545454545 Garlic Knots 5 9.045454545454545 Wings 8.5 9.045454545454545 Caesar Salad 7 9.045454545454545 - 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 answerHide 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
species pets all_pets bird 2 20 cat 7 20 dog 8 20 rabbit 3 20 - 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 answerHide 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
name weight_kg quartile Ziggy 0.1 1 Kiwi 0.1 1 Waffles 1.6 1 Clover 2.1 1 Pepper 3.2 1 Juniper 3.4 2 Luna 3.8 2 Olive 3.9 2
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.
- Ranking with OVER7 more exercises · sqlstreak Pro
- Running totals3 more exercises · sqlstreak Pro
- Frames and shares8 more exercises · sqlstreak Pro
- Buckets and subtotals6 more exercises · sqlstreak Pro
More practice sets
Check your level
The free SQL test takes about 10 minutes and tells you which lesson to start with.