SQL Exercise: Find the number of booking happened in stoppage time
28. From the following table, write a SQL query to count the number of bookings during stoppage time.
Sample table: player_booked
match_no | team_id | player_id | booking_time | sent_off | play_schedule | play_half
----------+---------+-----------+--------------+----------+---------------+-----------
1 | 1216 | 160349 | 32 | | NT | 1
1 | 1216 | 160355 | 45 | | NT | 1
1 | 1207 | 160159 | 69 | Y | NT | 2
1 | 1216 | 160360 | 78 | | NT | 2
2 | 1221 | 160470 | 14 | | NT | 1
2 | 1201 | 160013 | 23 | | NT | 1
2 | 1201 | 160013 | 36 | | NT | 1
2 | 1201 | 160014 | 63 | | NT | 2
2 | 1221 | 160472 | 66 | | NT | 2
.......
51 | 1214 | 160302 | 122 | | ET | 2
Sample Solution:
SQL Code:
-- This SQL query calculates the count of rows in the 'player_booked' table where the 'play_schedule' column has the value 'ST'.
SELECT COUNT(*)
-- COUNT(*) is an aggregate function that counts the number of rows in a result set.
FROM player_booked
-- 'player_booked' is the name of the table being queried.
WHERE play_schedule='ST';
-- The WHERE clause filters rows where the 'play_schedule' column has the value 'ST'.
Sample Output:
count
-------
10
(1 row)
Code Explanation:
The said query in SQL that determine the number of players who have been booked during stoppage time of a game.
The WHERE clause specifies a condition to filter the rows where the value of "play_schedule" is equal to 'ST'.
Relational Algebra Expression:
Relational Algebra Tree:
Go to:
PREV : Within each half of play, find the number of bookings.
NEXT : Find the number of booking happened in extra time.
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.
