-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day 6 (Group by, Order by & Having Clause).sql
108 lines (76 loc) · 2.5 KB
/
Day 6 (Group by, Order by & Having Clause).sql
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
96
97
98
99
100
101
102
103
104
105
106
107
use product;
select * from products;
insert into products (product_id, product_name, mrp, price, ownername)
values
('7','desktop',900,500,'Anurag'),
('8','desktop',900,500,'Supriya'),
('9','tablet',400,300,'Sushma');
# distinct command
select distinct ownername from products;
select distinct product_name from products;
select distinct ownername, product_name from products;
# Grouping Functions
### aggregrate functions
select min(price) from products;
select product_name, min(price) from products
group by product_name;
select product_name, max(price) from products
group by product_name;
select sum(price) from products;
select product_name, sum(price) from products
group by product_name;
select product_name, sum(mrp), sum(price) from products
group by product_name;
select product_name, count(product_name) from products
group by product_name;
select product_name, ownername, count(product_name) from products
group by product_name, ownername;
select count(*) from products;
select product_name, count(*) from products
group by product_name;
select product_name, count(product_name) from products
group by product_name;
select product_name, avg(price) from products
group by product_name;
select product_name, avg(mrp), avg(price) from products
group by product_name;
# Order by
select product_name, price from products
order by price;
select product_name, sum(mrp) as totalprice from products
group by product_name
order by totalprice;
select product_name, sum(mrp) as totalprice from products
group by product_name
order by totalprice asc;
select product_name, sum(mrp) as totalprice from products
group by product_name
order by totalprice desc;
select product_name, sum(mrp) as totalprice from products
group by product_name
having totalprice <1000 ;
-- Having Clause
select product_name, sum(mrp) as totalprice from products
group by product_name
having totalprice > 1000 ;
# Like Statement
---- % represents 0 ,1 or Multiple characters
select * from products
where ownername like 'a%';
select * from products
where ownername like '%a';
select * from products
where ownername like 'su%';
select * from products
where ownername like '%a%';
---- underscore (_) respresents one, single character
select * from products
where ownername like '_u%';
# 1. Find any value that start with a end with o having any thing in between
# 2. find any value that start with a and are atleast 3 characters in length
# 1.
select * from products
where ownername like 'a%o';
# 2.
select * from products
where ownername like 'a__%';