SQL Exercise: Find the players who booked most number of times
SQL soccer Database: Joins Exercise-42 with Solution
42. From the following tables, write a SQL query to count the players who booked the most number of times. Return player name, number of players who booked most number of times.
Sample table: soccer_country
Sample table: player_booked
Sample table: player_mast
Sample Solution:
SQL Code:
SELECT 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 c.player_name
having COUNT(b.*)=(
SELECT MAX(mm) FROM (
SELECT COUNT(*) mm
FROM player_booked
GROUP BY player_id) inner_result);
Sample Output:
player_name | booked -------------------+-------- NGolo Kante | 3 William Carvalho | 3 Bartosz Kapustka | 3 (3 rows)
Code Explanation:
The said query in SQL that joins three tables soccer_country, player_booked, and player_mast and groups the results by player name.
The JOIN clause joins the soccer_country and player_booked tables based on the country_id and team_id columns, and joins the result set with the table player_mast based on the player_id column.
Groups the results by player_name, so that the query can aggregate bookings by player.
Using a subquery finds the player with the highest number of bookings, and returns only that player's name and the number of bookings they have. The subquery first counts the number of bookings per player in the player_booked table, groups the result by player_id, and selects the maximum count. The outer query then filters the results to only include the player with that maximum count.
Practice Online
Sample Database: soccer

Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous SQL Exercise: Players with their teams booked in the tournament.
Next SQL Exercise: Find the number of players booked for each team.
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
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- 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