w3resource

SQL Exercise: Total number of goalless draws in the tournament


19. From the following table, write a SQL query to count the total number of goalless draws played in the entire tournament. Return number of goalless draws.

Sample table: match_details

 match_no | play_stage | team_id | win_lose | decided_by | goal_score | penalty_score | ass_ref | player_gk
----------+------------+---------+----------+------------+------------+---------------+---------+-----------
        1 | G          |    1207 | W        | N          |          2 |               |   80016 |    160140
        1 | G          |    1216 | L        | N          |          1 |               |   80020 |    160348
        2 | G          |    1201 | L        | N          |          0 |               |   80003 |    160001
        2 | G          |    1221 | W        | N          |          1 |               |   80023 |    160463
        3 | G          |    1224 | W        | N          |          2 |               |   80031 |    160532
        3 | G          |    1218 | L        | N          |          1 |               |   80025 |    160392
        4 | G          |    1206 | D        | N          |          1 |               |   80008 |    160117
        4 | G          |    1217 | D        | N          |          1 |               |   80019 |    160369
        5 | G          |    1222 | L        | N          |          0 |               |   80011 |    160486
..........
       51 | F          |    1207 | L        | N          |          0 |               |   80007 |    160140

View the table

Sample Solution:

-- This SQL query calculates the count of distinct 'match_no' values in the 'match_details' table
-- where certain conditions are met: 'win_lose' is 'D' and 'goal_score' is 0.

SELECT COUNT(DISTINCT match_no) 
-- COUNT(DISTINCT match_no) counts the number of unique 'match_no' values in the result set.
FROM match_details 
-- 'match_details' is the name of the table being queried.
WHERE win_lose='D' 
-- The WHERE clause filters rows where the 'win_lose' column has the value 'D'.
AND goal_score=0;
-- Further filters rows where the 'goal_score' column has the value 0.

Sample Output:

 count
-------
     4
(1 row)

Code Explanation:

The said query in SQL that returns a single row with a single column containing the count of distinct match numbers where the result was a draw ("D") and the total goals scored in the match was 0. The DISTINCT keyword is used to ensure that each match number is only counted once, even if there were multiple draws with 0 goals scored in the same match.

Relational Algebra Expression:

Relational Algebra Expression: Find the total number of goalless draws have there in the entire tournament.


Relational Algebra Tree:

Relational Algebra Tree: Find the total number of goalless draws have there in the entire tournament.


Go to:


PREV : Number of players replaced in the first half of play.
NEXT : Number of players replaced in the extra time of play.


Practice Online




Sample Database: soccer

soccer database relationship structure.


Query Visualization:

Duration:

Query visualization of Find the total number of goalless draws have there in the entire tournament - Duration


Rows:

Query visualization of Find the total number of goalless draws have there in the entire tournament - Rows


Cost:

Query visualization of Find the total number of goalless draws have there in the entire 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.