-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQL_queries.sql
More file actions
57 lines (53 loc) · 1.86 KB
/
SQL_queries.sql
File metadata and controls
57 lines (53 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
-- 1. Total spend by each customer
SELECT c.customer_id, c.first_name, c.last_name,
SUM(p.price * o.quantity) AS total_spent
FROM Orders o
JOIN Customers c ON o.customer_id = c.customer_id
JOIN Products p ON o.product_id = p.product_id
GROUP BY c.customer_id, c.first_name, c.last_name
ORDER BY total_spent DESC;
-- 2. Ranking customers by spend
WITH customer_sales AS (
SELECT c.customer_id, c.first_name, c.last_name,
SUM(p.price * o.quantity) AS total_spent
FROM Orders o
JOIN Customers c ON o.customer_id = c.customer_id
JOIN Products p ON o.product_id = p.product_id
GROUP BY c.customer_id, c.first_name, c.last_name
)
SELECT *,
RANK() OVER (ORDER BY total_spent DESC) AS spend_rank
FROM customer_sales;
-- 3. Running total of sales over time
SELECT o.order_date,
SUM(p.price * o.quantity) AS daily_sales,
SUM(SUM(p.price * o.quantity))
OVER (ORDER BY o.order_date) AS running_total
FROM Orders o
JOIN Products p ON o.product_id = p.product_id
GROUP BY o.order_date
ORDER BY o.order_date;
-- 4. Customers who bought more than 1 category
WITH categories AS (
SELECT o.customer_id, COUNT(DISTINCT p.category) AS category_count
FROM Orders o
JOIN Products p ON o.product_id = p.product_id
GROUP BY o.customer_id
)
SELECT c.first_name, c.last_name, category_count
FROM categories cat
JOIN Customers c ON cat.customer_id = c.customer_id
WHERE category_count > 1;
-- 5. Most popular product per city
WITH product_city AS (
SELECT c.city, p.product_name,
SUM(o.quantity) AS total_qty,
RANK() OVER (PARTITION BY c.city ORDER BY SUM(o.quantity) DESC) AS rnk
FROM Orders o
JOIN Customers c ON o.customer_id = c.customer_id
JOIN Products p ON o.product_id = p.product_id
GROUP BY c.city, p.product_name
)
SELECT city, product_name, total_qty
FROM product_city
WHERE rnk = 1;