w3resource
Soccer Database Exercises

SQL exercises on soccer Database: Find the venue that has seen the most goals

SQL soccer Database: Joins Exercise-25 with Solution

25. Write a query in SQL to find the venue that has seen the most goals.

Sample table: goal_details


Sample table: soccer_country


Sample table: match_mast


Sample table: soccer_venue


Sample Solution:

SQL Code:

SELECT venue_name, count(venue_name)
FROM goal_details
JOIN soccer_country
ON goal_details.team_id=soccer_country.country_id
JOIN match_mast ON goal_details.match_no=match_mast.match_no
JOIN soccer_venue ON match_mast.venue_id=soccer_venue.venue_id
GROUP BY venue_name
HAVING COUNT (venue_name)=( 
SELECT MAX(mycount) 
FROM ( 
SELECT venue_name, COUNT(venue_name) mycount
FROM goal_details
JOIN soccer_country
ON goal_details.team_id=soccer_country.country_id
JOIN match_mast ON goal_details.match_no=match_mast.match_no
JOIN soccer_venue ON match_mast.venue_id=soccer_venue.venue_id
GROUP BY venue_name) gd);

Sample Output:

   venue_name    | count
-----------------+-------
 Stade de France |    18
(1 row)

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 player and his team and how many matches he kept goal for his team.
Next: Write a query in SQL to find the oldest player to have appeared in a EURO cup 2016 match.

What is the difficulty level of this exercise?



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