SQL Exercise: Number of countries participated in the EURO cup 2016
2. From the following table, write a SQL query to count the number of countries that participated in the 2016-EURO Cup.
Sample table: player_mast
player_id | team_id | jersey_no | player_name | posi_to_play | dt_of_bir | age | playing_club
-----------+---------+-----------+-------------------------+--------------+------------+-----+---------------------
160001 | 1201 | 1 | Etrit Berisha | GK | 1989-03-10 | 27 | Lazio
160008 | 1201 | 2 | Andi Lila | DF | 1986-02-12 | 30 | Giannina
160016 | 1201 | 3 | Ermir Lenjani | MF | 1989-08-05 | 26 | Nantes
160007 | 1201 | 4 | Elseid Hysaj | DF | 1994-02-20 | 22 | Napoli
160013 | 1201 | 5 | Lorik Cana | MF | 1983-07-27 | 32 | Nantes
160010 | 1201 | 6 | Frederic Veseli | DF | 1992-11-20 | 23 | Lugano
160004 | 1201 | 7 | Ansi Agolli | DF | 1982-10-11 | 33 | Qarabag
160012 | 1201 | 8 | Migjen Basha | MF | 1987-01-05 | 29 | Como
160017 | 1201 | 9 | Ledian Memushaj | MF | 1986-12-17 | 29 | Pescara
......
160548 | 1224 | 23 | Simon Church | FD | 1988-12-10 | 27 | MK Dons
Sample Solution:
-- This SQL query calculates the count of distinct 'team_id' values in the 'player_mast' table.
SELECT COUNT(DISTINCT team_id)
-- COUNT(DISTINCT team_id) counts the number of unique 'team_id' values in the result set.
FROM player_mast;
-- 'player_mast' is the name of the table from which the distinct 'team_id' count is being calculated.
Sample Output:
count
-------
24
(1 row)
Code Explanation:
The said query in SQL that counts the number of distinct team_id values in the player_mast table. The result of the query will be a single value that represents the number of different teams that are represented in the player_mast table.
The DISTINCT keyword is used to eliminate duplicate values before counting. This means that if there are multiple records in the player_mast table with the same team_id, they will only be counted as one for the purpose of this query.
Relational Algebra Expression:
Relational Algebra Tree:
Go to:
PREV : Find the number of venues for EURO cup 2016.
NEXT : Number of goals in EURO cup 2016 in normal schedule.
Practice Online
Sample Database: soccer
Query Visualization:
Duration:
Rows:
Cost:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
