w3resource

SQL Exercise: Find the highest individual scorer in EURO cup 2016

SQL soccer Database: Joins Exercise-4 with Solution

4. From the following table, write a SQL query to find out who scored the most goals in the 2016 Euro Cup. Return player name, country name and highest individual scorer.

Sample table: goal_details


Sample table: player_mast


Sample table: soccer_country


Sample Solution:

SQL Code:

SELECT player_name,country_name,count(player_name)
FROM goal_details gd
JOIN player_mast pm ON gd.player_id =pm.player_id
JOIN soccer_country sc ON pm.team_id = sc.country_id
GROUP BY country_name,player_name HAVING COUNT(player_name) >= ALL
  (SELECT COUNT(player_name)
   FROM goal_details gd
   JOIN player_mast pm ON gd.player_id =pm.player_id
   JOIN soccer_country sc ON pm.team_id = sc.country_id
   GROUP BY country_name,player_name);

Sample Output:

     player_name     | country_name | count
--------------------+--------------+-------
 Antoine Griezmann  | France       |     6
(1 row)

Code Explanation:

The said query in SQL that selects the player name, country name, and the count of goals scored by each player, but only for players who have scored the most goals in their respective countries.
The subquery uses to determine the maximum number of goals scored by any player in each country. The outer query then filters the results to include only the players who have scored at least as many goals as the maximum for their country.
The JOIN clause joins the goal_details, player_mast, and soccer_country tables using their respective IDs.
The results set then grouped by the country name and player name.

Practice Online


Sample Database: soccer

soccer database relationship structure

Query Visualization:

Duration:

Query visualization of Find the highest individual scorer in EURO cup 2016 - Duration

Rows:

Query visualization of Find the highest individual scorer in EURO cup 2016 - Rows

Cost:

Query visualization of Find the highest individual scorer in EURO cup 2016 - Cost

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

Previous SQL Exercise: Goals scored by each player during normal play.
Next SQL Exercise: Find the scorer of only goal along with his country.

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