w3resource

SQL Exercise: 2nd highest stoppage time in the 2nd half of matches

SQL soccer Database: Joins Exercise-23 with Solution

23. From the following tables, write a SQL query to find those match(s) where the second highest amount of stoppage time was added in the second half of the match. Return match number, country name and stoppage time.

Sample table: match_mast


Sample table: match_details


Sample table: soccer_country


Sample Solution:

SQL Code:

SELECT a.match_no, c.country_name, a.stop2_sec
FROM match_mast a
JOIN match_details b ON a.match_no=b.match_no
JOIN soccer_country c ON b.team_id=c.country_id
WHERE (2-1) = (
SELECT COUNT(DISTINCT(b.stop2_sec))
FROM match_mast b
WHERE b.stop2_sec > a.stop2_sec);

Sample Output:

 match_no | country_name | stop2_sec
----------+--------------+-----------
       15 | France       |       374
       15 | Albania      |       374
(2 rows)

Code Explanation:

The said query in SQL that retrieves data from the tables match_mast, match_details, and soccer_country. It selects the match number, country name, and stop2_sec columns from the respective tables.
The JOIN clause joins the table match_mast on match_no with match_details, and match_details on team_id with soccer_country.
Filters the data based on the condition that the difference between the number 2 and the number 1 is equal to the count of distinct stop2_sec values in match_mast table that are greater than the stop2_sec value in the current row of 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: Matches end in a goalless draw in group stage of play.
Next SQL Exercise: How many games the goalkeeper played for his team?

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

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