w3resource
Soccer Database Exercises

SQL Joins exercises on soccer Database: Find the players along with their team booked number of times in the tournament

SQL soccer Database: Joins Exercise-41 with Solution

41. Write a query in SQL to find the players along with their team booked number of times in the tournament. Show the result according to the team and number of times booked in descending order.

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)

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 number of captains who was also the goalkeeper.
Next: Write a query in SQL to find the players who booked most number of times.

What is the difficulty level of this exercise?



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