SQL Exercise: Find the number of shots scored in penalty shootouts
23. From the following table, write a SQL query to count the number of shots that were scored in penalty shootouts matches. Return number of shots scored goal as "Goal Scored by Penalty Kicks".
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:
-- This SQL query calculates the count of rows in the 'penalty_shootout' table
-- where the 'score_goal' column has the value 'Y', and aliases the result as "Goal Scored by Penalty Kicks".
SELECT COUNT(*) AS "Goal Scored by Penalty Kicks"
-- COUNT(*) is an aggregate function that counts the number of rows in a result set, and the alias is set to "Goal Scored by Penalty Kicks".
FROM penalty_shootout
-- 'penalty_shootout' is the name of the table being queried.
WHERE score_goal='Y';
-- The WHERE clause filters rows where the 'score_goal' column has the value 'Y'.
Sample Output:
Goal Scored by Penalty Kicks
------------------------------
28
(1 row)
Code Explanation:
The said query in SQL that counts the number of goals scored by penalty kicks from the table 'penalty_shootout' and aliases the result column as "Goal Scored by Penalty Kicks". The WHERE clause in the query will consider the total number of penalty kicks resulting in a goal being scored.
Go to:
PREV : Number of shots taken in penalty shootout matches.
NEXT : Number of shots missed or saved in penalty shootouts.
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.
