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
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;
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)
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.
Sample Database: soccer
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.
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