﻿ SQL: Find the venue with number of goals that has seen

# SQL Exercise: Find the venue with number of goals that has seen

## SQL soccer Database: Joins Exercise-18 with Solution

18. From the following tables, write a SQL query to count the number of goals that have been seen. Return venue name and number of goals.

Sample table: soccer_country

Sample table: goal_details

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
ORDER BY COUNT(venue_name) DESC;
```
```

Sample Output:

```       venue_name        | count
-------------------------+-------
Parc des Princes        |     5
(10 rows)
```

Code Explanation:

The said query in SQL that selects a list of all venues along with the count of goals scored at each venue from the tables 'goal_details', 'soccer_country', 'match_mast', and 'soccer_venue'.
Using multiple JOIN operations the 'goal_details' and 'soccer_country' tables are joined based on the team ID and country ID columns, the 'goal_details' and 'match_mast' tables are joined based on the match number column,
the 'match_mast' and 'soccer_venue' tables are joined based on the venue ID column.
The GROUP BY clause groups the results by venue name. This means that the COUNT(venue_name) function will count the number of goals scored at each venue.
The ORDER BY clause sorts the results in descending order based on the count of goals scored at each venue. The venues with the most goals scored will be listed first.

Relational Algebra Expression:

Relational Algebra Tree:

## Practice Online

Sample Database: soccer

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

Previous: SQL Exercise Find the yellow cards received by each country.
Next SQL Exercise: Match where no stoppage time added in 1st half of play.

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

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