w3resource

SQL Exercise: Find those referees who managed most of the match


52. From the following tables, write a SQL query to find those referees who managed most of the matches. Return referee name, country name and 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 Solution:

SQL Code:

-- Selecting the referee name, country name, and count of matches for each referee in each country
SELECT 
    c.referee_name, -- Selecting the referee name
    b.country_name, -- Selecting the country name
    COUNT(a.match_no) -- Counting the number of matches for each referee in each country
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 
    c.referee_name, -- Grouping the results by referee name
    b.country_name -- Grouping the results by country name
HAVING 
    COUNT(a.match_no) = ( -- Filtering the results to match the maximum count of matches for a referee in a country
        SELECT 
            MAX(mm) -- Selecting the maximum count of matches
        FROM 
            ( -- Subquery to calculate the count of matches for each referee in each country
                SELECT 
                    COUNT(a.match_no) AS mm -- Counting the number of matches for each referee in each country
                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 
                    c.referee_name, -- Grouping the results by referee name
                    b.country_name -- Grouping the results by country name
            ) hh -- Alias for the subquery
    );

Sample Output:

   referee_name   | country_name | count
------------------+--------------+-------
 Damir Skomina    | Slovenia     |     4
 Mark Clattenburg | England      |     4
 Nicola Rizzoli   | Italy        |     4
(3 rows)

Code Explanation:

The provided query in SQL that retrieves information about referees and the countries they are associated with, as well as the number of matches they have officiated.
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.
It then groups the data by referee_name and country_name and calculates the count of matches for each group.
The HAVING clause filters the results and only return the rows where the count of matches is equal to the maximum count of matches in the entire dataset.

Alternative Solution:

Using a Window Function:

-- Selecting the referee name, country name, and match count for the top-ranked referee in each country
SELECT 
    referee_name, -- Selecting the referee name
    country_name, -- Selecting the country name
    match_count -- Selecting the match count
FROM 
    ( -- Subquery to generate ranked results based on match count
        SELECT 
            c.referee_name, -- Selecting the referee name
            b.country_name, -- Selecting the country name
            COUNT(a.match_no) as match_count, -- Counting the number of matches for each referee in each country
            RANK() OVER (ORDER BY COUNT(a.match_no) DESC) as rank -- Generating rank based on match count
        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 
            c.referee_name, -- Grouping the results by referee name
            b.country_name -- Grouping the results by country name
    ) ranked -- Alias for the subquery
	-- Filtering the results to select the top-ranked referee in each country
WHERE 
    rank = 1; 

Explanation:

This query uses a window function RANK() to assign a rank to each combination of referee and country based on the count of matches. It then selects the combination with rank 1, which corresponds to the highest match count.

Go to:


PREV : Find the referees managed the number of matches.
NEXT : Find the number of matches each referee 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.