-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaggregation_functions.sql
More file actions
96 lines (70 loc) · 2.62 KB
/
aggregation_functions.sql
File metadata and controls
96 lines (70 loc) · 2.62 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
-- Count the number of customer records present in the database
SELECT COUNT(customer_id)
FROM customers;
-- Find the average price of our products?
SELECT AVG(price) AS Avg_Price
FROM products;
SELECT SUM(price) / COUNT(price) -- Here avg is sum of price divided by count of price
FROM products;
SELECT SUM(price) / COUNT(product_id)
FROM products;
-- Find the average price of our products only upto 2 decimal places?
SELECT ROUND(AVG(price),2) AS avg_price
FROM products;
-- Identify the regions where the number of customers is high?
SELECT city, COUNT(customer_id)
FROM customers
GROUP BY city;
-- adding new column status
ALTER TABLE orders
ADD COLUMN status VARCHAR(100);
SELECT * FROM orders;
-- Get the count of different Status
SELECT status, COUNT(order_id) AS number_of_orders
FROM orders
GROUP BY status;
SELECT COALESCE(status, "Not Specified") AS status, COUNT(order_id) AS number_of_orders
FROM orders
GROUP BY status;
-- Group By using 2 Columns
-- Write a SQL query to find the number of payments received for each payment method on
-- each date in order so as to analyze the trend over span of days
SELECT payment_date, payment_method, COUNT(payment_id) AS number_of_payments
FROM payment
GROUP BY payment_date, payment_method
ORDER BY payment_date, payment_method;
SELECT payment_date, payment_method, COUNT(payment_id) AS number_of_payments
FROM payment
GROUP BY payment_date, payment_method
ORDER BY payment_date ASC, payment_method DESC;
SELECT payment_date, payment_method, SUM(payment_amount) AS total_amount
FROM payment
GROUP BY payment_date, payment_method
ORDER BY total_amount DESC;
-- HAVING CLAUSE
-- Identify the customers who spend more than 5000 on a single order and if single customer
-- has multiple orders greater than 5000 show the order with the maximum amount.
SELECT customer_id, MAX(total_amount) AS max_amount
FROM orders
GROUP BY customer_id
HAVING max_amount > 5000;
SELECT customer_id, MAX(total_amount) AS max_amount
FROM orders
GROUP BY customer_id
HAVING max_amount > 5000
ORDER BY max_amount DESC;
-- Questions
-- Group orders by order date and calculate the total order amount for each date
SELECT order_date, SUM(total_amount) AS total_amount
FROM orders
GROUP BY order_date;
-- Find customers who have placed more than one order
SELECT customer_id, COUNT(order_id) AS total_orders
FROM orders
GROUP BY customer_id
HAVING total_orders > 1;
-- Identify products that have been ordered multiple times in the exact same quantity
-- across different orders and determine the frequency of these occurrences
SELECT product_id, quantity, COUNT(*) AS COUNT
FROM order_items
GROUP BY product_id, quantity;