SQL Exercise: Find the number of self-goals scored in EURO cup 2016
7. From the following table, write a SQL query to find the number of self-goals scored during the 2016 European Championship.
Sample table: goal_details
goal_id | match_no | player_id | team_id | goal_time | goal_type | play_stage | goal_schedule | goal_half
---------+----------+-----------+---------+-----------+-----------+------------+---------------+-----------
1 | 1 | 160159 | 1207 | 57 | N | G | NT | 2
2 | 1 | 160368 | 1216 | 65 | P | G | NT | 2
3 | 1 | 160154 | 1207 | 89 | N | G | NT | 2
4 | 2 | 160470 | 1221 | 5 | N | G | NT | 1
5 | 3 | 160547 | 1224 | 10 | N | G | NT | 1
6 | 3 | 160403 | 1218 | 61 | N | G | NT | 2
7 | 3 | 160550 | 1224 | 81 | N | G | NT | 2
8 | 4 | 160128 | 1206 | 73 | N | G | NT | 2
9 | 4 | 160373 | 1217 | 93 | N | G | ST | 2
.........
108 | 51 | 160319 | 1214 | 109 | N | F | ET | 2
Sample Solution:
-- This SQL query calculates the count of rows in the 'goal_details' table where the 'goal_type' column has the value 'O'.
SELECT COUNT(*)
-- COUNT(*) is an aggregate function that counts the number of rows in a result set.
FROM goal_details
-- 'goal_details' is the name of the table being queried.
WHERE goal_type='O';
-- The WHERE clause filters rows where the 'goal_type' column has the value 'O'.
Sample Output:
count
-------
3
(1 row)
Code Explanation:
The said query in SQL that selects the number of rows in the goal_details table where the goal_type column is equal to 'O'.
The result of the query will be a single value representing the count of rows where goal_type is equal to 'O'.
Relational Algebra Expression:
Relational Algebra Tree:
Go to:
PREV : Find the date when did Football EURO cup 2016 begin.
NEXT : Number of matches ended with a results in group stage.
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.
