0% found this document useful (0 votes)
52 views

Questions and Answers On Ipl Database

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Questions and Answers On Ipl Database

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

QUESTIONS AND ANSWERS ON

IPL DATABASE
1.Find the top 3 players with the highest total runs in all matches using INNER JOIN?

select p.player_name,sum(pp.runs_scored) as Highest_Runs from players p

INNER join performance pp on p.player_id=pp.player_id

group by p.player_name

order by Highest_Runs desc

limit 3;

2.Find all players and their respective teams, including those who haven't played any matches
(use LEFT JOIN)?

select p.player_name, t.team_name from players p

left join performance pp on p.player_id=pp.player_id

left join teams t on t.team_id=p.team_id

WHERE p.player_id IS NULL

group by p.player_name, t.team_name ;

3. Find the total number of matches played by each team using JOIN?

SELECT t.team_name, COUNT(m.match_id) AS matches_played

FROM teams t

INNER JOIN matches m ON t.team_id IN (m.team1_id, m.team2_id)

GROUP BY t.team_name;
4.Get a list of all teams that have never won a match using LEFT JOIN?

SELECT t.team_name

FROM teams t

LEFT JOIN matches m ON t.team_id = m.winner_team_id

WHERE m.winner_team_id IS NULL;

5.List all players who have played in matches won by their own team using JOIN?

SELECT p.player_name, t.team_name, m.match_id

FROM players p

INNER JOIN matches m ON p.team_id = m.winner_team_id

INNER JOIN teams t ON p.team_id = t.team_id;

6.Calculate the running total of runs scored by each player across all matches?

select p.player_name,p.player_id,pp.match_id,

sum(pp.runs_scored) over(partition by pp.match_id order by p.player_id) as Total_Runs

from players p

join performance pp on p.player_id=pp.player_id

join teams t on t.team_id=p.team_id;

7.Rank players based on their highest runs in a single match using RANK()?

SELECT p.player_name, per.runs_scored,

RANK() OVER (ORDER BY per.runs_scored DESC) AS ranks

FROM players p

INNER JOIN performance per ON p.player_id = per.player_id;


8. Find the top 2 run scorers per team using ROW_NUMBER()?

SELECT player_name, team_name, runs_scored

FROM (

SELECT p.player_name, t.team_name, per.runs_scored,

ROW_NUMBER() OVER (PARTITION BY t.team_name ORDER BY per.runs_scored DESC) AS


rn

FROM players p

INNER JOIN performance per ON p.player_id = per.player_id

INNER JOIN teams t ON p.team_id = t.team_id

) AS ranked_players

WHERE rn <= 2;

9.Calculate the average runs scored per player over their last 3 matches using LAG()?

SELECT p.player_name, per.match_id, per.runs_scored,

AVG(LAG(per.runs_scored, 1) OVER (PARTITION BY p.player_id ORDER BY per.match_id)) AS


avg_last_3_matches

FROM players p

INNER JOIN performance per ON p.player_id = per.player_id;

10.Calculate the total wickets taken by a bowler and rank them within their team using
DENSE_RANK()?

SELECT p.player_name, t.team_name, per.wickets_taken,

DENSE_RANK() OVER (PARTITION BY t.team_name ORDER BY per.wickets_taken DESC) AS


ranks

FROM players p

INNER JOIN performance per ON p.player_id = per.player_id

INNER JOIN teams t ON p.team_id = t.team_id

WHERE p.role = 'Bowler';


11. Use CASE to categorize players based on their roles?

SELECT player_name,

CASE

WHEN role = 'Batsman' THEN 'Batsman'

WHEN role = 'Bowler' THEN 'Bowler'

WHEN role = 'All-Rounder' THEN 'All-Rounder'

ELSE 'Other'

END AS role_category

FROM players;

12. Use a CTE to find the total runs scored by players who have scored less than 1000 runs
across all matches?

WITH player_totals AS (

SELECT p.player_name, SUM(per.runs_scored) AS total_runs

FROM players p

INNER JOIN performance per ON p.player_id = per.player_id

GROUP BY p.player_name

SELECT * FROM player_totals

WHERE total_runs < 1000;

13.Create a VIEW to list the top 5 highest run-scorers of all time?


CREATE VIEW top_run_scorers AS

SELECT p.player_name, SUM(per.runs_scored) AS total_runs

FROM players p

INNER JOIN performance per ON p.player_id = per.player_id

GROUP BY p.player_name

ORDER BY total_runs DESC

LIMIT 5;

14. Use a Subquery to find players who have played in more than 5 matches?

SELECT p.player_name

FROM players p

WHERE (SELECT COUNT(*) FROM performance per WHERE per.player_id = p.player_id) > 5;

15.Use CASE to calculate bonus points based on performance (e.g., runs scored and wickets
taken)?

SELECT player_id,

runs_scored,

wickets_taken,

CASE

WHEN runs_scored >= 50 THEN 5

WHEN wickets_taken >= 3 THEN 10

ELSE 0

END AS bonus_points

FROM performance;

You might also like