Total Sales Amount by Product Category (JOIN)

SQL coding challenge · Difficulty: medium · Topic: Joins · +75 XP

Problem

Join sales with product on product_id and find the total sales amount for each product category, highest first.

Tables

Table: product

| product_id | category |
| --- | --- |
| 11 | Electronics |
| 12 | Clothing |
| 13 | Accessories |

Table: sales

| order_id | customer_id | product_id | amount | order_date |
| --- | --- | --- | --- | --- |
| 1 | 1 | 11 | 3000 | 2024-01-01 |
| 2 | 2 | 11 | 2230 | 2024-01-02 |
| 3 | 1 | 12 | 2000 | 2024-01-03 |
| 4 | 3 | 12 | 1120 | 2024-01-04 |
| 5 | 2 | 13 | 980 | 2024-01-05 |

Expected Output

| category | total_sales |
| --- | --- |
| Electronics | 5230 |
| Clothing | 3120 |
| Accessories | 980 |

What this SQL challenge teaches you

“Total Sales Amount by Product Category (JOIN)” is a medium-level SQL challenge focused on Joins. Working through it gives you hands-on practice with JOIN, GROUP BY, SUM — 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

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. The category lives in product and the amount lives in sales, so join them before you aggregate.
  2. JOIN product ON sales.product_id = product.product_id, then GROUP BY category and SUM(amount).

Where this comes up

Variations of this problem have been reported in interviews at Amazon, Flipkart, Netflix. Interviewers use it to check whether you can express the logic cleanly and reason about correctness on edge cases such as ties, nulls and empty groups.

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

Helpful resources

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 medium and covers Joins.

Solve this challenge free on PySpark.in