Skip to content
Intermediate SQL
Data Manipulation in SQL
Here you can access every table used in the course. To access each table, you will need to specify the soccer schema in your queries (e.g., soccer.match for the match table, and soccer.league for the league table).
Note: When using sample databases such as those that contain course data, you have read-only access. You can run queries, but cannot make any changes such as adding, deleting, or modifying the data (e.g., creating tables, views, etc.).
Take Notes
Add notes about the concepts you've learned and SQL cells with queries you want to keep.
Add your notes here
DataFrameas
df
variable
-- Add your own queries here
SELECT
l.name AS league, c.name AS country, CASE WHEN c.name IN ('England', 'Scotland', 'Wales') THEN 'Great Britain' ELSE 'Wales' END AS Region, COUNT (m.home_goal + m.away_goal) AS total_goal
FROM soccer.league AS l
LEFT JOIN soccer.country AS c
ON l.country_id = c.id
LEFT JOIN soccer.match AS m
ON l.country_id = m.country_id
GROUP BY country,league
ORDER BY total_goal DESC;
Explore Datasets
Use the match, league, and country tables to explore the data and practice your skills!
- Use the
match,league, andcountrytables to return the number of matches played in Great Britain versus elsewhere in the world.- "England", "Scotland", and "Wales" should be categorized as "Great Britain"
- All other leagues will need to be categorized as "World".
- Use the
matchandcountrytables to return the countries in which the average number of goals (home and away goals) scored are greater than the average number of goals of all matches. - In a soccer league, points are assigned to teams based on the result of a game. Here, let's assume that 3 points are awarded for a win, 1 for a tie, and 0 for a defeat. Use the
matchtable to calculate the running total of points earned by the team "Chelsea" (team id 8455) in the season "2014/2015".- The final output should have the match date, the points earned by Chelsea, and the running total.
DataFrameas
df1
variable
SELECT
c.name AS country,
AVG(m.home_goal + m.away_goal) AS avg_goal
FROM soccer.match AS m
LEFT JOIN soccer.country AS c
ON m.country_id = c.id
GROUP BY country
HAVING AVG(m.home_goal + m.away_goal) > (SELECT AVG(home_goal + away_goal) FROM soccer.match);DataFrameas
df2
variable
WITH Game_point AS (
SELECT
m.date,
t.team_long_name,
CASE WHEN m.home_goal = 3 AND m.away_goal = 3 THEN 'win'
WHEN m.home_goal = 1 AND m.away_goal = 3 THEN 'tie'
ELSE 'defeat' END AS point
FROM soccer.match AS m
LEFT JOIN soccer.team AS t
ON m.hometeam_id = t.team_api_id
WHERE t.team_api_id = 8455
AND m.season = '2014/2015'
)
SELECT
date,
point,
SUM(CASE WHEN point = 'win' THEN 3 WHEN point = 'tie' THEN 1 ELSE 0 END) OVER(ORDER BY date ASC) AS running_total
FROM Game_point
ORDER BY date ASC;