01 · Basic SELECT
- SELECT … FROM
- WHERE
- ORDER BY (ASC/DESC)
End of the SQL Course
Congratulations: you have completed the 8 modules of the SQL Course — from the first SELECT queries to WITH RECURSIVE, window functions, and transactions. Below is the map of everything you now master and a final challenge that combines key parts of them. If you solve it, you are ready to write SQL in any real project.
Four techniques in a single query: a CTE that aggregates orders per customer, two JOINs to build it, a window function to assign rank, and LIMIT to return the podium. No new lesson: just combine what you have learned from Module 1 to Module 8.
Find the top 3 customers who have spent the most. Use a CTE to calculate total spending per customer (sum of quantity * unit_price on all order_items), then assign each a rank with RANK() OVER (ORDER BY ... DESC) and return first_name, last_name, total, and rank, ordered from highest to lowest. Limit to 3 rows.
In the CTE: JOIN orders on o.customer_id = c.id, JOIN order_items on oi.order_id = o.id, sum quantity * unit_price. In the outer query: RANK() OVER (ORDER BY total DESC) and LIMIT 3.
Solution available after 3 attempts
A page with all the essential SQL syntax, ready to keep handy while you write your queries.
The best way to consolidate is free practice. Open the Playground, load one of the datasets, and try to answer questions that come to mind: who earns above the average in the world?, how many employees per city?, which projects have at least two people assigned? Nothing is better to turn theory into instinct.