Repository navigation
Expand file tree
/
Copy pathSQL_Class_3_Commands.sql
More file actions
252 lines (194 loc) · 6.78 KB
/
Copy pathSQL_Class_3_Commands.sql
File metadata and controls
252 lines (194 loc) · 6.78 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# How to use IS NULL or IS NOT NULL in the where clause
insert into employee values(10,'Kapil', null, '2021-08-10', 10000, 'Assam');
insert into employee values(11,'Nikhil', 30, '2021-08-10', null, 'Assam');
select * from employee;
# get all those employees whos age value is null
select * from employee where age is null;
select * from employee;
# get all those employees whos salary value is not null
select * from employee where salary is not null;
# Table and Data for Group By
create table orders_data
(
cust_id int,
order_id int,
country varchar(50),
state varchar(50)
);
insert into orders_data values(1,100,'USA','Seattle');
insert into orders_data values(2,101,'INDIA','UP');
insert into orders_data values(2,103,'INDIA','Bihar');
insert into orders_data values(4,108,'USA','WDC');
insert into orders_data values(5,109,'UK','London');
insert into orders_data values(4,110,'USA','WDC');
insert into orders_data values(3,120,'INDIA','AP');
insert into orders_data values(2,121,'INDIA','Goa');
insert into orders_data values(1,131,'USA','Seattle');
insert into orders_data values(6,142,'USA','Seattle');
insert into orders_data values(7,150,'USA','Seattle');
select * from orders_data;
# calculate total order placed country wise
select country, count(*) as order_count_by_each_country from orders_data group by country;
# Write a query to find the total salary by each age group
select * from employee;
select age, sum(salary) as total_salary_by_each_age_group from employee group by age;
# calculate different aggregated metrices for salary
select age,
sum(salary) as total_salary_by_each_age_group,
max(salary) as max_salary_by_each_age_group,
min(salary) as min_salary_by_each_age_group,
avg(salary) as avg_salary_by_each_age_group,
count(*) as total_employees_by_each_age_group
from employee group by age;
# Group by on multiple columns
select
state,
count(*) as state_wise_order
from orders_data
group by country, state;
select
country,
state,
count(*) as state_wise_order
from orders_data
group by country, state;
# Use of Having Clause
# Write a query to find the country where only 1 order was placed
select country from orders_data group by country having count(*)=1;
# Where Clause and Group By Clause --> What should be the proper sequence??
# Answer -> Where Clause and then Group By
# How to use GROUP_CONCAT
# Query - Write a query to print distinct states present in the dataset for each country?
select country, GROUP_CONCAT(state) as states_in_country from orders_data group by country;
select country, GROUP_CONCAT(distinct state) as states_in_country from orders_data group by country;
select country, GROUP_CONCAT(distinct state order by state desc) as states_in_country from orders_data group by country;
select country, GROUP_CONCAT(distinct state order by state desc separator '<->') as states_in_country from orders_data group by country;
# Subqueries in SQL
create table employees
(
id int,
name varchar(50),
salary int
);
insert into employees values(1,'Shashank',5000),(2,'Amit',5500),(3,'Rahul',7000),(4,'Rohit',6000),(5,'Nitin',4000),(6,'Sunny',7500);
select * from employees;
# Write a query to print all those employee records who are getting more salary than 'Rohit'
# Wrong solution -> select * from employees where salary > 6000;
select * from employees where salary > (select salary from employees where name='Rohit');
# Use of IN and NOT IN
# Write a query to print all orders which were placed in 'Seattle' or 'Goa'
select * from orders_data;
SELECT * FROM orders_data WHERE state in ('Seattle', 'Goa');
create table customer_order_data
(
order_id int,
cust_id int,
supplier_id int,
cust_country varchar(50)
);
insert into customer_order_data values(101,200,300,'USA'),(102,201,301,'INDIA'),(103,202,302,'USA'),(104,203,303,'UK');
create table supplier_data
(
supplier_id int,
sup_country varchar(50)
);
insert into supplier_data values(300,'USA'),(303,'UK');
# write a query to find all customer order data where all coustomers are from same countries
# as the suppliers
select * from customer_order_data where cust_country in
(select distinct sup_country from supplier_data);
# Another example of Sub-Query
select *
from (select
country,
count(*) as country_wise_order
from orders_data
group by country) result
where country_wise_order=1;
# Uber SQL Interview questions
create table tree
(
node int,
parent int
);
insert into tree values (5,8),(9,8),(4,5),(2,9),(1,5),(3,9),(8,null);
select * from tree;
select node,
CASE
when node not in (select distinct parent from tree where parent is not null) then 'LEAF'
when parent is null then 'ROOT'
else 'INNER'
END as node_type
from tree;
# Examples for join
create table orders
(
order_id int,
cust_id int,
order_dat date,
shipper_id int
);
create table customers
(
cust_id int,
cust_name varchar(50),
country varchar(50)
);
create table shippers
(
ship_id int,
shipper_name varchar(50)
);
insert into orders values(10308, 2, '2022-09-15', 3);
insert into orders values(10309, 30, '2022-09-16', 1);
insert into orders values(10310, 41, '2022-09-19', 2);
insert into customers values(1, 'Neel', 'India');
insert into customers values(2, 'Nitin', 'USA');
insert into customers values(3, 'Mukesh', 'UK');
insert into shippers values(3,'abc');
insert into shippers values(1,'xyz');
select * from orders;
select * from customers;
select * from shippers;
# perform inner JOIN
# get the customer informations for each order order, if value of customer is present in orders TABLE
select
o.*, c.*
from orders o
inner join customers c on o.cust_id = c.cust_id;
# Left Join
select
o.*, c.*
from orders o
left join customers c on o.cust_id = c.cust_id;
# Right Join
select
o.*, c.*
from orders o
right join customers c on o.cust_id = c.cust_id;
# How to join more than 2 datasets?
# perform inner JOIN
# get the customer informations for each order order, if value of customer is present in orders TABLE
# also get the information of shipper name
select
o.*, c.*, s.*
from orders o
inner join customers c on o.cust_id = c.cust_id
inner join shippers s on o.shipper_id = s.ship_id;
create table employees_full_data
(
emp_id int,
name varchar(50),
mgr_id int
);
insert into employees_full_data values(1, 'Shashank', 3);
insert into employees_full_data values(2, 'Amit', 3);
insert into employees_full_data values(3, 'Rajesh', 4);
insert into employees_full_data values(4, 'Ankit', 6);
insert into employees_full_data values(6, 'Nikhil', null);
select * from employees_full_data;
# Write a query to print the distinct names of managers??
select
emp.name as manager
from employees_full_data emp
inner join (select distinct mgr_id as mgr_id from employees_full_data) mgr on mgr.mgr_id = emp.emp_id;