w3resource
Soccer Database Exercises

SQL Subqueries exercises on soccer Database: Find the match no. and teams who played the match where highest number of penalty shots had been taken

SQL soccer Database: Subqueries Exercise-28 with Solution

Write a query in SQL to find the match no. and teams who played the match where highest number of penalty shots had been taken.

Sample table: penalty_shootout

Sample Solution:

SQL Code:

SELECT b.match_no,
       a.country_name
FROM penalty_shootout b,
     soccer_country a
WHERE b.team_id=a.country_id
  AND match_no=
    (SELECT match_no
     FROM penalty_shootout
     GROUP BY match_no
     HAVING COUNT(*)=
       (SELECT MAX(shots)
        FROM
          (SELECT COUNT(*) shots
           FROM penalty_shootout
           GROUP BY match_no) inner_result))
GROUP BY b.match_no,
         a.country_name;

Sample Output:

 match_no | country_name
----------+--------------
       47 | Germany
       47 | Italy
(2 rows)

Query Visualization:

Duration:

Query visualization of Find the match no. and teams who played the match where highest number of penalty shots had been taken - Duration

Rows:

Query visualization of Find the match no. and teams who played the match where highest number of penalty shots had been taken - Rows

Cost:

Query visualization of Find the match no. and teams who played the match where highest number of penalty shots had been taken - Cost

SQL Code Editor:

Practice Online


Sample Database: soccer

soccer database relationship structure

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

Previous: Write a query in SQL to find the match no. where highest number of penalty shots taken.
Next: Write a query in SQL to find the player of portugal who taken the 7th kick against poland.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming