SQL Exercise: The number of matches each referee has managed
SQL soccer Database: Joins Exercise-49 with Solution
49. From the following tables, write a SQL query to count the number of matches managed by referees of each country. Return country name, number of matches.
Sample table: match_mast
Sample table: referee_mast
Sample table: soccer_country
SELECT country_name, count(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 country_name ORDER BY count(match_no) DESC;
country_name | count ----------------+------- England | 7 Italy | 4 Slovenia | 4 Spain | 3 Serbia | 3 Netherlands | 3 Turkey | 3 Germany | 3 Poland | 3 Sweden | 3 Hungary | 3 France | 2 Czech Republic | 2 Norway | 2 Russia | 2 Romania | 2 Scotland | 2 (17 rows)
The said query in SQL that selects the name of countries and the count of match numbers in the match_mast table for each 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 country name using the GROUP BY clause and applies an aggregate function, count(), to count the number of match numbers for each country. The results are then ordered in descending order of the count of match numbers using the ORDER BY clause.
Relational Algebra Expression:
Relational Algebra Tree:
Sample Database: soccer
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous SQL Exercise: List the referees with their countries for each match.
Next SQL Exercise: Identify countries where referees managed most matches.
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
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- 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