w3resource
Soccer Database Exercises

SQL Joins exercises on soccer Database: Find the countries from where the assistant referees assist most of the matches

SQL soccer Database: Joins Exercise-47 with Solution

Write a query in SQL to find the countries from where the assistant referees assist most of the matches.

Sample table: match_details


Sample table: asst_referee_mast


Sample table: soccer_country


Sample Solution:

SQL Code:

SELECT country_name,
       count(DISTINCT match_no)
FROM match_details a
JOIN asst_referee_mast c ON a.ass_ref=c.ass_ref_id
JOIN soccer_country b ON c.country_id=b.country_id
GROUP BY country_name
HAVING count(DISTINCT match_no)=
  (SELECT max(mm)
   FROM
     (SELECT count(DISTINCT match_no) mm
      FROM match_details a
      JOIN asst_referee_mast c ON a.ass_ref=c.ass_ref_id
      JOIN soccer_country b ON c.country_id=b.country_id
      GROUP BY country_name) hh);

Sample Output:

 country_name | count
--------------+-------
 England      |     7
(1 row)

Practice Online


Sample Database: soccer

soccer database relationship structure

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a query in SQL to find the assistant referees of each countries assists the number of matches.
Next: Write a query in SQL to list the name of referees with their countries for each match.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming