-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay_13 Interview Preparation Queries.sql
213 lines (187 loc) · 5.04 KB
/
Day_13 Interview Preparation Queries.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
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
create database mocktest ;
use mocktest ;
# 1. find the number of new and repeat customers on each Order Date
create table customer_orders (
order_id integer,
customer_id integer,
order_date date,
order_amount integer
);
select * from customer_orders ;
insert into customer_orders values
(1,100,cast('2022-01-01' as date),2000),
(2,200,cast('2022-01-01' as date),2500),
(3,300,cast('2022-01-01' as date),2100),
(4,100,cast('2022-01-02' as date),2000),
(5,400,cast('2022-01-02' as date),2200),
(6,500,cast('2022-01-02' as date),2700),
(7,100,cast('2022-01-03' as date),3000),
(8,400,cast('2022-01-03' as date),1000),
(9,600,cast('2022-01-03' as date),3000);
-- Solution --
with orders as
(
Select
customer_Id,
order_date,
min(order_date) over (partition by customer_id) as first_order_date
from customer_orders
)
Select
Order_date,
sum(case when order_Date = first_order_Date then 1 else 0 end) as Newcustomers,
sum(case when order_Date != first_order_Date then 1 else 0 end) as Repeatcustomers
from orders
Group by Order_date ;
# 2. Display the name, floor, most visited floor and resoruces used
create table entries (
name varchar(20),
address varchar(20),
email varchar(20),
floor int,
resources varchar(10)) ;
insert into entries values
('A','Bangalore','[email protected]',1,'CPU'),
('A','Bangalore','[email protected]',1,'CPU'),
('A','Bangalore','[email protected]',2,'DESKTOP'),
('B','Bangalore','[email protected]',2,'DESKTOP'),
('B','Bangalore','[email protected]',2,'DESKTOP'),
('B','Bangalore','[email protected]',1,'MONITOR');
-- Solution --
with mv as
(
Select
name,
floor,
count(*) as Number_of_Visits,
Rank() over (Partition by name Order by Count(*) desc) as rk
from entries
Group by name,floor
)
Select *
from mv
where rk = 1 ;
# Q3. Display the list of Bowlers(Top5) who conceeded more extras in IPL
Select
p.player_name Bowler,
Sum(extra_runs) as Total_Extras
from ipl.extra_runs e
Inner join ipl.ball_by_ball b on
e.Match_Id = b.Match_Id and e.Over_Id = b.Over_Id and
e.Ball_Id = b.Ball_Id and e.Innings_No = b.Innings_No
Inner join ipl.player p on b.Bowler = p.Player_Id
Group by e.Match_Id
Order by Total_Extras desc
Limit 5 ;
# Q4. Display the list of most expensive overs in IPL Match Date, Teams Played, Bowler Name and TotalRuns should be in the Output
Select
m.Match_date Date,
b.Over_Id OverId,
b.Innings_No Innings,
t.Team_Name as TeamA,
t2.Team_Name TeamB,
p.Player_Name Bowler,
Sum(Runs_Scored) Runs,
group_concat(distinct p2.player_name) Players
From ipl.ball_by_ball b
Inner join ipl.batsman_scored bs on
b.Match_Id = bs.Match_Id && b.Over_Id = bs.Over_Id &&
b.Ball_Id = bs.Ball_Id && b.Innings_No = bs.Innings_No
Inner Join ipl.player p on b.Bowler = p.Player_Id
Inner join ipl.player p2 on b.Striker = p2.Player_Id
Inner join ipl.match m on b.Match_Id = m.Match_Id
Inner join ipl.team t on m.Team_1 = t.Team_Id
Inner join ipl.team t2 on m.Team_2 = t2.Team_Id
Group by
Date, OverId, Innings, TeamA, TeamB, Bowler
Order by Runs desc ;
# Q5. Display the list of films which released multiple times with same title Output should be in Format
-- Title OldReleaseDate NewReleaseDate
with earlyfilms as
(
Select
Title,
ReleaseDate
from movies.film
Where Year(ReleaseDate) < 2000
),
recentfilms as
(
Select
Title,
ReleaseDate
from movies.film
Where Year(ReleaseDate) >=2000
)
Select
e.Title,
e.ReleaseDate as EarlyDate,
r.ReleaseDate as RecentDate
from earlyfilms e join recentfilms r on e.Title = r.Title ;
# Q6. Use BookStore Database,Display BookName,Prequel BookName and Sequel BookName
Create table tblbook
(BookId int,
BookName varchar(100),
SequelBookId int) ;
Insert into tblbook Values
(1,'The Fellowship of the Ring',4),
(2,'The Girl with the Draggon Tattoo',5),
(3,'The Hobbit',1),
(4,'The Two Towers',6),
(5,'The Girl who Played with Fire',10),
(7,'Harry Potter''s and the Philosopher''s Stone',8) ;
Insert into tblbook(BookId,BookName) Values
(9,'The Life of Pi'),
(8,'Harry Potter and the Prisoner of Azkabhan'),
(6,'The Return of the King'),
(10,'The Girl who Kicked the Hornet''s Nest') ;
-- Solution --
Select
b.BookName as Book,
s.Bookname as Sequel,
p.Bookname as Prequel
from tblbook b left join tblbook s on b.sequelbookid = s.bookid
left join tblbook p on b.bookid = p.sequelbookid ;
# Q7. Display the list of Products which were unsold on any given date Output should contain
-- SalesDate ProductName TotalQty TotalSales
CREATE TABLE Product
(ID int,
ProductName varchar(100),
Cost Double) ;
CREATE TABLE SalesItem
(ID int,
SalesDate datetime,
ProductID int,
Qty int,
TotalSalesAmt double) ;
INSERT INTO Product VALUES
(1,'Widget',21.99),
(2,'Thingamajig',5.38),
(3,'Watchamacallit',1.96) ;
INSERT INTO SalesItem VALUES
(1,'2014-10-1',1,1,21.99),
(2,'2014-10-2',3,1,1.96),
(3,'2014-10-3',3,10,19.60),
(4,'2014-10-3',1,2,43.98),
(5,'2014-10-3',1,2,43.98) ;
-- Solution --
With sd as
(
Select distinct salesdate
from SalesItem
),
cs as
(
Select
Salesdate,
ProductName
from sd cross join Product
)
Select
cs.SalesDate,
cs.ProductName,
Sum(Qty) as TotalQty,
Sum(TotalSalesAmt) as TotalSales
from cs left join salesitem s on s.SalesDate = cs.SalesDate
Group by cs.SalesDate,
cs.ProductName ;