w3resource

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


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

 country_id | country_abbr |    country_name
------------+--------------+---------------------
       1201 | ALB          | Albania
       1202 | AUT          | Austria
       1203 | BEL          | Belgium
       1204 | CRO          | Croatia
       1205 | CZE          | Czech Republic
       1206 | ENG          | England
       1207 | FRA          | France
       1208 | GER          | Germany
       1209 | HUN          | Hungary
........
       1229 | NOR          | Norway

View the table

Sample table: goal_details

 goal_id | match_no | player_id | team_id | goal_time | goal_type | play_stage | goal_schedule | goal_half
---------+----------+-----------+---------+-----------+-----------+------------+---------------+-----------
       1 |        1 |    160159 |    1207 |        57 | N         | G          | NT            |         2
       2 |        1 |    160368 |    1216 |        65 | P         | G          | NT            |         2
       3 |        1 |    160154 |    1207 |        89 | N         | G          | NT            |         2
       4 |        2 |    160470 |    1221 |         5 | N         | G          | NT            |         1
       5 |        3 |    160547 |    1224 |        10 | N         | G          | NT            |         1
       6 |        3 |    160403 |    1218 |        61 | N         | G          | NT            |         2
       7 |        3 |    160550 |    1224 |        81 | N         | G          | NT            |         2
       8 |        4 |    160128 |    1206 |        73 | N         | G          | NT            |         2
       9 |        4 |    160373 |    1217 |        93 | N         | G          | ST            |         2
........
     108 |       51 |    160319 |    1214 |       109 | N         | F          | ET            |         2

View the table

Sample table: match_mast

 match_no | play_stage | play_date  | results | decided_by | goal_score | venue_id | referee_id | audence | plr_of_match | stop1_sec | stop2_sec
----------+------------+------------+---------+------------+------------+----------+------------+---------+--------------+-----------+-----------
        1 | G          | 2016-06-11 | WIN     | N          | 2-1        |    20008 |      70007 |   75113 |       160154 |       131 |       242
        2 | G          | 2016-06-11 | WIN     | N          | 0-1        |    20002 |      70012 |   33805 |       160476 |        61 |       182
        3 | G          | 2016-06-11 | WIN     | N          | 2-1        |    20001 |      70017 |   37831 |       160540 |        64 |       268
        4 | G          | 2016-06-12 | DRAW    | N          | 1-1        |    20005 |      70011 |   62343 |       160128 |         0 |       185
        5 | G          | 2016-06-12 | WIN     | N          | 0-1        |    20007 |      70006 |   43842 |       160084 |       125 |       325
        6 | G          | 2016-06-12 | WIN     | N          | 1-0        |    20006 |      70014 |   33742 |       160291 |         2 |       246
        7 | G          | 2016-06-13 | WIN     | N          | 2-0        |    20003 |      70002 |   43035 |       160176 |        89 |       188
        8 | G          | 2016-06-13 | WIN     | N          | 1-0        |    20010 |      70009 |   29400 |       160429 |       360 |       182
        9 | G          | 2016-06-13 | DRAW    | N          | 1-1        |    20008 |      70010 |   73419 |       160335 |        67 |       194
.........
       51 | F          | 2016-07-11 | WIN     | N          | 1-0        |    20008 |      70005 |   75868 |       160307 |       161 |       181

View the table

Sample table:soccer_venue

 venue_id |       venue_name        | city_id | aud_capacity
----------+-------------------------+---------+--------------
    20001 | Stade de Bordeaux       |   10003 |        42115
    20002 | Stade Bollaert-Delelis  |   10004 |        38223
    20003 | Stade Pierre Mauroy     |   10005 |        49822
    20004 | Stade de Lyon           |   10006 |        58585
    20005 | Stade VElodrome         |   10007 |        64354
    20006 | Stade de Nice           |   10008 |        35624
    20007 | Parc des Princes        |   10001 |        47294
    20008 | Stade de France         |   10002 |        80100
    20009 | Stade Geoffroy Guichard |   10009 |        42000
    20010 | Stadium de Toulouse     |   10010 |        33150

Sample Solution:

SQL Code:

-- Selecting venue_name and the count of goals scored in each venue
SELECT venue_name, count(venue_name)
-- From clause with JOINs between goal_details, soccer_country, match_mast, and soccer_venue
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
-- Grouping the results by venue_name to count goals scored in each venue
GROUP BY venue_name
-- Ordering the results by the count of goals scored in each venue in descending order
ORDER BY COUNT(venue_name) DESC;

Sample Output:

       venue_name        | count
-------------------------+-------
 Stade de France         |    18
 Stade de Lyon           |    16
 Stade Pierre Mauroy     |    13
 Stade de Bordeaux       |    13
 Stade VElodrome         |    11
 Stadium de Toulouse     |     9
 Stade de Nice           |     8
 Stade Geoffroy Guichard |     8
 Stade Bollaert-Delelis  |     7
 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.

Alternative Solutions:

Subquery with EXISTS:

-- Selecting venue_name and the count of goals scored in each venue
SELECT v.venue_name, COUNT(gd.goal_id) as num_goals
-- From clause with JOINs between soccer_venue, match_mast, and goal_details
FROM soccer_venue v
JOIN match_mast m ON v.venue_id = m.venue_id
JOIN goal_details gd ON m.match_no = gd.match_no
-- Where clause with EXISTS subquery to filter results for matches with at least one goal
WHERE EXISTS (
    -- Subquery to check if there is at least one goal_details record for the match
    SELECT 1
    FROM goal_details gd2
    WHERE gd2.match_no = gd.match_no
)
-- Grouping the results by venue_name to count goals scored in each venue
GROUP BY v.venue_name
-- Ordering the results by the count of goals scored in each venue in descending order
ORDER BY num_goals DESC;

Explanation:

This query uses an EXISTS subquery to check if there's at least one record in goal_details for each match. It joins soccer_venue, match_mast, and goal_details, and then uses the EXISTS clause to filter the results.

Using Inner Join with Goal_Details:

-- Selecting venue_name and the count of goals scored in each venue
SELECT v.venue_name, COUNT(gd.goal_id) as num_goals
-- From clause with JOINs between soccer_venue, match_mast, and goal_details
FROM soccer_venue v
JOIN match_mast m ON v.venue_id = m.venue_id
JOIN goal_details gd ON m.match_no = gd.match_no
-- Grouping the results by venue_name to count goals scored in each venue
GROUP BY v.venue_name
-- Ordering the results by the count of goals scored in each venue in descending order
ORDER BY num_goals DESC;

Explanation:

This query directly joins all the necessary tables (soccer_venue, match_mast, goal_details, and soccer_country) using inner joins and then counts the goals.

Using a Subquery in the WHERE Clause:

-- Selecting venue_name and the count of goals scored in each venue
SELECT v.venue_name, COUNT(gd.goal_id) as num_goals
-- From clause with JOINs between soccer_venue, match_mast, and goal_details
FROM soccer_venue v
JOIN match_mast m ON v.venue_id = m.venue_id
JOIN goal_details gd ON m.match_no = gd.match_no
-- Where clause to filter results for matches that have at least one goal
WHERE m.match_no IN (
    -- Subquery to select distinct match_no values with at least one goal_details record
    SELECT DISTINCT gd2.match_no
    FROM goal_details gd2
)
-- Grouping the results by venue_name to count goals scored in each venue
GROUP BY v.venue_name
-- Ordering the results by the count of goals scored in each venue in descending order
ORDER BY num_goals DESC;

Explanation:

This query uses a subquery in the WHERE clause to filter the matches. It first selects distinct match numbers from goal_details and then checks if the match number is in the list of matches for each venue. This way, only venues with at least one goal are included in the result.

Relational Algebra Expression:

Relational Algebra Expression: Find the venue with number of goals that has seen.


Relational Algebra Tree:

Relational Algebra Tree: Find the venue with number of goals that has seen.


Go to:


PREV : Find the yellow cards received by each country.
NEXT : Match where no stoppage time added in 1st half of play.


Practice Online



Sample Database: soccer

soccer database relationship structure.


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

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.