Find the Top 15 Customers by Lifetime Revenue
Rank customers by total order value and show name, email, order count and amount spent.
The challenge runs on the SQLabHub practice database (SQLite) and uses the users and orders tables. You can solve it in the browser, with nothing to install.
The task
Find the top 15 customers by total lifetime order value. Show their full name, email, number of orders, and total spent.
A correct answer returns 15 rows with 4 columns: full_name, email, order_count, lifetime_value.
Tables used in this challenge
users 150 rows
id INTEGER (PK) · first_name TEXT · last_name TEXT · email TEXT · phone TEXT · city TEXT · country TEXT · created_at TEXT · status TEXT · is_premium INTEGER
| id | first_name | last_name | phone | city | country | created_at | status | is_premium | |
|---|---|---|---|---|---|---|---|---|---|
orders 200 rows
id INTEGER (PK) · user_id INTEGER · total REAL · discount REAL · item_count INTEGER · status TEXT · order_date TEXT · shipped_date TEXT · shipping_city TEXT · shipping_country TEXT
| id | user_id | total | discount | item_count | status | order_date | shipped_date | shipping_city | shipping_country |
|---|---|---|---|---|---|---|---|---|---|
The first rows of each table are shown. See how all the tables relate on the database schema page.
Expected output
| full_name | order_count | lifetime_value | |
|---|---|---|---|
First 5 of 15 rows. This is the output of the model solution on the practice database.
What you practise
- Summarising many rows into one value with COUNT, SUM, AVG, MIN or MAX
- Grouping rows with GROUP BY to get one result row per group
- Keeping every row of the left table with LEFT JOIN, matched or not
- Building and cutting text values
- Sorting the result with ORDER BY
- Returning only the first rows with LIMIT
Hint and approach
Try the challenge first. Open these only when you need them.
Show the hint
JOIN users with orders, then GROUP BY user and use SUM(total)
Show the step-by-step approach
How the model solution is built, in the order the database evaluates it:
- Read from the
userstable. - LEFT JOIN
ordersonorders.user_id=users.id. Rows from the left side that have no match are kept, with NULL in the joined columns. - Group the rows by
users.id, so the result has one row for each value. - For each group, compute
COUNT(orders.id)andSUM(orders.total). - Build
full_nameby joining text values with the||operator. ROUND()trims the calculated figures to a fixed number of decimals.- Sort the result by
lifetime_valuedescending. - Keep only the first 15 rows with
LIMIT.
Model solution
Checking your session…
What to watch for
The query starts from users and LEFT JOINs orders, so customers who never ordered stay in the result with a count of 0 and a NULL total. COUNT(o.id) is what makes that count 0; COUNT(*) would report 1 for them.
- LEFT JOIN. A WHERE condition on the right-hand table removes the unmatched rows and turns the LEFT JOIN back into an inner join. Conditions on that table go in the ON clause.
- GROUP BY. Every selected column must either be in GROUP BY or sit inside an aggregate. SQLite accepts other columns and returns an arbitrary value for them; most databases raise an error.
- String functions. In SQLite and PostgreSQL, joining text with || returns NULL as soon as one of the parts is NULL.
Related SQL challenges
- Find the Top 5 Customers by Total SpendSimilar levelMedium
- Count Orders and Total Spend for Every User, Including ZeroSimilar levelMedium
- Concatenate First and Last Name for Premium UsersEasierEasy
- Segment Customers by Recency, Frequency and Monetary Value (RFM)HarderHard
- Find the City with the Highest Total RevenueHarderHard
Keep learning
- Lesson: LEFT JOIN
- Lesson: GROUP BY
- Lesson: String Functions
- Lesson: LIMIT / TOP / FETCH
- All SQL practice challenges, by difficulty and topic
- SQL interview questions with answers
- SQL Playground: run any query on the same database