SQL Exercise: Find where the penalty kick number 23 had been taken
From the following table, write a SQL query to find the stage of the match where penalty kick number 23 was taken. Return match number, play_stage.
Sample table: match_mast
match_no | play_stage | play_date | results | decided_by | goal_score | venue_id | referee_id | audence | plr_of_match | stop1_sec | stop2_sec
----------+------------+------------+---------+------------+------------+----------+------------+---------+--------------+-----------+-----------
1 | G | 2016-06-11 | WIN | N | 2-1 | 20008 | 70007 | 75113 | 160154 | 131 | 242
2 | G | 2016-06-11 | WIN | N | 0-1 | 20002 | 70012 | 33805 | 160476 | 61 | 182
3 | G | 2016-06-11 | WIN | N | 2-1 | 20001 | 70017 | 37831 | 160540 | 64 | 268
4 | G | 2016-06-12 | DRAW | N | 1-1 | 20005 | 70011 | 62343 | 160128 | 0 | 185
5 | G | 2016-06-12 | WIN | N | 0-1 | 20007 | 70006 | 43842 | 160084 | 125 | 325
6 | G | 2016-06-12 | WIN | N | 1-0 | 20006 | 70014 | 33742 | 160291 | 2 | 246
7 | G | 2016-06-13 | WIN | N | 2-0 | 20003 | 70002 | 43035 | 160176 | 89 | 188
8 | G | 2016-06-13 | WIN | N | 1-0 | 20010 | 70009 | 29400 | 160429 | 360 | 182
9 | G | 2016-06-13 | DRAW | N | 1-1 | 20008 | 70010 | 73419 | 160335 | 67 | 194
.........
51 | F | 2016-07-11 | WIN | N | 1-0 | 20008 | 70005 | 75868 | 160307 | 161 | 181
Sample table: penalty_shootout
kick_id | match_no | team_id | player_id | score_goal | kick_no
---------+----------+---------+-----------+------------+---------
1 | 37 | 1221 | 160467 | Y | 1
2 | 37 | 1213 | 160297 | Y | 2
3 | 37 | 1221 | 160477 | N | 3
4 | 37 | 1213 | 160298 | Y | 4
5 | 37 | 1221 | 160476 | Y | 5
6 | 37 | 1213 | 160281 | Y | 6
7 | 37 | 1221 | 160470 | Y | 7
8 | 37 | 1213 | 160287 | Y | 8
9 | 37 | 1221 | 160469 | Y | 9
........
37 | 47 | 1208 | 160166 | Y | 18
Sample Solution:
SQL Code:
SELECT match_no,
play_stage
FROM match_mast
WHERE match_no=
(SELECT match_no
FROM penalty_shootout
WHERE kick_id=23);
Sample Output:
match_no | play_stage
----------+------------
47 | Q
(1 row)
Code Explanation:
The said query in SQL that selects the match number and play stage for a specific match from the match_mast table in which a penalty kick with ID 23 was taken.
The condition compares the match number in the match_mast table to a subquery that selects the match number from the penalty_shootout table where the kick ID is 23. The subquery is enclosed in parentheses to indicate that it returns a single value. The overall effect of this condition is to retrieve data for the match in which the specified penalty kick was taken.
Alternative Solution:
JOIN with Subquery:
SELECT m.match_no, m.play_stage
FROM match_mast m
JOIN (
SELECT match_no
FROM penalty_shootout
WHERE kick_id = 23
) p ON m.match_no = p.match_no;
Explanation:
This query uses an inner join between the match_mast table and a subquery that selects match_no from penalty_shootout where kick_id is 23. The result is filtered based on matching match_no.
Using EXISTS:
SELECT match_no, play_stage
FROM match_mast m
WHERE EXISTS (
SELECT 1
FROM penalty_shootout p
WHERE p.match_no = m.match_no
AND p.kick_id = 23
);
Explanation:
This query uses the EXISTS keyword to check if there exists at least one row in the penalty_shootout table where both match_no and kick_id match the corresponding columns in match_mast.
Using IN:
SELECT match_no, play_stage
FROM match_mast
WHERE match_no IN (
SELECT match_no
FROM penalty_shootout
WHERE kick_id = 23
);
Explanation:
This query uses the IN operator to filter the match_no in match_mast based on the set of match_no values returned by the subquery that selects match_no where kick_id is 23 in the penalty_shootout table.
Go to:
PREV : Player of portugal taken the 7th kick against poland.
NEXT : Find the venues where penalty shootout matches played.
Practice Online
Sample Database: soccer
Query Visualization:
Duration:
Rows:
Cost:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
