-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6-SQL-Aggregation_Function
More file actions
95 lines (72 loc) · 2.98 KB
/
6-SQL-Aggregation_Function
File metadata and controls
95 lines (72 loc) · 2.98 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
Aggregation Functions in SQL
Aggregation functions are used to summarize data from multiple rows into a single value.
They are very important for:
📊 Data analysis
📈 Dashboards
📉 Business reporting
💼 Data analyst interviews
1️⃣ COUNT() : Counts Rows
The count() function counts the number of rows that match a specified condition.
2️⃣ SUM() : Adds Values
The sum() function calculates the total sum of a numeric column.
3️⃣ AVG() : Average Value
The avg() function calculates the average value of a numeric column.
4️⃣ MIN() : Smallest Value
The min() function returns the smallest value in a specified column.
5️⃣ MAX() : Largest Value
The max() function returns the largest value in a specified column.
NOTE: ROUND() function is used to round a numeric value to a specified number of decimal places. It is not an aggreaction fuction
but is often used with aggregation functions to format the output.
EXAMPLE:
USE market_star_schema;
SELECT
COUNT(market_fact_id) AS no_of_sales,
SUM(sales) AS total_sales,
AVG(sales) AS avg_sales,
MAX(sales) AS max_sales,
MIN(sales) AS min_sales
FROM market_fact_full;
-- how many null values are present in the manu_id column
SELECT COUNT(*) - COUNT(manu_id) -- this line counts the total number of rows & subtracts the count of non-null manu_id values.
FROM prod_dimen;
-- no of customers from city of Cochin
SELECT COUNT(cust_id) FROM cust_dimen
WHERE city='Cochin';
-- no of orders are placed in year of 2010
SELECT COUNT(ord_id) AS no_of_orders FROM orders_dimen
WHERE order_date BETWEEN '2010-01-01' AND '2010-12-31';
-- no of orders are placed in year of March 2010 with critical as order priority
SELECT COUNT(ord_id) AS no_of_orders FROM orders_dimen
WHERE (order_date BETWEEN '2010-03-01' AND '2010-03-31')
AND order_priority='Critical';
-- no of customers either from city of Chennai or Hyderabad
SELECT COUNT(cust_id) AS no_of_customers FROM cust_dimen
WHERE city IN ('Chennai','Hyderabad');
-- find the city with highest no of customers
SELECT city, COUNT(*) AS no_of_cust
FROM cust_dimen
GROUP BY city
ORDER BY no_of_cust DESC
LIMIT 1;
-- find third highest customer segment in cities of Chennai or Delhi with more no of customers
SELECT customer_segment, COUNT(*) AS no_of_customers
FROM cust_dimen
WHERE city IN ('Chennai','Delhi')
GROUP BY customer_segment
ORDER BY no_of_customers DESC
LIMIT 1 OFFSET 2;
-- find the sum of sales
SELECT ROUND(SUM(sales),2) AS total_sales
FROM market_fact_full;
-- find the maximum profit value among the orders which has sales value more than 1000
SELECT ROUND(MAX(profit),2) AS max_profit
FROM market_fact_full
WHERE sales>1000;
-- find the minimum sales value among the orders which has profit value more than 500
SELECT ROUND(MIN(sales),2) AS min_sales
FROM market_fact_full
WHERE profit>500;
-- find the average profit value among the orders which has sales value more than 500 and profit value less thn 1000
SELECT ROUND(AVG(profit),2) AS avg_profit
FROM market_fact_full
WHERE sales>500 AND profit<1000;