-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQL queries.sql
More file actions
32 lines (26 loc) · 915 Bytes
/
Copy pathSQL queries.sql
File metadata and controls
32 lines (26 loc) · 915 Bytes
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
CREATE DATABASE cust_product;
USE cust_product;
-- Total sales by region
SELECT region, ROUND(SUM(quantity * price_per_unit),2) AS total_sales
FROM purchases
GROUP BY region;
-- Total sales by product
SELECT product_id, Round(SUM(quantity * price_per_unit), 2) AS total_sales
FROM purchases
GROUP BY product_id
ORDER BY total_sales DESC
LIMIT 5;
-- Total sales by month
SELECT EXTRACT(MONTH FROM purchase_date) AS Month_, ROUND(SUM(quantity * price_per_unit),2) AS total_sales
FROM purchases
GROUP BY Month_
ORDER BY Month_;
-- Total amount spend by each customer
SELECT customer_id, customer_name, ROUND(SUM(quantity * price_per_unit),2) AS total_amount_spent
FROM purchases
GROUP BY customer_id, customer_name;
-- Total sales by region and product
SELECT product_id, region, SUM(quantity * price_per_unit) AS total_sales
FROM purchases
GROUP BY product_id, region
ORDER BY product_id ASC, total_sales DESC;