SQL · Level 2 Project

Project: SQL Customer Leaderboard

Rank the top 3 customers by paid revenue: join, filter, group, keep only regular customers with HAVING, then sort and limit.

  • JOIN, WHERE, GROUP BY, HAVING
  • ORDER BY then LIMIT
  • Filter before you aggregate

Real reports chain every clause in a fixed order: JOIN, WHERE, GROUP BY, HAVING, ORDER BY, LIMIT. Trap: WHERE runs before the grouping, so a refund filtered in HAVING would already be inside the totals. Filter rows in WHERE, groups in HAVING.

SELECT c.country, SUM(o.total) AS revenue
FROM orders AS o
JOIN customers AS c ON c.id = o.customer_id
WHERE o.status = 'paid'
GROUP BY c.country
HAVING COUNT(*) >= 2
ORDER BY revenue DESC
LIMIT 3;

Return the top 3 customers by paid revenue: their name, number of paid orders and paid revenue, only for customers with at least 2 paid orders, highest revenue first. In the database below, Bo has the biggest single order but only one.

Sample data from the first test. The other tests use different rows, so write a query, not a list of answers.

customers 5 rows
idINTEGERnameTEXT
1Ana
2Bo
3Cy
4Di
5Eve
orders 10 rows
idINTEGERcustomer_idINTEGERstatusTEXTtotalREAL
11paid50.0
21paid70.0
32paid300.0
43paid40.0
53paid45.0
63refunded500.0
74paid60.0
84paid80.0
95paid10.0
105paid15.0
solution.sql
Test Suites Pending
Output