Your question is Top Products SQL by Category. Start with the requirements and the three tables on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
HCLTech's retail analytics team needs a quarterly product leaderboard for executive reporting. Write a PostgreSQL query that evaluates product sales during the previous completed calendar quarter relative to the current date.
Calculate total units sold and total revenue for each product, associate every product with its category, and assign a position within that category. Revenue is calculated as quantity * unit_price for each order item.
category_name, product_name, total_units_sold, total_revenue, and revenue_rank.| Column | Type | Description |
|---|---|---|
| category_idPK | INT | Primary key for the product category |
| category_name | VARCHAR(100) | Display name of the category |
| Column | Type | Description |
|---|---|---|
| product_idPK | INT | Primary key for the product |
| category_id | INT | References categories.category_id |
| product_name | VARCHAR(150) | Display name of the product |
| Column | Type | Description |
|---|---|---|
| order_item_idPK | INT | Primary key for the order line |
| product_id | INT | References products.product_id |
| order_date | DATE | Date on which the item was sold |
| quantity | INT | Number of units sold |
| unit_price | NUMERIC(10,2) | Sale price per unit |