w3resource

SQL Exercise: Find the most number of cards shown in the matches

SQL soccer Database: Joins Exercise-44 with Solution

44. From the following tables, write a SQL query to find the matches in which the most cards are displayed. Return match number, number of cards shown.

Sample table: player_booked


Sample Solution:

SQL Code:

SELECT match_no, Booked FROM (
SELECT match_no,COUNT(*) Booked 
FROM player_booked  
GROUP BY match_no) M1 where Booked=(
SELECT MAX(MX1) 
FROM (SELECT match_no,COUNT(*) MX1 
FROM player_booked  
GROUP BY match_no) M2);

Sample Output:

 match_no | booked 
----------+--------
       51 |     10
(1 row)

Code Explanation:

The said query in SQL that selects the match number and the number of players booked for each match from the table 'player_booked'.
The subquery counts the number of players booked for each match and groups them by the match number.
The outer query selects only those rows where the number of players booked is equal to the maximum number of players booked across all matches. This is done by using a subquery to find the maximum number of players booked (MX1) and then selecting only those rows where the number of players booked is equal to MX1.

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: Find the number of players booked for each team.
Next SQL Exercise: Each match assistant referee and their country.

Test your Programming skills with w3resource's quiz.

What is the difficulty level of this exercise?



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