SQL Exercise: Find the number of players booked for each team
SQL soccer Database: Joins Exercise-43 with Solution
43. From the following tables, write a SQL query to find out how many players each team has booked. Return country name, number of players booked.
Sample table: soccer_country
Sample table: player_booked
SELECT a.country_name,COUNT(b.*) Booked FROM soccer_country a JOIN player_booked b ON a.country_id=b.team_id GROUP BY a.country_name ORDER BY Booked DESC;
country_name | booked ---------------------+-------- Italy | 16 France | 13 Portugal | 13 Iceland | 12 Hungary | 12 Germany | 11 Wales | 11 Romania | 10 Poland | 10 Albania | 10 Belgium | 9 Slovakia | 9 Republic of Ireland | 9 Croatia | 8 Austria | 7 Turkey | 7 Northern Ireland | 6 Ukraine | 5 Czech Republic | 5 Spain | 5 Switzerland | 5 England | 3 Sweden | 3 Russia | 2 (24 rows)
The said query in SQL that selects the country name and counts the number of bookings for each country from the tables 'soccer_country' and 'player_booked'.
The JOIN clause joins the 'soccer_country' table aliased as "a" with the 'player_booked' table aliased as "b" using the country ID from "a" and the team ID from "b".
It then groups the results by country name and orders them in descending order based on the number of bookings so that the country with the most bookings appears first.
Sample Database: soccer
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous SQL Exercise: Find the players who booked most number of times.
Next SQL Exercise: Find the most number of cards shown in the 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