Total Sales Amount by Product Category (JOIN)
SQL coding challenge · Difficulty: medium · +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 |
- Return:
category,total_sales(ordered bytotal_salesDESC) - Approach:
JOINonproduct_id, thenSUM(amount)withGROUP BY category