Final SQL Project
Final SQL Project
YOUR FIRST PRIORITY IS TO GET 2-3 PLAYERS WITH HIGH S.R WHO HAVE FACED AT LEAST 500
BALLS.AND TO DO THAT YOU HAVE TO MAKE A LIST OF 10 PLAYERS YOU WANT TO BID IN THE
AUCTION SO THAT WHEN YOU TRY TO GRAB THEM IN AUCTION YOU SHOULD NOT PAY THE
AMOUNT GREATER THAN YOU HAVE IN THE PURSE FOR A PARTICULAR PLAYER.
Solution:
select batsman,sum(batsman_runs) as total_run, sum(ball) as
ball_face,
N Pooran
BCJ Cutting
SP Narine
MM Ali
AD Russell
38 39 40 41 42 43 44 45
TASK :2
Now you need to get 2-3 players with good Average who have played more the 2 ipl
seasons . And to do that you have to make a list of 10 players you want to bid in the
auction so that when you try to grab them in auction you should not pay the amount
greater than you have in the purse for a particular player.
• Solution:
KL Rahul 72 2647 62 42
JP Duminy 75 2029 49 41
ML Hayden 32 1107 27 41
KS Williamson 52 1619 41 39
SE Marsh 69 2477 64 38
TOP 10 GOOD AVERAGE BATSMAN
average
42 42
41 41 41 41
39 39
38 38
e rs h ul i ny y le e n
n er n s o n
se y
rsh
i l li Ra m a y d ar m
o s s a
V L D u H
G
H a W i m l i am H u
E
M
e K C L i l K S
d JP M DA PS W E
AB LM KS
M
TASK:3
Now you need to get 2-3 Hard-hitting players who have scored most runs in
boundaries and have played more the 2 ipl season. To do that you have to make a list
of 10 players you want to bid in the auction so that when you try to grab them in
auction you should not pay the amount greater than you have in the purse for a
particular player.
Solution:
SELECT batsman, SUM(batsman_runs) AS total_run,
sum(case when batsman_runs > 3 then batsman_runs else null end) as boundaries,
(sum(case when batsman_runs > 3 then batsman_runs else null end) * 100
/sum(batsman_runs)) as percentage
FROM ipl_ball
GROUP BY batsman
having count(distinct id) >28
order by percentage desc
LIMIT 10;
OUTPUT TASK:3
TOP 10 HARD HITTER BATSMAN
batsman total_run boundaries percentage
Solution:
select bowler ,
count(ball) as total_ball ,sum(total_runs) as total_run,
(sum(total_runs)/(count(ball)/6.0)) as economy
from
ipl_ball
group by bowler
having count(ball)>=500
order by economy
limit 10;
OUTPUT TASK:4
TOP 10 ECONOMY BOWLER PLAYER
bowler total_ball total_run economy
6.8
6.6
6.4
6.2
6
n
bl
e an yn in e ri ar ha a
Kh
a
ar te w ar
i n
tto d o t ati
d u m
il th S
A sh N V e Sun JB ew
shi A
K
ra D W R SP DL on R
T
Ra M
u
ng
t
M h i
as
W
task:5
Now you need to get 2-3 bowlers with the best strike rate and who have bowled at
least 500 balls in IPL so far.To do that you have to make a list of 10 players you want to bid in
the auction so that when you try to grab them in auction you should not pay the amount
greater than you have in the purse for a particular player.
• Solution:
select bowler ,
count(ball) as total_ball ,
sum(is_wicket) as wicket,
(count(ball)/sum(is_wicket)) as strike_rate
from
ipl_ball
group by bowler
having count(ball)>=500
order by strike_rate asc
limit 10;
OUTPUT TASK:5
TOP 10 BEST STRIKE RATE BOWLER
bowler total_ball wicket strike_rate
K Rabada 840 66 12
DE Bollinger 600 43 13
AJ Tye 645 45 14
MA Starc 612 39 15
MM Patel 1382 82 16
KK Cooper 600 36 16
Strike Rate Of Bowler
strike_rate wicket
KK Cooper 16
36
DJ Bravo 16
175
MM Patel 16
82
A Nehra 16
121
Imran Tahir 15
83
SL Malinga 15
188
MA Starc 15
39
AJ Tye 14
45
DE Bollinger 13
43
K Rabada 12
66
Task:6
Now you need to get 2-3 All_rounders with the best batting as well as bowling strike rate and who
have faced at least 500 balls in IPL so far and have bowled minimum 300 particular player.
select
a.bowler AS player, a.total_ball, a.strike_rate as bowler_strikerate, b.strike_rate as
batsman_strikerate
from
(select bowler , count(ball) as total_ball , (count(ball)/sum(is_wicket)) as strike_rate
from ipl_ball
group by bowler having count(ball)>=300 order by strike_rate asc
) as a
inner join
(select batsman, (sum(batsman_runs)*100/ sum(ball)) as strike_rate
from ipl_ball
group by batsman having count(ball)>=500 order by strike_rate desc
)as b
on
a.bowler=b.batsman
limit 10;
OUTPUT TASK:6
TOP 10 BEST ALL ROUNDER PLAYER
Player total_ball bowler_strikerate batsman_strikerate
AD Russell 1186 17 44
SP Narine 2824 19 43
HH Pandya 914 20 40
GJ Maxwell 558 27 40
CH Gayle 584 30 39
KA Pollard 1414 19 38
YK Pathan 1184 25 37
DR Smith 557 20 37
BA Stokes 711 22 37
RG Sharma 342 21 36
All Rounder Player
bowler_strikerate batsman_strikerate
40 39 37
40 38 37 37 36
44 43
27 30 25
20 19 20 22 21
17 19
ell e ya el
l le rd n ith es a
s ir n d a y lla h a k rm
us
Na an a xw G o a t Sm St
o a
R P CH
P P
DR Sh
AD SP HH J M
KA YK BA RG
G
Try to define a criteria best suited for a
wicketkeeper required in a IPL T20
Solution:
select city,count(city) as count_of_city from ipl_matches group
by city
order by city;
120
100
80
60
101
40 74 77
65 56 57 64
20 29 47 38
26
12 15 12 9 15 9 8 4 3 5 13 6 10 7 12 13
0 2 7 7 3 3 7
i re i i i re ta ur hi
h ab lo tein r io
n
n na elh
u ba d on o u rg r ley k a NA beth ip n c n am
ga on D d b be ol a
u
D n f n tu
C he D L on In n es m K liz
a R Ra
pa
t
Ab Ba oe
m Ce as
t an Ki rt
E
kh
a
l E h o a
B Jo P Vi
s
Additional: 2
Create table deliveries_v02 with all the columns of the table ‘deliveries’
and an additional column ball_result containing values boundary, dot or
other depending on the total_run (boundary for >= 4, dot for 0 and other
for any other number)
Solution:
create table deliveries_v02 as
select id,inning,over,ball,batsman,non_striker,bowler,batsman_runs,
extra_runs,total_runs,is_wicket,dismissal_kind,player_dismissed,fielder,
extras_type,batting_team,bowling_team,
case
when total_runs =1 then 'single'
when total_runs =2 then 'double'
when total_runs =3 then 'three'
when total_runs >=4 then 'boundary'
else 'dot boll'
end as ball_result
from ipl_ball
Additional: 3
Write a query to fetch the total number of boundaries and dot balls from
the deliveries_v02 table.
Solution:
select ball_result,count(ball_result) from deliveries_v02
group by ball_result
having ball_result='boundary' or ball_result= 'dot boll‘
Output:
“boundary" 31468
"dot boll" 67841
Additional:4
Write a query to fetch the total number of boundaries scored by each
team from the deliveries_v02 table and order it in descending order of
the number of boundaries scored.
Solution:
select batting_team,count(ball_result)as total_boundaries
from deliveries_v02
group by batting_team,ball_result
having ball_result='boundary'
order by count(ball_result) desc
Output:4
Boundaries scored by each team
batting_team total_boundaries
Mumbai Indians 4118
Royal Challengers Bangalore 3800
Kings XI Punjab 3780
Kolkata Knight Riders 3739
Chennai Super Kings 3496
Rajasthan Royals 3041
Delhi Daredevils 3022
Sunrisers Hyderabad 2306
Deccan Chargers 1387
Pune Warriors 733
Delhi Capitals 659
Gujarat Lions 624
Rising Pune Supergiant 290
Rising Pune Supergiants 242
Kochi Tuskers Kerala 231
Additional: 5
Write a query to fetch the total number of dot balls bowled by each
team and order it in descending order of the total number of dot balls
bowled.
Solution:
select bowling_team,count(ball_result)as total_dot_ball from
deliveries_v02
group by bowling_team,ball_result
having ball_result='dot boll'
order by count(ball_result) desc;
Output:5
Dot balls bowled by each team
bowling_team total_dot_ball
Mumbai Indians 8714
Royal Challengers Bangalore 7955
Kolkata Knight Riders 7894
Kings XI Punjab 7679
Chennai Super Kings 7593
Rajasthan Royals 6665
Delhi Daredevils 6520
Sunrisers Hyderabad 5248
Deccan Chargers 3306
Pune Warriors 1900
Delhi Capitals 1338
Gujarat Lions 1095
Rising Pune Supergiant 698
Kochi Tuskers Kerala 626
Rising Pune Supergiants 539
Na 71
Dot ball and boundaries by each team
14000
12000
10000
8000
6000
total_boundaries
Axis Title 4000 total_dot_ball
2000
0
ns rs s s rs tals t ts
i a e ng vi l e i an n
n d R
d
i r Ki d e arg api rg rgia
iI e pe pe
a g ht upe Dar n Ch hi C u
m
b
Kni i S
l h i
cc a
D e l e
S
e
Su
u a e n
M kata enn D D e Pu
P u n
o l h n g g
C i
K
Ri
s i sin
R
Additional:6
Write a query to fetch the total number of dismissals by dismissal kinds
where dismissal kind is not NA
Solution:
select dismissal_kind,count (dismissal_kind) from deliveries
where dismissal_kind!='NA'
group by dismissal_kind;
bowled 1700
caught 5743
caught and bowled 269
hit wicket 12
lbw 571
obstructing the field 2
retired hurt 11
run out 893
stumped 294
total number of dismissals by dismissal kinds
stumped
3%
retired hurt
0%
obstructing run out bowled
thewicket
hit field 9% 18%
0%
0% lbw
6%
caught and
bowled
3%
caught
60%
Additional: 7
Write a query to get the top 5 bowlers who conceded maximum extra
runs from the deliveries_v02 table
Solution:
select bowler,
sum(extra_runs) as sum_of_extra_run from deliveries_v02
group by bowler
order by sum(extra_runs) desc
limit 5;
Output:7
Top 5 bowlers who conceded maximum extra runs
sum_of_extra_run
sum_of_extra_ru sum_of_extra_run
bowler n
293
SL Malinga 293
236 226
210 201
P Kumar 236
UT Yadav 226
DJ Bravo 210
Solution :
create table deliveries_v03 as (
select
a.*, b.venue,b.date as match_date
from
ipl_ball as a
left join
ipl_matches as b
on
a.id=b.id);
Additional :9
Write a query to fetch the total runs scored for each venue and order it
in the descending order of total runs scored.
Solution:
select venue,sum(total_runs) as venue_total_run
from deliveries_v03
group by venue
order by sum(total_runs) desc;
Addtional:10
Write a query to fetch the year-wise total runs scored at Eden
Gardens and order it in the descending order of total runs scored.
Solution:
select venue,sum(total_runs) as total_run,
(extract (year from match_date))as year from deliveries_v03
where venue= 'Eden Gardens'
group by venue,(extract (year from match_date))
order by sum(total_runs) desc
Output:10
year-wise total runs scored at Eden Gardens
venue total_run year
Eden Gardens 2885 2018
3500
3000
2500
2000
1500
1000
500
0
2018 2019 2015 2013 2017 2010 2016 2012 2011 2008 2014