w3resource

SQL Exercise: Players with their teams booked in the tournament

SQL soccer Database: Joins Exercise-41 with Solution

41. From the following tables, write a SQL query to find out how many times a player had been booked in the tournament. Show the result according to the team and number of times booked in descending order. Return country name, player name, and booked number of times.

Sample table: soccer_country


Sample table: player_booked


Sample table: player_mast


Sample Solution:

SQL Code:

SELECT a.country_name,c.player_name,COUNT(b.*) Booked 
FROM soccer_country a
JOIN player_booked b ON a.country_id=b.team_id
JOIN player_mast c ON b.player_id=c.player_id
GROUP BY a.country_name,c.player_name
ORDER BY a.country_name,Booked DESC;

Sample Output:

    country_name     |       player_name       | booked
---------------------+-------------------------+--------
 Albania             | Lorik Cana              |      2
 Albania             | Burim Kukeli            |      2
 Albania             | Ledian Memushaj         |      1
 Albania             | Mergim Mavraj           |      1
 Albania             | Migjen Basha            |      1
 Albania             | Elseid Hysaj            |      1
 Albania             | Amir Abrashi            |      1
 Albania             | Ergys Kace              |      1
 Austria             | Aleksandar Dragovic     |      2
 Austria             | Marc Janko              |      1
 Austria             | Martin Harnik           |      1
 Austria             | Alessandro Schopf       |      1
 Austria             | Christian Fuchs         |      1
 Austria             | Martin Hinteregger      |      1
 Belgium             | Thomas Vermaelen        |      2
 Belgium             | Marouane Fellaini       |      2
 Belgium             | Michy Batshuayi         |      1
 Belgium             | Thomas Meunier          |      1
 Belgium             | Axel Witsel             |      1
 Belgium             | Toby Alderweireld       |      1
 Belgium             | Jan Vertonghen          |      1
 Croatia             | Milan Badelj            |      1
 Croatia             | Sime Vrsaljko           |      1
 Croatia             | Marko Rog               |      1
 Croatia             | Marcelo Brozovic        |      1
 Croatia             | Ivan PeriSic            |      1
 Croatia             | Ivan Strinic            |      1
 Croatia             | Darijo Srna             |      1
 Croatia             | Domagoj Vida            |      1
--------
 Turkey              | Hakan Balta             |      2
 Turkey              | Cenk Tosun              |      1
 Turkey              | Volkan Sen              |      1
 Turkey              | Burak Yilmaz            |      1
 Turkey              | Ozan Tufan              |      1
 Turkey              | Ismail Koybasi          |      1
 Ukraine             | Ruslan Rotan            |      1
 Ukraine             | Olexandr Kucher         |      1
 Ukraine             | Yevhen Konoplyanka      |      1
 Ukraine             | Serhiy Sydorchuk        |      1
 Ukraine             | Yevhen Seleznyov        |      1
 Wales               | Ben Davies              |      2
 Wales               | Aaron Ramsey            |      2
 Wales               | James Chester           |      2
 Wales               | Chris Gunter            |      1
 Wales               | Sam Vokes               |      1
 Wales               | Joe Allen               |      1
 Wales               | Neil Taylor             |      1
 Wales               | Gareth Bale             |      1
(167 rows)

Code Explanation:

The provided query in SQL that retrieves the number of bookings made by each player of each country in soccer matches.
The JOIN clause joins the soccer_country aliased as 'a' and player_booked aliased as 'b' based on the country_id and team_id columns and join the player_mast table aliased as 'c' with the result set based on the player_id column.
The GROUP BY clause is used to group the result set by country and player.
The ORDER BY clause is used to sort the result set by country name and the number of bookings in descending order.

Practice Online


Sample Database: soccer

soccer database relationship structure

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

Previous SQL Exercise: Find the number of captains who were also goalkeepers.
Next SQL Exercise: Find the players who booked most number of times.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

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