SQL Exercise: Matches with most stoppage time added in the 2nd half

SQL soccer Database: Joins Exercise-21 with Solution

21. From the following tables, write a SQL query to find those matches where the highest stoppage time was added in 2nd half of play. Return match number, country name, stoppage time(sec.).

Sample table: match_details

Sample table: match_mast

Sample table: soccer_country

Sample Solution:

SQL Code:

SELECT match_details.match_no, soccer_country.country_name,
match_mast.stop2_sec as "Stoppage Time(sec.)" 
FROM match_mast
JOIN match_details 
ON match_mast.match_no=match_details.match_no
JOIN soccer_country
ON match_details.team_id=soccer_country.country_id
WHERE stop2_sec IN (
SELECT MAX(stop2_sec) 
FROM match_mast);

Sample Output:

 match_no |   country_name   | Stoppage Time(sec.)
       17 | Ukraine          |                 411
       17 | Northern Ireland |                 411
(2 rows)

Code Explanation:

The said query in SQL that selects the match number, country name, and stoppage time in seconds for all matches where the stoppage time in seconds is equal to the maximum stoppage time in seconds across all matches from the tables match_mast, match_details, and soccer_country.
The JOIN clause is used to join the tables match_mast and match_details table based on the match_no column and join the soccer_country table with the results of previous join based on team_id columns of match_details table and country_id column of soccer_country table.
The WHERE clause filters the results to only show matches where the stop2_sec column in the match_mast table is equal to the maximum value of stop2_sec in the same table. The subquery inside the WHERE clause is used to get the maximum value of stop2_sec from the match_mast table.

Practice Online

Sample Database: soccer

soccer database relationship structure

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

Previous SQL Exercise: Team(s) who conceded the most goals in EURO cup 2016.
Next SQL Exercise: Matches end in a goalless draw in group stage of play.

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.

SQL: Tips of the Day

How to drop all tables from a database with one SQL query?

USE Databasename

SELECT  'DROP TABLE [' + name + '];'
FROM    sys.tables

Ref: https://bit.ly/3PIUmPL


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