2. CREATED IPL PLAYER DATAAS ipl_player_data FROM IPL_BALL.CSV FILE
CREATE TABLE ipl_player_data
(id int,inning int,over int,ball int,batsman char(50),non_striker char(50),bowler char(50),batsman_runs int,
extra_runs int,total_runs int,is_wicket int,dismissal_kind char(50),player_dismissed char(50),fielder char(50),
extras_type char(50),batting_team varchar(50),bowling_team varchar(50) );
--COPYING CSV FILE QUERY :
COPY ipl_player_data FROM 'C:Sudhanshu WaliainternshalaSQLProjectIPL DatasetIPL_Ball.csv'
DELIMITER ',' CSV HEADER;
--QUERY TO RETRIEVE DATA :
SELECT * FROM ipl_player_data;
3. CREATED IPL MATCH DATAAS ipl_match_data FROM IPL_MATCHES.CSV FILE
CREATE TABLE ipl_match_data
(id int,city char(50),match_date varchar(50),player_of_match char(50),venue varchar(255),neutral_venue varchar(255),
team1 varchar(100),team2 varchar(100),toss_winner varchar(100),toss_decision char(50),winner varchar(100),
result varchar(50),result_margin int,eliminator char(10),method varchar(50),umpire1 char(50),umpire2 char(50)
);
--COPYING CSV FILE QUERY :
COPY ipl_match_data FROM 'C:Sudhanshu WaliainternshalaSQLProjectIPL DatasetIPL_matches.csv' DELIMITER ','
CSV HEADER;
--ALTER THE DATATYPE OF match_date COLUMN TO DATE DATATYPE
ALTER TABLE ipl_match_data ALTER COLUMN match_date TYPE DATE USING to_date(match_date,'DD-MM-
YYYY’);
--QUERY TO RETRIEVE DATA :
select * from ipl_match_data;
4. Q1: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.
SELECT batsman ,cast(strike_rate as decimal(4,1)),DENSE_RANK() OVER (ORDER BY strike_rate DESC) AS player_rank
FROM(
SELECT batsman,CAST (player_total_runs as float)/balls_faced*100 as strike_rate
FROM( SELECT batsman,sum(batsman_runs) AS player_total_runs,
count(ball) AS balls_faced FROM ipl_player_data WHERE NOT extras_type ='wides'
GROUP BY batsman) AS a WHERE balls_faced>500 ) AS b ORDER BY strike_rate DESC LIMIT 10;
182.3
164.3
159.3
155.4
154.7
152
151.9
150.1
149.9
149.6
AD Russell
SP Narine
HH Pandya
V Sehwag
GJ Maxwell
RR Pant
AB de Villiers
CH Gayle
KA Pollard
JC Buttler
Batsman Strike_Rate
5. Q2: Now you need to get 2-3 players with good Average who have played more than 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
SELECT batsman,player_average,DENSE_RANK() OVER (ORDER BY player_average DESC)
FROM ( SELECT *,cast(AVG(total_runs/dismissed_no) as decimal(3,1) )AS player_average
FROM( select batsman,sum(batsman_runs) as total_runs,
sum(is_wicket) as dismissed_no,count(distinct extract(year from match_date)) as played_years
from (SELECT a.batsman, a.batsman_runs, a.is_wicket, b.match_date
FROM ipl_player_data as a full join ipl_match_data as b
on a.id=b.id ) AS c GROUP BY batsman
) as d group by batsman,total_runs,dismissed_no,
played_years having dismissed_no>=1 and played_years>2 ORDER BY player_average DESC) as e LIMIT 10;
88
42 42 41 41 41 41 39 39 38
Batsman With Good Average
6. Q3: 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.
select batsman,cast(boundary_percentage as decimal(3,1)),DENSE_RANK() OVER (ORDER BY boundary_percentage DESC) from(
select *,(cast(boundary_runs as float)/total_runs*100) as boundary_percentage
from ( SELECT batsman,total_runs,SUM(batsman_runs) AS boundary_runs,
COUNT(batsman_runs) AS boundaries_total,count(distinct extract(year from match_date)) as played_years
from (SELECT a.batsman,a.batsman_runs,SUM(a.batsman_runs) OVER (PARTITION BY a.batsman) as total_runs,
b.match_date FROM ipl_player_data as a full join ipl_match_data as b
on a.id=b.id ) as c WHERE batsman_runs=4 or batsman_runs=6 group by total_runs,batsman order by boundaries_total DESC
) as d where played_years>2 order by boundary_percentage desc ) as e limit 10 ;
81.2
78.7
76.1
75.1
74.2
73.1 72.9 72.9 72.7 72.4
Player with most boundary_percentage
7. Q4: Your first priority is to get 2-3 bowlers with good economy 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
select bowler,cast(economy as decimal(2,1)),DENSE_RANK() OVER (ORDER BY economy asc) from(
select *,conceded_runs/cast(bowled_overs as decimal) as economy from(
select bowler,conceded_runs,bowled_balls,count(ball)/6||'.'||count(ball)%6 as bowled_overs
from( SELECT bowler,ball, sum(total_runs) over (partition by bowler) as conceded_runs,
count(ball) over (partition by bowler) as bowled_balls,over
from ipl_player_data ) as a where bowled_balls>500 group by bowler,conceded_runs,bowled_balls) as b
) as c limit 10 ;
6.3
6.7
6.7
6.8
6.8
6.8
6.9
6.9
6.9
7
Rashid Khan
A Kumble
M Muralitharan
DW Steyn
R Ashwin
SP Narine
DL Vettori
Washington Sundar
J Botha
R Tewatia
Bowler's Economy
8. Q5: 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
select bowler,cast(strike_rate as decimal(3,1)),DENSE_RANK() OVER (ORDER BY strike_rate) from (
select *,cast(total_balls as float)/wicket_taken as strike_rate from (
select bowler,total_balls,sum(is_wicket) as wicket_taken from (
select bowler,is_wicket,count(ball) over (partition by bowler) as total_balls from ipl_player_data ) as a
where is_wicket>0 and total_balls>500
group by bowler,total_balls )as b order by strike_rate asc limit 10 )as c
12.7
14
14.3
15.7
15.8
15.8
16.3
16.3
16.4
16.7
K Rabada
DE Bollinger
AJ Tye
MA Starc
SL Malinga
Imran Tahir
DJ Bravo
A Nehra
S Aravind
KK Cooper
Bowler's Strike Rate
9. Q6: 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 balls.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.
create table bats_sr as ( select batsman,cast(player_total_runs as decimal)/balls_faced*100 as batting_sr from(
SELECT batsman,sum(batsman_runs) AS player_total_runs,
count(ball) AS balls_faced FROM ipl_player_data WHERE NOT extras_type ='wides'
GROUP BY batsman having count(ball)>500 ) as a )
create table bowl_sr as (select bowler,cast(total_balls as decimal)/wicket_taken as bowling_sr from(
select bowler,total_balls,sum(is_wicket) as wicket_taken from (
select bowler,is_wicket,count(ball) over (partition by bowler) as total_balls from ipl_player_data ) as a
where is_wicket>0 and total_balls>300
group by bowler,total_balls) as a )
select a.batsman as allrounder,cast(a.batting_sr as decimal(4,1)),cast(b.bowling_sr as decimal(3,1))
from bats_sr as a inner join bowl_sr as b on a.batsman=b.bowler where batting_sr>150 and bowling_sr<21
182.3 164.3 159.3
17.7
19.7 20.3
AD Russell SP Narine HH Pandya
bowling_sr 17.7 19.7 20.3
batting_sr 182.3 164.3 159.3
Allrounder's Data
batting_sr bowling_sr
10. Wicketkeeper :
• Should have been played more than 2 ipl seasons.
• Having batting strike rate of 125+ .
• Having bowling economy rate < 10.
• Having good fielding rate.
12. --CREATING TABLE OF MATCHES DATA :
CREATE TABLE Matches
(id int,city char(50),match_date varchar(50),player_of_match char(50),venue varchar(255),neutral_venue varchar(255),team1
varchar(100),team2 varchar(100),toss_winner varchar(100),toss_decision char(50),winner varchar(100),result varchar(50),result_margin
int,eliminator char(10),method varchar(50),umpire1 char(50),umpire2 char(50) );
--COPYING CSV FILE :
COPY Matches FROM 'C:Sudhanshu WaliainternshalaSQLProjectIPL DatasetIPL_matches.csv' DELIMITER ',' CSV HEADER;
--ALTER THE DATATYPE OF match_date COLUMN TO DATE DATATYPE :
ALTER TABLE Matches ALTER COLUMN match_date TYPE DATE USING to_date(match_date,'DD-MM-YYYY’);
select * from Matches;
13. 1. Get the count of cities that have hosted an IPL match
QUERY : select count(city) from Matches;
14. 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) (Hint 1 : CASE
WHEN statement is used to get condition based results) (Hint 2: To convert the output data of the select statement into a table,
you can use a subquery. Create table table_name as [entire select statement].
QUERY : create table deliveries_v02 as ( select *,
case
when total_runs>=4 then 'boundary'
when total_runs=0 then 'dot'
else 'other'
end ball_result
from Deliveries )
15. 3. Write a query to fetch the total number of boundaries and dot balls from the deliveries_v02 table.
QUERY : select ball_result,count(ball_result) from deliveries_v02 where ball_result='boundary' or
ball_result='dot' group by ball_result
16. 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.
QUERY : select batting_team,count(ball_result) as no_of_boundaries from deliveries_v02 where
ball_result='boundary' group by batting_team order by no_of_boundaries desc
17. 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.
QUERY : select bowling_team,count(ball_result) as no_of_dot_balls from deliveries_v02 where
ball_result='dot' group by bowling_team order by no_of_dot_balls desc
18. 6. Write a query to fetch the total number of dismissals by dismissal kinds where dismissal kind is not NA
QUERY : select dismissal_kind,count(dismissal_kind) from deliveries_v02 where not dismissal_kind='NA' group
by dismissal_kind
19. 7. Write a query to get the top 5 bowlers who conceded maximum extra runs from the deliveries table
QUERY : select bowler,sum(extra_runs) as conceded_extra_runs from deliveries_v02 group by bowler order by
bowler desc limit 5;
20. 8. Write a query to create a table named deliveries_v03 with all the columns of deliveries_v02 table and two
additional column (named venue and match_date) of venue and date from table matches
QUERY : create table deliveries_v03 as( select a.* ,b.venue,b.match_date from deliveries_v02 as a full join
Matches as b on a.id=b.id) ;
21. 9. Write a query to fetch the total runs scored for each venue and order it in the descending order of total runs
scored.
QUERY : select venue,sum(total_runs) as total_runs from deliveries_v03 group by venue order by venue desc ;
22. 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.
QUERY : select distinct extract(year from match_date) as yearwise_total_run,sum(total_runs) as total_runs
from deliveries_v03 where venue='Eden Gardens' group by yearwise_total_run;