w3resource

SQL Exercise: Find the number of matches each referee managed


53. From the following tables, write a SQL query to find those referees who managed the number of matches at each venue. Return referee name, country name, venue name, number of matches.

Sample table: match_mast

 match_no | play_stage | play_date  | results | decided_by | goal_score | venue_id | referee_id | audence | plr_of_match | stop1_sec | stop2_sec
----------+------------+------------+---------+------------+------------+----------+------------+---------+--------------+-----------+-----------
        1 | G          | 2016-06-11 | WIN     | N          | 2-1        |    20008 |      70007 |   75113 |       160154 |       131 |       242
        2 | G          | 2016-06-11 | WIN     | N          | 0-1        |    20002 |      70012 |   33805 |       160476 |        61 |       182
        3 | G          | 2016-06-11 | WIN     | N          | 2-1        |    20001 |      70017 |   37831 |       160540 |        64 |       268
        4 | G          | 2016-06-12 | DRAW    | N          | 1-1        |    20005 |      70011 |   62343 |       160128 |         0 |       185
        5 | G          | 2016-06-12 | WIN     | N          | 0-1        |    20007 |      70006 |   43842 |       160084 |       125 |       325
        6 | G          | 2016-06-12 | WIN     | N          | 1-0        |    20006 |      70014 |   33742 |       160291 |         2 |       246
        7 | G          | 2016-06-13 | WIN     | N          | 2-0        |    20003 |      70002 |   43035 |       160176 |        89 |       188
        8 | G          | 2016-06-13 | WIN     | N          | 1-0        |    20010 |      70009 |   29400 |       160429 |       360 |       182
        9 | G          | 2016-06-13 | DRAW    | N          | 1-1        |    20008 |      70010 |   73419 |       160335 |        67 |       194
........
       51 | F          | 2016-07-11 | WIN     | N          | 1-0        |    20008 |      70005 |   75868 |       160307 |       161 |       181

View the table

Sample table: referee_mast

 referee_id |      referee_name       | country_id
------------+-------------------------+------------
      70001 | Damir Skomina           |       1225
      70002 | Martin Atkinson         |       1206
      70003 | Felix Brych             |       1208
      70004 | Cuneyt Cakir            |       1222
      70005 | Mark Clattenburg        |       1206
      70006 | Jonas Eriksson          |       1220
      70007 | Viktor Kassai           |       1209
      70008 | Bjorn Kuipers           |       1226
      70009 | Szymon Marciniak        |       1213
.......
      70018 | Clement Turpin          |       1207

View the table

Sample table: soccer_country

 country_id | country_abbr |    country_name
------------+--------------+---------------------
       1201 | ALB          | Albania
       1202 | AUT          | Austria
       1203 | BEL          | Belgium
       1204 | CRO          | Croatia
       1205 | CZE          | Czech Republic
       1206 | ENG          | England
       1207 | FRA          | France
       1208 | GER          | Germany
       1209 | HUN          | Hungary
.......
       1229 | NOR          | Norway

View the table

Sample table: soccer_venue

 venue_id |       venue_name        | city_id | aud_capacity
----------+-------------------------+---------+--------------
    20001 | Stade de Bordeaux       |   10003 |        42115
    20002 | Stade Bollaert-Delelis  |   10004 |        38223
    20003 | Stade Pierre Mauroy     |   10005 |        49822
    20004 | Stade de Lyon           |   10006 |        58585
    20005 | Stade VElodrome         |   10007 |        64354
    20006 | Stade de Nice           |   10008 |        35624
    20007 | Parc des Princes        |   10001 |        47294
    20008 | Stade de France         |   10002 |        80100
    20009 | Stade Geoffroy Guichard |   10009 |        42000
    20010 | Stadium de Toulouse     |   10010 |        33150

Sample Solution:

SQL Code:

-- Selecting the referee name, country name, venue name, and match count for each referee, country, and venue
SELECT 
    c.referee_name, -- Selecting the referee name
    b.country_name, -- Selecting the country name
    d.venue_name, -- Selecting the venue name
    COUNT(a.match_no) -- Counting the number of matches for each combination of referee, country, and venue
FROM 
    match_mast a -- Specifying the match_mast table with alias 'a'
JOIN 
    referee_mast c ON a.referee_id = c.referee_id -- Joining the match_mast table with the referee_mast table based on referee ID
JOIN 
    soccer_country b ON c.country_id = b.country_id -- Joining the referee_mast table with the soccer_country table based on country ID
JOIN 
    soccer_venue d ON a.venue_id = d.venue_id -- Joining the match_mast table with the soccer_venue table based on venue ID
GROUP BY 
    c.referee_name, -- Grouping the results by referee name
    b.country_name, -- Grouping the results by country name
    d.venue_name -- Grouping the results by venue name
	-- Sorting the results by referee name
ORDER BY 
    c.referee_name; 

Sample Output:

      referee_name       |  country_name  |       venue_name        | count
-------------------------+----------------+-------------------------+-------
 Bjorn Kuipers           | Netherlands    | Stade de Bordeaux       |     1
 Bjorn Kuipers           | Netherlands    | Stade de France         |     2
 Carlos Velasco Carballo | Spain          | Stade Bollaert-Delelis  |     2
 Carlos Velasco Carballo | Spain          | Stade Geoffroy Guichard |     1
 Clement Turpin          | France         | Parc des Princes        |     1
 Clement Turpin          | France         | Stade de Bordeaux       |     1
 Cuneyt Cakir            | Turkey         | Stade de Bordeaux       |     1
 Cuneyt Cakir            | Turkey         | Stade de France         |     1
 Cuneyt Cakir            | Turkey         | Stade Geoffroy Guichard |     1
 Damir Skomina           | Slovenia       | Stade de Nice           |     1
 Damir Skomina           | Slovenia       | Stade Pierre Mauroy     |     3
 Felix Brych             | Germany        | Stade Bollaert-Delelis  |     1
 Felix Brych             | Germany        | Stade de Nice           |     1
 Felix Brych             | Germany        | Stade VElodrome         |     1
 Jonas Eriksson          | Sweden         | Parc des Princes        |     1
 Jonas Eriksson          | Sweden         | Stade de Lyon           |     1
 Jonas Eriksson          | Sweden         | Stadium de Toulouse     |     1
 Mark Clattenburg        | England        | Stade de France         |     1
 Mark Clattenburg        | England        | Stade de Lyon           |     1
 Mark Clattenburg        | England        | Stade Geoffroy Guichard |     2
 Martin Atkinson         | England        | Parc des Princes        |     1
 Martin Atkinson         | England        | Stade de Lyon           |     1
 Martin Atkinson         | England        | Stade Pierre Mauroy     |     1
 Milorad Mazic           | Serbia         | Stade de France         |     1
 Milorad Mazic           | Serbia         | Stade de Nice           |     1
 Milorad Mazic           | Serbia         | Stadium de Toulouse     |     1
 Nicola Rizzoli          | Italy          | Parc des Princes        |     1
 Nicola Rizzoli          | Italy          | Stade de Lyon           |     1
 Nicola Rizzoli          | Italy          | Stade VElodrome         |     2
 Ovidiu Hategan          | Romania        | Stade de Nice           |     1
 Ovidiu Hategan          | Romania        | Stade Pierre Mauroy     |     1
 Pavel Kralovec          | Czech Republic | Stade de Lyon           |     2
 Sergei Karasev          | Russia         | Parc des Princes        |     1
 Sergei Karasev          | Russia         | Stade VElodrome         |     1
 Svein Oddvar Moen       | Norway         | Stade de Bordeaux       |     1
 Svein Oddvar Moen       | Norway         | Stade VElodrome         |     1
 Szymon Marciniak        | Poland         | Stade de France         |     1
 Szymon Marciniak        | Poland         | Stade Pierre Mauroy     |     1
 Szymon Marciniak        | Poland         | Stadium de Toulouse     |     1
 Viktor Kassai           | Hungary        | Stade de Bordeaux       |     1
 Viktor Kassai           | Hungary        | Stade de France         |     1
 Viktor Kassai           | Hungary        | Stadium de Toulouse     |     1
 William Collum          | Scotland       | Stade Bollaert-Delelis  |     1
 William Collum          | Scotland       | Stade VElodrome         |     1
(44 rows)

Code Explanation:

The said query in SQL that retrieves data from multiple tables and returns the number of matches each referee has officiated in each venue and country.
The JOIN clause joins the match_mast and referee_mast tables based on the referee_id column, the referee_mast and soccer_country tables based on the country_id column and the match_mast and soccer_venue tables based on the venue_id column.
The GROUP BY statement groups the results by referee_name, country_name, and venue_name.
The ORDER BY statement sorts the results in ascending order by referee_name.

Alternative Solutions:

Subquery with JOINs:

-- Selecting the referee name, country name, venue name, and match count for each referee, country, and venue
SELECT 
    c.referee_name, -- Selecting the referee name
    b.country_name, -- Selecting the country name
    d.venue_name, -- Selecting the venue name
    match_count -- Selecting the match count
FROM 
    ( -- Subquery to calculate the match count for each combination of referee, country, and venue
        SELECT 
            a.referee_id, -- Selecting the referee ID
            c.country_id, -- Selecting the country ID
            a.venue_id, -- Selecting the venue ID
            COUNT(a.match_no) as match_count -- Counting the number of matches for each combination
        FROM 
            match_mast a -- Specifying the match_mast table with alias 'a'
        JOIN 
            referee_mast c ON a.referee_id = c.referee_id -- Joining the match_mast table with the referee_mast table based on referee ID
        JOIN 
            soccer_country b ON c.country_id = b.country_id -- Joining the referee_mast table with the soccer_country table based on country ID
        GROUP BY 
            a.referee_id, c.country_id, a.venue_id -- Grouping the results by referee ID, country ID, and venue ID
    ) subquery -- Alias for the subquery
JOIN 
    referee_mast c ON subquery.referee_id = c.referee_id -- Joining the subquery with the referee_mast table based on referee ID
JOIN 
    soccer_country b ON subquery.country_id = b.country_id -- Joining the subquery with the soccer_country table based on country ID
JOIN 
    soccer_venue d ON subquery.venue_id = d.venue_id -- Joining the subquery with the soccer_venue table based on venue ID
	-- Sorting the results by referee name
ORDER BY 
    c.referee_name; 

Explanation:

This query first creates a subquery that calculates the match count for each combination of referee, country, and venue. It then joins this subquery with the necessary tables to retrieve the referee name, country name, venue name, and match count.

Using CROSS JOIN and Subquery:

-- Selecting the referee name, country name, venue name, and match count for each referee, country, and venue
SELECT 
    c.referee_name, -- Selecting the referee name
    b.country_name, -- Selecting the country name
    d.venue_name, -- Selecting the venue name
    COUNT(a.match_no) -- Counting the number of matches
FROM 
    match_mast a -- Specifying the match_mast table with alias 'a'
CROSS JOIN 
    referee_mast c -- Performing a cross join with the referee_mast table with alias 'c'
CROSS JOIN 
    soccer_country b -- Performing a cross join with the soccer_country table with alias 'b'
CROSS JOIN 
    soccer_venue d -- Performing a cross join with the soccer_venue table with alias 'd'
WHERE 
    a.referee_id = c.referee_id -- Matching the referee ID between match_mast and referee_mast
    AND c.country_id = b.country_id -- Matching the country ID between referee_mast and soccer_country
    AND a.venue_id = d.venue_id -- Matching the venue ID between match_mast and soccer_venue
GROUP BY 
    c.referee_name, b.country_name, d.venue_name -- Grouping the results by referee name, country name, and venue name
	-- Sorting the results by referee name
ORDER BY 
    c.referee_name; 

Explanation:

This query uses CROSS JOINs to generate all possible combinations of referees, countries, and venues. It then filters the results to match the respective IDs and groups by referee name, country name, and venue name.

Using a Self-Join and Subquery:

-- Selecting the referee name, country name, venue name, and match count for each referee, country, and venue
SELECT 
    c.referee_name, -- Selecting the referee name
    b.country_name, -- Selecting the country name
    d.venue_name, -- Selecting the venue name
    COUNT(a.match_no) -- Counting the number of matches
FROM 
    match_mast a -- Specifying the match_mast table with alias 'a'
JOIN 
    referee_mast c ON a.referee_id = c.referee_id -- Joining with referee_mast table on referee ID
JOIN 
    soccer_country b ON c.country_id = b.country_id -- Joining with soccer_country table on country ID
JOIN 
    soccer_venue d ON a.venue_id = d.venue_id -- Joining with soccer_venue table on venue ID
JOIN 
    (
        -- Subquery to calculate the match count for each referee, country, and venue
        SELECT 
            a.referee_id, 
            c.country_id, 
            a.venue_id, 
            COUNT(a.match_no) as match_count -- Counting the number of matches
        FROM 
            match_mast a -- Specifying the match_mast table with alias 'a'
        JOIN 
            referee_mast c ON a.referee_id = c.referee_id -- Joining with referee_mast table on referee ID
        JOIN 
            soccer_country b ON c.country_id = b.country_id -- Joining with soccer_country table on country ID
        GROUP BY 
            a.referee_id, c.country_id, a.venue_id -- Grouping by referee ID, country ID, and venue ID
    ) subquery ON a.referee_id = subquery.referee_id -- Joining with the subquery on referee ID
           AND c.country_id = subquery.country_id -- Joining with the subquery on country ID
           AND a.venue_id = subquery.venue_id -- Joining with the subquery on venue ID
GROUP BY 
    c.referee_name, b.country_name, d.venue_name -- Grouping the results by referee name, country name, and venue name
	-- Sorting the results by referee name
ORDER BY 
    c.referee_name; 

Explanation:

This query first creates a subquery to calculate the match count for each combination of referee, country, and venue. It then joins this subquery with the main query using a self-join on the respective IDs.

Relational Algebra Expression:

Relational Algebra Expression: Find the referees managed the number of matches in each venue.


Relational Algebra Tree:

Relational Algebra Tree: Find the referees managed the number of matches in each venue.



PREV : Find those referees who managed most of the match.
NEXT : Find the referees and number of booked he made.


Practice Online



Sample Database: soccer

soccer database relationship structure.


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.