w3resource

SQL Exercise: Find the yellow cards received by each country

SQL soccer Database: Joins Exercise-17 with Solution

17. From the following tables, write a SQL query to count the number of yellow cards each country has received. Return country name and number of yellow cards.

Sample table: soccer_country


Sample table: player_booked


Sample Solution:

SQL Code:

SELECT country_name, COUNT(*)
FROM soccer_country 
JOIN player_booked
ON soccer_country.country_id=player_booked.team_id
GROUP BY country_name
ORDER BY COUNT(*) DESC;

Sample Output:

    country_name     | count
---------------------+-------
 Italy               |    16
 France              |    13
 Portugal            |    13
 Hungary             |    12
 Iceland             |    12
 Wales               |    11
 Germany             |    11
 Romania             |    10
 Albania             |    10
 Poland              |    10
 Republic of Ireland |     9
 Slovakia            |     9
 Belgium             |     9
 Croatia             |     8
 Turkey              |     7
 Austria             |     7
 Northern Ireland    |     6
 Czech Republic      |     5
 Spain               |     5
 Ukraine             |     5
 Switzerland         |     5
 England             |     3
 Sweden              |     3
 Russia              |     2
(24 rows)

Code Explanation:

The said query in SQL that selects a list of all countries along with the count of player bookings for each country from the tables 'soccer_country' and 'player_booked'.
The JOIN keyword with ON clause is used to specify the join condition where the country ID from the 'soccer_country' table matches the team ID from the 'player_booked' table.
The GROUP BY clause groups the results by country name. This means that the COUNT(*) function will count the number of player bookings for each country.
The query is using the ORDER BY clause to sort the results in descending order based on the count of player bookings.

Relational Algebra Expression:

Relational Algebra Expression: Find the yellow cards received by each country.

Relational Algebra Tree:

Relational Algebra Tree: Find the yellow cards received by each country.

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: Teams that scored only one goal to the torunament.
Next SQL Exercise: Find the venue with number of goals that has seen.

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