﻿ SQL: Find the number of players booked for each team

# 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

Sample Solution:

SQL Code:

``````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;
```
```

Sample Output:

```    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)
```

Code Explanation:

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.

## Practice Online

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

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

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