SQL Exercise: Number of matches got a result by penalty shootout
9. From the following table, write a SQL query to find the number of matches that resulted in a penalty shootout.
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:
-- This SQL query calculates the count of distinct 'match_no' values in the 'penalty_shootout' table.
SELECT COUNT(DISTINCT match_no)
-- COUNT(DISTINCT match_no) counts the number of unique 'match_no' values in the result set.
FROM penalty_shootout;
-- 'penalty_shootout' is the name of the table from which the distinct 'match_no' count is being calculated.
Sample Output:
count
-------
3
(1 row)
Code Explanation:
The said query in SQL that selects the count of distinct values in the match_no column of the penalty_shootout table.
The result of the query will be a single value. This query will count the number of unique matches that have penalty shootouts recorded in the penalty_shootout table.
Relational Algebra Expression:
Relational Algebra Tree:
Go to:
PREV : Number of matches ended with a results in group stage.
NEXT : Matches decided by penalties in the Round of 16.
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.
