SQL Fundamentals
Aggregation & GROUP BYMediumWrite Code
3/5
SUM + HAVING

Description

You're analysing sales data. Write a query that shows total revenue and order count per category for the current year, but only for categories with more than $1,000 in revenue. **Schema:**
sql
products(id, name, category, price)
order_items(id, order_id, product_id, quantity)
orders(id, placed_at, status)

Requirements

01JOIN order_items with products to get category and price
02JOIN with orders to filter by date
03Use SUM(price * quantity) for revenue and COUNT(DISTINCT order_id) for order count
04GROUP BY category
05Use HAVING to filter categories with revenue > 1000
06Order by revenue descending

Example

-- category    | total_revenue | order_count
-- Electronics | 45230.00      | 312
-- Clothing    | 12840.50      | 198
-- Books       | 3210.75       | 89
Python 3
1
2
3
4
5
6
7
8
9
10
11
12
Ln 12, Col 1