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

# 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

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