SQL Exercise: Find the teams played the first match of EURO cup 2016
1. From the following table, write a SQL query to find out which teams played the first match of the 2016 Euro Cup. Return match number, country name.
Sample table: match_details
match_no | play_stage | team_id | win_lose | decided_by | goal_score | penalty_score | ass_ref | player_gk
----------+------------+---------+----------+------------+------------+---------------+---------+-----------
1 | G | 1207 | W | N | 2 | | 80016 | 160140
1 | G | 1216 | L | N | 1 | | 80020 | 160348
2 | G | 1201 | L | N | 0 | | 80003 | 160001
2 | G | 1221 | W | N | 1 | | 80023 | 160463
3 | G | 1224 | W | N | 2 | | 80031 | 160532
3 | G | 1218 | L | N | 1 | | 80025 | 160392
4 | G | 1206 | D | N | 1 | | 80008 | 160117
4 | G | 1217 | D | N | 1 | | 80019 | 160369
5 | G | 1222 | L | N | 0 | | 80011 | 160486
..........
51 | F | 1207 | L | N | 0 | | 80007 | 160140
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 Solution:
SQL Code:
-- This SQL query retrieves the match number and country name from the 'match_details' and 'soccer_country' tables
-- where the team ID in 'match_details' matches the country ID in 'soccer_country' and the match number is 1.
SELECT match_no, country_name
-- Selects the match number and country name columns.
FROM match_details a, soccer_country b
-- Specifies the tables involved in the query, using aliases 'a' for 'match_details' and 'b' for 'soccer_country'.
WHERE a.team_id = b.country_id
-- Connects rows in 'match_details' where 'team_id' matches 'country_id' in 'soccer_country'.
AND a.match_no = 1;
-- Further filters rows where the match number is 1.
Sample Output:
match_no | country_name
----------+--------------
1 | France
1 | Romania
(2 rows)
Code Explanation:
The said query in SQL that retrieves the match number and country name from the tables 'match_details' alias as a and 'soccer_country' alias as b where the team ID in the 'match_details' table must matche the country ID in the 'soccer_country' table, and the match number must be 1.
Alternative Solution:
Using INNER JOIN:
SELECT match_no, country_name
FROM match_details a
JOIN soccer_country b ON a.team_id = b.country_id
WHERE a.match_no = 1;
Explanation:
This query uses an INNER JOIN to combine the match_details and soccer_country tables based on the condition that team_id matches country_id. It then filters the results to only include rows where match_no is 1.
Relational Algebra Expression:
Relational Algebra Tree:
Go to:
PREV : SQL SUBQUERIES Exercises on Soccer Database
NEXT : Find the winner of EURO cup 2016.
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.
