w3resource

SQL Exercise: List the referees with their countries for each match


48. From the following table, write a SQL query to find the name of referees for each match. Sort the result-set on match number. Return match number, country name, referee name.

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 Solution:

SQL Code:

-- Selecting match number, country name, and referee name for each match
SELECT 
    a.match_no, -- Selecting the match number from the match_mast table
    b.country_name, -- Selecting the country name from the soccer_country table
    c.referee_name -- Selecting the referee name from the referee_mast table
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 on the referee ID
JOIN 
    soccer_country b ON c.country_id = b.country_id -- Joining the referee_mast table with the soccer_country table on the country ID
	-- Ordering the results by match number
ORDER BY 
    a.match_no; 

Sample Output:

 match_no |  country_name  |      referee_name
----------+----------------+-------------------------
        1 | Hungary        | Viktor Kassai
        2 | Spain          | Carlos Velasco Carballo
        3 | Norway         | Svein Oddvar Moen
        4 | Italy          | Nicola Rizzoli
        5 | Sweden         | Jonas Eriksson
        6 | Romania        | Ovidiu Hategan
        7 | England        | Martin Atkinson
        8 | Poland         | Szymon Marciniak
        9 | Serbia         | Milorad Mazic
       10 | England        | Mark Clattenburg
       11 | France         | Clement Turpin
       12 | Turkey         | Cuneyt Cakir
       13 | Slovenia       | Damir Skomina
       14 | Russia         | Sergei Karasev
       15 | Scotland       | William Collum
       16 | Germany        | Felix Brych
       17 | Czech Republic | Pavel Kralovec
       18 | Netherlands    | Bjorn Kuipers
       19 | Hungary        | Viktor Kassai
       20 | England        | Mark Clattenburg
       21 | Serbia         | Milorad Mazic
       22 | Turkey         | Cuneyt Cakir
       23 | Russia         | Sergei Karasev
       24 | Italy          | Nicola Rizzoli
       25 | Czech Republic | Pavel Kralovec
       26 | Slovenia       | Damir Skomina
       27 | Sweden         | Jonas Eriksson
       28 | Spain          | Carlos Velasco Carballo
       29 | Norway         | Svein Oddvar Moen
       30 | France         | Clement Turpin
       31 | Scotland       | William Collum
       32 | Netherlands    | Bjorn Kuipers
       33 | Poland         | Szymon Marciniak
       34 | England        | Martin Atkinson
       35 | Romania        | Ovidiu Hategan
       36 | Germany        | Felix Brych
       37 | England        | Mark Clattenburg
       38 | England        | Martin Atkinson
       39 | Spain          | Carlos Velasco Carballo
       40 | Italy          | Nicola Rizzoli
       41 | Poland         | Szymon Marciniak
       42 | Serbia         | Milorad Mazic
       43 | Turkey         | Cuneyt Cakir
       44 | Slovenia       | Damir Skomina
       45 | Germany        | Felix Brych
       46 | Slovenia       | Damir Skomina
       47 | Hungary        | Viktor Kassai
       48 | Netherlands    | Bjorn Kuipers
       49 | Sweden         | Jonas Eriksson
       50 | Italy          | Nicola Rizzoli
       51 | England        | Mark Clattenburg
(51 rows)

Code Explanation:

The said query in SQL that selects the match number, country name, and referee name for each match in the match_mast table.
The JOIN clause in this query then joins the match_mast and referee_mast tables based on the referee_id column and joins the results with soccer_country tables based on the country_id column.
The query then orders the results by match number in ascending order using the ORDER BY clause.

Alternative Solution:

Correlated Subquery:

-- Selecting match number, country name, and referee name for each match
SELECT 
  a.match_no, -- Selecting the match number from the match_mast table
  ( -- Subquery to select the country name associated with the referee for each match
    SELECT b.country_name -- Selecting the country name from the soccer_country table
    FROM soccer_country b -- Specifying the soccer_country table with alias 'b'
    JOIN referee_mast c ON b.country_id = c.country_id -- Joining the soccer_country table with the referee_mast table on the country ID
    WHERE a.referee_id = c.referee_id -- Matching the referee ID with the one in the outer query
  ) AS country_name, -- Alias for the country name subquery result
  ( -- Subquery to select the referee name for each match
    SELECT c.referee_name -- Selecting the referee name from the referee_mast table
    FROM referee_mast c -- Specifying the referee_mast table with alias 'c'
    WHERE a.referee_id = c.referee_id -- Matching the referee ID with the one in the outer query
  ) AS referee_name -- Alias for the referee name subquery result
FROM 
  match_mast a -- Specifying the match_mast table with alias 'a'
  -- Ordering the results by match number
ORDER BY 
  a.match_no; 

Explanation:

This query uses correlated subqueries to retrieve the country name and referee name based on the referee ID associated with each match. The outer query selects the match number.

Relational Algebra Expression:

Relational Algebra Expression: List the name of referees with their countries for each match.


Relational Algebra Tree:

Relational Algebra Tree: List the name of referees with their countries for each match.


Go to:


PREV : Countries where assistant referees assist most matches.
NEXT : The number of matches each referee has managed.


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.