Aggregate Function in SQL
SELECT * FROM
XXSD_FOOD_TL
FRUIT_NAME
|
UNIT_PRICE
|
QUANTITY
|
PURCHASE_DATE
|
Apple
|
100
|
60
|
01-Jan-20
|
Apple
|
105
|
45
|
02-Jan-20
|
Apple
|
101
|
11
|
06-Jan-20
|
Apple
|
115
|
12
|
07-Jan-20
|
Apple
|
105
|
12
|
09-Jan-20
|
Banana
|
70
|
20
|
03-Jan-20
|
Banana
|
60
|
23
|
04-Jan-20
|
Banana
|
65
|
25
|
08-Jan-20
|
kiwi
|
190
|
22
|
02-Jan-20
|
kiwi
|
200
|
19
|
06-Jan-20
|
Orange
|
110
|
40
|
01-Jan-20
|
Pumpkin
|
14
|
120
|
03-Jan-20
|
Pumpkin
|
11
|
12
|
05-Jan-20
|
Pumpkin
|
16
|
33
|
08-Jan-20
|
Tomato
|
13
|
15
|
04-Jan-20
|
Tomato
|
22
|
100
|
05-Jan-20
|
Tomato
|
11
|
21
|
07-Jan-20
|
Tomato
|
12
|
29
|
09-Jan-20
|
--====================================================
MIN in SQL
--====================================================
SELECT MIN(UNIT_PRICE)
FROM XXSD_FOOD_TL
FRUIT_NAME
|
UNIT_PRICE
|
QUANTITY
|
PURCHASE_DATE
|
Pumpkin
|
11
|
12
|
05-Jan-20
|
Tomato
|
11
|
21
|
07-Jan-20
|
--====================================================
MAX by in SQL
--====================================================
SELECT MAX(UNIT_PRICE)
FROM XXSD_FOOD_TL
FRUIT_NAME
|
UNIT_PRICE
|
QUANTITY
|
PURCHASE_DATE
|
kiwi
|
200
|
19
|
06-Jan-20
|
SELECT MIN(UNIT_PRICE), MAX(UNIT_PRICE)
FROM XXSD_FOOD_TL
--====================================================
SUM by in SQL
--====================================================
SELECT MIN(UNIT_PRICE), MAX(UNIT_PRICE) ,SUM(QUANTITY)
FROM XXSD_FOOD_TL
FRUIT_NAME
|
UNIT_PRICE
|
QUANTITY
|
PURCHASE_DATE
|
Apple
|
100
|
60
|
01-Jan-20
|
Apple
|
105
|
45
|
02-Jan-20
|
Apple
|
101
|
11
|
06-Jan-20
|
Apple
|
115
|
12
|
07-Jan-20
|
Apple
|
105
|
12
|
09-Jan-20
|
Banana
|
70
|
20
|
03-Jan-20
|
Banana
|
60
|
23
|
04-Jan-20
|
Banana
|
65
|
25
|
08-Jan-20
|
kiwi
|
190
|
22
|
02-Jan-20
|
kiwi
|
200
|
19
|
06-Jan-20
|
Orange
|
110
|
40
|
01-Jan-20
|
Pumpkin
|
14
|
120
|
03-Jan-20
|
Pumpkin
|
11
|
12
|
05-Jan-20
|
Pumpkin
|
16
|
33
|
08-Jan-20
|
Tomato
|
13
|
15
|
04-Jan-20
|
Tomato
|
22
|
100
|
05-Jan-20
|
Tomato
|
11
|
21
|
07-Jan-20
|
Tomato
|
12
|
29
|
09-Jan-20
|
619
|
--====================================================
AVG by in SQL
--====================================================
SELECT AVG(QUANTITY)
FROM XXSD_FOOD_TL
where FRUIT_NAME = 'Apple'
FRUIT_NAME
|
UNIT_PRICE
|
QUANTITY
|
PURCHASE_DATE
|
Apple
|
100
|
60
|
01-Jan-20
|
Apple
|
105
|
45
|
02-Jan-20
|
Apple
|
101
|
11
|
06-Jan-20
|
Apple
|
115
|
12
|
07-Jan-20
|
Apple
|
105
|
12
|
09-Jan-20
|
Total
|
140
|
||
Total no of items
|
5
|
||
Avg
|
28
|
--====================================================
COUNT by in SQL
--====================================================
SELECT COUNT(*)
FROM XXSD_FOOD_TL
where FRUIT_NAME = 'Apple'
SELECT
FRUIT_NAME, MIN(UNIT_PRICE), MAX(UNIT_PRICE) ,SUM(QUANTITY)
FROM XXSD_FOOD_TL
GROUP BY
FRUIT_NAME
order by 1
FRUIT_NAME
|
UNIT_PRICE
|
QUANTITY
|
PURCHASE_DATE
|
Apple
|
100
|
60
|
01-Jan-20
|
Apple
|
105
|
45
|
02-Jan-20
|
Apple
|
101
|
11
|
06-Jan-20
|
Apple
|
115
|
12
|
07-Jan-20
|
Apple
|
105
|
12
|
09-Jan-20
|
Apple
|
|
Min
|
100
|
Max
|
115
|
SUM
|
140
|
Comments
Post a Comment