w3resource

SQL Exercise: Team(s) who conceded the most goals in EURO cup 2016

SQL soccer Database: Joins Exercise-20 with Solution

20. From the following tables, write a SQL query to find the team(s) who conceded the most goals in EURO cup 2016. Return country name, team group and match played.

Sample table: soccer_team


Sample table: soccer_country


Sample Solution:

SQL Code:

SELECT country_name,team_group,match_played,
won,lost,goal_for,goal_agnst
FROM soccer_team 
JOIN soccer_country 
ON soccer_team.team_id=soccer_country.country_id
WHERE goal_agnst=(
SELECT MAX(goal_agnst) 
FROM soccer_team);

Sample Output:

 country_name | team_group | match_played | won | lost | goal_for | goal_agnst
--------------+------------+--------------+-----+------+----------+------------
 Russia       | B          |            3 |   0 |    2 |        2 |          6
(1 row)

Code Explanation:

The said query in SQL that selects the country name, team group, number of matches played, number of matches won, number of matches lost, number of goals scored for, and number of goals scored against for all teams in the soccer_team table, that have the highest number of goals scored against them.
The JOIN clause joins the tables using the team_id and country_id columns, which are primary keys in the soccer_team and soccer_country tables respectively.
The WHERE clause filters the results to only show teams where the goal_agnst column in the soccer_team table is equal to the maximum value of goal_agnst in the same table. The subquery inside the WHERE clause is used to get the maximum value of goal_agnst from the soccer_team 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: Match where no stoppage time added in 1st half of play.
Next SQL Exercise: Matches with most stoppage time added in the 2nd half.

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