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,
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,

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 Expression: Find the referees managed the number of matches.

Relational Algebra Tree:

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

Practice Online

Sample Database: soccer

soccer database relationship structure

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.

Follow us on Facebook and Twitter for latest update.

SQL: Tips of the Day

How to drop all tables from a database with one SQL query?

USE Databasename

SELECT  'DROP TABLE [' + name + '];'
FROM    sys.tables

Ref: https://bit.ly/3PIUmPL


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