SQL Exercise: Find the referees managed the number of matches
SQL soccer Database: Joins Exercise-51 with Solution
51. From the following tables, write a SQL query to find the number of matches managed by each referee. Return referee name, country name, 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;
Sample Output:
referee_name | country_name | count -------------------------+----------------+------- Damir Skomina | Slovenia | 4 Martin Atkinson | England | 3 Clement Turpin | France | 2 Jonas Eriksson | Sweden | 3 Ovidiu Hategan | Romania | 2 Sergei Karasev | Russia | 2 Cuneyt Cakir | Turkey | 3 Felix Brych | Germany | 3 Viktor Kassai | Hungary | 3 Carlos Velasco Carballo | Spain | 3 Svein Oddvar Moen | Norway | 2 Milorad Mazic | Serbia | 3 Szymon Marciniak | Poland | 3 Bjorn Kuipers | Netherlands | 3 William Collum | Scotland | 2 Mark Clattenburg | England | 4 Nicola Rizzoli | Italy | 4 Pavel Kralovec | Czech Republic | 2 (18 rows)
Code Explanation:
The given query in SQL that selects the name of referees, the name of countries, and the count of match numbers in the match_mast table for each combination of referee and country.
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 groups the results by referee name and country name using the GROUP BY clause and applies an aggregate function, count(), to count the number of match numbers for each combination of referee and country.
Relational Algebra Expression:

Relational Algebra Tree:

Practice Online
Sample Database: soccer

Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous SQL Exercise: Identify countries where referees managed most matches.
Next SQL Exercise: Find those referees who managed most of the match.
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