SQL Level 3 Test
This test checks Level 3 of the course, Advanced: the advanced lessons: CTEs, window functions, design, concurrency, performance, procedural SQL and JSON. It has two parts. The 15 questions test that you know what each feature does; the 5 hands-on tasks test that you can use it, the way real work does: you run your own SQL in Try it and compare it with the solution. If you pass, you're ready for the next level; if you don't, every question links to the lesson that explains it.
How the Test Works
- Part 1: 15 multiple-choice questions, marked for you.
- Part 2: 5 tasks of these kinds: Write, Fix and Optimize. Write your query, run it, then open the solution and compare the rows. Any query that returns the same rows passes; the order matters only when the task asks for one.
- Pass mark: 15 out of 20 (questions plus tasks). Give yourself one point per task whose result matches.
- Time: about 45 minutes. No notes for Part 1; Part 2 is open-book, as real work is.
- Covers: the lessons from Common Table Expressions to Generated Columns and Domains. Some tasks use the shop dataset, an online store with 5,000 customers and 60,000 orders; Try it loads it the first time you run one of those tasks, which takes a few seconds.
Part 1: 15 Questions
Part 2: 5 Hands-On Tasks
Task 1 (Write): Latest payment per student. For each student with payments, show their most recent payment date and amount, using a window function.
Show solution
SELECT first_name, paid_on, amount_usd
FROM (
SELECT s.first_name, p.paid_on, p.amount_usd,
ROW_NUMBER() OVER (PARTITION BY p.student_id
ORDER BY p.paid_on DESC, p.payment_id DESC) AS rn
FROM payments p
JOIN students s ON s.student_id = p.student_id
) AS ranked
WHERE rn = 1
ORDER BY first_name; first_name | paid_on | amount_usd
------------+------------+------------
Navika | 2025-12-19 | 225.00
Srihitha | 2026-01-05 | 200.00
(2 rows)Task 2 (Write (shop)): Monthly revenue with change. On the shop dataset, show revenue (quantity times unit price, delivered orders only) for each month of 2025 and the change from the previous month.
Show solution
WITH monthly AS (
SELECT date_trunc('month', o.ordered_on)::date AS month,
sum(i.quantity * i.unit_price_usd) AS revenue_usd
FROM orders o
JOIN order_items i ON i.order_id = o.order_id
WHERE o.status = 'delivered'
AND o.ordered_on >= '2025-01-01' AND o.ordered_on < '2026-01-01'
GROUP BY 1
)
SELECT to_char(month, 'YYYY-MM') AS month, revenue_usd,
revenue_usd - lag(revenue_usd) OVER (ORDER BY month) AS change_usd
FROM monthly
ORDER BY month; month | revenue_usd | change_usd
---------+-------------+------------
2025-01 | 505543.50 | NULL
2025-02 | 459243.50 | -46300.00
2025-03 | 508992.50 | 49749.00
2025-04 | 489691.50 | -19301.00
2025-05 | 505545.50 | 15854.00
2025-06 | 495053.00 | -10492.50
2025-07 | 507352.50 | 12299.50
2025-08 | 507213.50 | -139.00
2025-09 | 491436.50 | -15777.00
2025-10 | 505231.50 | 13795.00
2025-11 | 492655.00 | -12576.50
2025-12 | 393749.50 | -98905.50
(12 rows)Task 3 (Write (shop)): Top product per category. On the shop dataset, find the best-selling product (by units) in each category, keeping ties.
Show solution
WITH units AS (
SELECT p.category, p.name, sum(i.quantity) AS units
FROM order_items i
JOIN products p ON p.product_id = i.product_id
GROUP BY p.category, p.name
)
SELECT category, name, units
FROM (
SELECT u.*, RANK() OVER (PARTITION BY category ORDER BY units DESC) AS r
FROM units u
) ranked
WHERE r = 1
ORDER BY category, name; category | name | units
-------------+-------------------------+-------
books | SQL Pocket Guide | 10319
courses | SQL Basics Video Course | 10316
electronics | Monitor 24 Inch | 10315
stationery | Index Cards, 300 | 10323
(4 rows)Task 4 (Optimize (shop)): One customer's orders. This query reads every order to find one customer's: SELECT order_id, ordered_on FROM orders WHERE customer_id = 3 ORDER BY ordered_on;. Show its plan, add the right index, and show the new plan.
Show solution
EXPLAIN (COSTS OFF)
SELECT order_id, ordered_on FROM orders WHERE customer_id = 3 ORDER BY ordered_on;
CREATE INDEX orders_customer_ordered_idx ON orders (customer_id, ordered_on);
EXPLAIN (COSTS OFF)
SELECT order_id, ordered_on FROM orders WHERE customer_id = 3 ORDER BY ordered_on; QUERY PLAN
-----------------------------------
Sort
Sort Key: ordered_on
-> Seq Scan on orders
Filter: (customer_id = 3)
(4 rows)
CREATE INDEX
QUERY PLAN
--------------------------------------------------------------
Sort
Sort Key: ordered_on
-> Bitmap Heap Scan on orders
Recheck Cond: (customer_id = 3)
-> Bitmap Index Scan on orders_customer_ordered_idx
Index Cond: (customer_id = 3)
(6 rows)Task 5 (Fix): NOT IN returns nothing. This should list students with no payments, but it returns no rows once a payment with a NULL student_id exists. Fix it: SELECT first_name FROM students WHERE student_id NOT IN (SELECT student_id FROM payments_copy);
Show solution
CREATE TEMP TABLE payments_copy AS SELECT student_id FROM payments;
INSERT INTO payments_copy VALUES (NULL);
-- Broken: NOT IN with a NULL in the list is never true
SELECT first_name FROM students
WHERE student_id NOT IN (SELECT student_id FROM payments_copy);
-- Fixed: NOT EXISTS ignores the NULL
SELECT first_name FROM students s
WHERE NOT EXISTS (SELECT 1 FROM payments_copy p WHERE p.student_id = s.student_id)
ORDER BY first_name;SELECT 7
INSERT 0 1
first_name
------------
(0 rows)
first_name
------------
Advitha
(1 row)What Your Score Means
| Score | What to do |
|---|---|
| 18 to 20 | Excellent. Move on to the SQL Level 4 Test when you reach it. |
| 15 to 17 | A pass. Reread the lessons linked from the questions you missed. |
| Below 15 | Not yet. Revise the linked lessons and take the test again in a day or two. |
Was this tutorial helpful?