w3resource

SQL Exercise: Total number of players replaced in the tournament


15. From the following table, write a SQL query to count the number of players replaced in the tournament. Return number of players as "Player Replaced".

Sample table: player_in_out

 match_no | team_id | player_id | in_out | time_in_out | play_schedule | play_half
----------+---------+-----------+--------+-------------+---------------+-----------
        1 |    1207 |    160151 | I      |          66 | NT            |         2
        1 |    1207 |    160160 | O      |          66 | NT            |         2
        1 |    1207 |    160161 | I      |          77 | NT            |         2
        1 |    1207 |    160161 | O      |          77 | NT            |         2
        1 |    1207 |    160157 | I      |           2 | ST            |         2
        1 |    1207 |    160154 | O      |           2 | ST            |         2
        1 |    1216 |    160365 | I      |          61 | NT            |         2
        1 |    1216 |    160366 | O      |          61 | NT            |         2
        1 |    1216 |    160357 | I      |          72 | NT            |         2
........
       51 |    1214 |    160316 | O      |          79 | NT            |         2	   	

View the table

Sample Solution:

-- This SQL query calculates the count of rows in the 'player_in_out' table where the 'in_out' column has the value 'I'
-- and aliases the result as "Player Replaced".

SELECT COUNT(*) as "Player Replaced"
-- COUNT(*) is an aggregate function that counts the number of rows in a result set, and the alias is set to "Player Replaced".
FROM player_in_out
-- 'player_in_out' is the name of the table being queried.
WHERE in_out='I';
-- The WHERE clause filters rows where the 'in_out' column has the value 'I'.

Sample Output:

 Player Replaced
-----------------
             293
(1 row)

Code Explanation:

The said query in SQL that selects the count of all rows where the in_out column has a value of 'I' from the table 'player_in_out'. The query also assigns the label "Player Replaced" to the resulting count using the AS keyword.
The result of this query returns a single value representing the total number of times that a player was substituted into a match.

Go to:


PREV : Matches, one goal wins except decided by penalty kicks.
NEXT : Total number of players replaced during normal play.


Practice Online




Sample Database: soccer

soccer database relationship structure.


Query Visualization:

Duration:

Query visualization of Find the total number of players replaced in the tournament - Duration.


Rows:

Query visualization of Find the total number of players replaced in the tournament - Rows.


Cost:

Query visualization of Find the total number of players replaced in the tournament - 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.



Follow us on Facebook and Twitter for latest update.