SQL Exercise: Name and country of referee who managed the final game
9. From the following tables, write a SQL query to find the referee who managed the final match. Return referee name, country name.
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
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
Sample table: referee_mast
referee_id | referee_name | country_id
------------+-------------------------+------------
70001 | Damir Skomina | 1225
70002 | Martin Atkinson | 1206
70003 | Felix Brych | 1208
70004 | Cuneyt Cakir | 1222
70005 | Mark Clattenburg | 1206
70006 | Jonas Eriksson | 1220
70007 | Viktor Kassai | 1209
70008 | Bjorn Kuipers | 1226
70009 | Szymon Marciniak | 1213
.......
70018 | Clement Turpin | 1207
Sample Solution:
SQL Code:
-- Selecting referee_name and country_name
SELECT b.referee_name, c.country_name
-- From clause with NATURAL JOIN between match_mast, referee_mast, and soccer_country
FROM match_mast a
NATURAL JOIN referee_mast b
NATURAL JOIN soccer_country c
-- Filtering the results to include only matches in the final play stage
WHERE play_stage = 'F';
Sample Output:
referee_name | country_name ------------------+-------------- Mark Clattenburg | England (1 row)
Code Explanation:
The provided query in SQL that retrieves information about the referee and country for matches in the final stage from the tables match_mast, referee_mast, and soccer_country.
The NATURAL JOIN keyword is used to join the tables based on columns with the same name in the tables match_mast, referee_mast, and soccer_country.
The condition filters the rows only for matches in the final stage, where play_stage is equal to 'F'.
Relational Algebra Expression:
Relational Algebra Tree:
Go to:
PREV : Name and country of referee who managed the first game.
NEXT : Referee who assisted in the opening match.
Practice Online
Sample Database: soccer
Query Visualization:
Duration:
Rows:
Cost:
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.
