SQL Exercise: Find those referees who managed most of the match
SQL soccer Database: Joins Exercise-52 with Solution
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
Sample table: referee_mast
Sample table: soccer_country
Sample Solution:
SQL Code:
SELECT c.referee_name,
b.country_name,
count(a.match_no)
FROM match_mast a
JOIN referee_mast c ON a.referee_id=c.referee_id
JOIN soccer_country b ON c.country_id=b.country_id
GROUP BY c.referee_name,
b.country_name
HAVING count(a.match_no) =
(SELECT max(mm)
FROM
(SELECT count(a.match_no) mm
FROM match_mast a
JOIN referee_mast c ON a.referee_id=c.referee_id
JOIN soccer_country b ON c.country_id=b.country_id
GROUP BY c.referee_name,
b.country_name) hh);
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.
Practice Online
Sample Database: soccer

Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous SQL Exercise: Find the referees managed the number of matches.
Next SQL Exercise: Find the number of matches each referee managed.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
SQL: Tips of the Day
What is the best way to paginate results in SQL Server?
SELECT * FROM ( SELECT ROW_NUMBER() OVER ( ORDER BY OrderDate ) AS RowNum, * FROM Orders WHERE OrderDate >= '1980-01-01' ) AS RowConstrainedResult WHERE RowNum >= 1 AND RowNum < 20 ORDER BY RowNum
Database: SQL Server
Ref: https://bit.ly/3MGrNlk
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook