Total Sales per Region using CTEs
SQL coding challenge · Difficulty: easy · Topic: CTEs · +50 XP
A retail company wants to analyze its sales performance across different regions. Given a 'sales' table, calculate the total sales amount for each region. Use a Common Table Expression (CTE) to simplify the query. Return region and total_sales, ordered by total_sales descending.
Example input
`
sales table:
sale_id | region | amount
1 | North | 1200.00
2 | South | 850.50
3 | East | 640.30
4 | West | 960.00
5 | North | 760.75
6 | South | 1120.00
7 | East | 950.50
`
Expected output
`
region | total_sales
South | 1970.50
North | 1960.75
East | 1590.80
West | 960.00
`
What this SQL challenge teaches you
“Total Sales per Region using CTEs” is a easy-level SQL challenge focused on CTEs. Working through it gives you hands-on practice with CTE, aggregations, group by — the kind of transformation you are asked to write in real data engineering work and in technical interviews. You can solve it directly in the browser: the dataset is pre-loaded, so you write the query or DataFrame code, run it, and compare your output against the expected result immediately.
Concepts covered
- CTE
- aggregations
- group by
How to approach it
If you get stuck, work through these steps in order before looking at a full solution — each one narrows the problem down:
- 1 ≤ number of rows ≤ 100,000
- Each region appears at least once
- amount values are valid non-negative decimals
- Return columns: region (TEXT), total_sales (DECIMAL), ordered by total_sales DESC
How to practise it on PySpark.in
Open the challenge, write your SQL query in the editor and press Run to execute it against the sample dataset. Submitting checks your output against every test case, including hidden ones, so you find out straight away whether your logic holds up. You can retry as often as you like, and each solved challenge adds to your XP.
Related SQL challenges
- Top 3 Products per Category
- Running Total Revenue
- Find Duplicate Emails
- Median Salary per Department
- Second Highest Salary
- Latest Order Per Customer
Frequently asked questions
Do I need to install Spark or a database to solve this?
No. The SQL environment runs in your browser with the sample data already loaded, so there is nothing to install or configure.
Is this challenge free?
Yes - the problem, the sample dataset, the hints and unlimited test runs are free.
What level is it?
It is rated easy and covers CTEs.