代写BA,DS和A&F专业的R和python作业请私信
What is your favorite team?
The European Soccer Database contains data about 12,800 matches from 11 countries played between 2011-2015! Throughout this course, you will be shown filtered versions of the tables in this database in order to better explore their contents.
In this exercise, you will identify matches played between FC Schalke 04 and FC Bayern Munich. There are 2 teams identified in each match in the hometeam_id
and awayteam_id
columns, available to you in the filtered matches_germany
table. ID can join to the team_api_id
column in the teams_germany
table, but you cannot perform a join on both at the same time.
However, you can perform this operation using a CASE
statement once you’ve identified the team_api_id
associated with each team!
Instruction 1
teams_germany
table.IN
, giving you the team_api_IDs
needed for the next step.SELECT
-- Select the team long name and team API id
team_long_name,
team_api_id
FROM teams_germany
-- Only include FC Schalke 04 and FC Bayern Munich
WHERE
team_long_name IN ('FC Schalke 04',
'FC Bayern Munich');
Instruction 2
CASE
statement that identifies whether a match in Germany included FC Bayern Munich
, FC Schalke 04
, or neither as the home team.CASE
statement alias, home_team
.-- Identify the home team as Bayern Munich, Schalke 04, or neither
SELECT
CASE WHEN hometeam_id = 9789 THEN 'FC Schalke 04'
WHen hometeam_id = 9823 THEN 'FC Bayern Munich'
ELSE 'Other' END AS home_team,
COUNT(id) AS total_matches
FROM matches_germany
-- Group by the CASE statement alias
GROUP BY home_team;
Barcelona is considered one of the strongest teams in Spain’s soccer league.
In this exercise, you will be creating a list of matches in the 2011/2012 season where Barcelona was the home team. You will do this using a CASE statement that compares the values of two columns to create a new group – wins, losses, and ties.
In 3 steps, you will build a query that identifies a match’s winner, identifies the identity of the opponent, and finally filters for Barcelona as the home team. Completing a query in this order will allow you to watch your results take shape with each new piece of information.
The matches_spain
table currently contains Barcelona’s matches from the 2011/2012 season, and has two key columns, hometeam_id
and awayteam_id
, that can be joined with the teams_spain
table. However, you can only join teams_spain
to one column at a time.
Instruction 1
date
of the match and create a CASE
statement to identify matches as home wins, home losses, or ties.SELECT
-- Select the date of the match
date,
-- Identify home wins, losses, or ties
CASE WHEN home_goal > away_goal THEN 'Home win!'
WHEN home_goal < away_goal THEN 'Home loss :('
ELSE 'Tie' END AS outcome
FROM matches_spain;
Instruction 2:
teams_spain
table team_api_id
column to the matches_spain
table awayteam_id
. This allows us to retrieve the away team’s identity.team_long_name
from teams_spain
as opponent
and complete the CASE
statement from Step 1.SELECT
m.date,
--Select the team long name column and call it 'opponent'
t.team_long_name AS opponent,
-- Complete the CASE statement with an alias
CASE WHEN m.home_goal > away_goal THEN 'Home win!'
WHEN m.home_goal < away_goal THEN 'Home loss :('
ELSE 'Tie' END AS outcome
FROM matches_spain AS m
-- Left join teams_spain onto matches_spain
LEFT JOIN teams_spain AS t
ON m.awayteam_id = t.team_api_id;
Instruction 3
CASE
statement as the previous steps.8634
).SELECT
m.date,
t.team_long_name AS opponent,
-- Complete the CASE statement with an alias
CASE WHEN m.home_goal > away_goal THEN 'Barcelona win!'
WHEN m.home_goal < away_goal THEN 'Barcelona loss :('
ELSE 'Tie' END AS outcome
FROM matches_spain AS m
LEFT JOIN teams_spain AS t
ON m.awayteam_id = t.team_api_id
-- Filter for Barcelona as the home team
WHERE m.hometeam_id = 8634;
Instruction
CASE
statement to identify Barcelona’s away team games (id = 8634)
as wins, losses, or ties.teams_spain
table team_api_id
column on the matches_spain
table hometeam_id
column. This retrieves the identity of the home team opponent.-- Select matches where Barcelona was the away team
SELECT
m.date,
t.team_long_name AS opponent,
CASE WHEN m.home_goal < m.away_goal ThEN'Barcelona win!'
WHEN m.home_goal > m.away_goal THEN'Barcelona loss :('
ELSE 'Tie' END AS outcome
FROM
matches_spain AS m
-- Join teams_spain to matches_spain
LEFT JOIN
teams_spain AS t
ON
m.hometeam_id = t.team_api_id
WHERE
m.awayteam_id = 8634;
Barcelona and Real Madrid have been rival teams for more than 80 years. Matches between these two teams are given the name El Clásico (The Classic). In this exercise, you will query a list of matches played between these two rivals.
You will notice in Step 2 that when you have multiple logical conditions in a CASE statement, you may quickly end up with a large number of WHEN clauses to logically test every outcome you are interested in. It’s important to make sure you don’t accidentally exclude key information in your ELSE clause.
In this exercise, you will retrieve information about matches played between Barcelona (id = 8634
) and Real Madrid (id = 8633
). Note that the query you are provided with already identifies the Clásico matches using a filter in the WHERE
clause.
Instruction 1
CASE
statement, identifying Barcelona or Real Madrid as the home team using the hometeam_id
column.CASE
statement in the same way, using awayteam_id
.SELECT
date,
-- Identify the home team as Barcelona or Real Madrid
CASE WHEN hometeam_id = 8634 THEN 'FC Barcelona'
ELSE 'Real Madrid CF' END AS home,
-- Identify the away team as Barcelona or Real Madrid
CASE WHEN awayteam_id = 8634 THEN 'FC Barcelona'
ELSE 'Real Madrid CF' END AS away
FROM
matches_spain
WHERE
(awayteam_id = 8634 OR hometeam_id = 8634) AND
(awayteam_id = 8633 OR hometeam_id = 8633);
Instruction 2
CASE
statement identifying who won each match. Note there are 3 possible outcomes, but 5 conditions that you need to identify.SELECT
date,
CASE WHEN hometeam_id = 8634 THEN 'FC Barcelona'
ELSE 'Real Madrid CF' END as home,
CASE WHEN awayteam_id = 8634 THEN 'FC Barcelona'
ELSE 'Real Madrid CF' END as away,
-- Identify all possible match outcomes
CASE WHEN home_goal > away_goal AND
hometeam_id = 8634
THEN 'Barcelona win!'
WHEN home_goal > away_goal AND
hometeam_id = 8633
THEN 'Real Madrid win!'
WHEN home_goal < away_goal AND
awayteam_id = 8634
THEN 'Barcelona win!'
WHEN home_goal < away_goal AND
awayteam_id = 8633
THEN 'Real Madrid win!'
ELSE 'Tie!' END AS outcome
FROM
matches_spain
WHERE (awayteam_id = 8634 OR hometeam_id = 8634) AND
(awayteam_id = 8633 OR hometeam_id = 8633);
Let’s generate a list of matches won by Italy’s Bologna team! There are quite a few additional teams in the two tables, so a key part of generating a usable query will be using your CASE
statement as a filter in the WHERE
clause.
CASE
statements allow you to categorize data that you’re interested in – and exclude data you’re not interested in. In order to do this, you can use a CASE statement as a filter in the WHERE statement to remove output you don’t want to see.
Here is how you might set that up:
SELECT *
FROM table
WHERE
CASE WHEN a > 5 THEN 'Keep'
WHEN a <= 5 THEN 'Exclude' END = 'Keep';
In essence, you can use the CASE statement as a filtering column like any other column in your database. The only difference is that you don’t alias the statement in WHERE.
Instruction 1
Bologna
’s team ID listed in the teams_italy
table by selecting the team_long_name
and team_api_id
.-- Select team_long_name and team_api_id from team
SELECT
team_long_name,
team_api_id
FROM teams_italy
-- Filter for team name
WHERE team_long_name = 'Bologna';
Instruction 2
season
and date
that a match was played.CASE
statement so that only Bologna’s home and away wins are identified.-- Select the season and date columns
SELECT
season,
date,
-- Identify when Bologna won a match
CASE WHEN hometeam_id = 9857 AND
home_goal > away_goal THEN 'Bologna Win'
WHEN awayteam_id = 9857 AND
away_goal > home_goal THEN 'Bologna Win'
END AS outcome
FROM matches_italy;
Instruction 3
home_goal
and away_goal
for each match.CASE
statement in the WHERE
clause to filter all NULL
values generated by the statement in the previous step.-- Select the season, date, home_goal, and away_goal columns
SELECT
season,
date,
home_goal,
away_goal
FROM
matches_italy
WHERE
-- Exclude games not won by Bologna
CASE WHEN hometeam_id = 9857 AND
home_goal > away_goal THEN 'Bologna Win'
WHEN awayteam_id = 9857 AND
away_goal > home_goal THEN 'Bologna Win'
END IS NOT NULL;
Instruction 1
CASE
statement that identifies the id
of matches played in the 2012/2013 season. Specify that you want ELSE
values to be NULL
.CASE
statement in a COUNT
function and group the query by the country
alias.SELECT
c.name AS country,
-- Count games from the 2012/2013 season
COUNT(CASE WHEN m.season = '2012/2013'
THEN m.id
ELSE NULL END) AS matches_2012_2013
FROM country AS c
LEFT JOIN match AS m
ON c.id = m.country_id
-- Group by country name alias
GROUP BY country;
Instruction 2
CASE WHEN
statements counting the matches played in each country across the 3 seasons.END
your CASE
statement without an ELSE
clause.SELECT
c.name AS country,
-- Count matches in each of the 3 seasons
COUNT(CASE WHEN m.season = '2012/2013'
THEN m.id END) AS matches_2012_2013,
COUNT(CASE WHEN m.season = '2013/2014'
THEN m.id END) AS matches_2013_2014,
COUNT(CASE WHEN m.season = '2014/2015'
THEN m.id END) AS matches_2014_2015
FROM country AS c
LEFT JOIN match AS m
ON c.id = m.country_id
-- Group by country name alias
GROUP BY country;
Instruction
CASE
statements to “count” matches in the '2012/2013'
, '2013/2014'
, and '2014/2015'
seasons, respectively.CASE
statement return a 1
for every match you want to include, and a 0
for every match to exclude.CASE
statement in a SUM
to return the total matches played in each season.SELECT
c.name AS country,
-- Sum the total records in each season where the home team won
SUM(CASE WHEN m.season = '2012/2013' AND m.home_goal > m.away_goal
THEN 1 ELSE 0 END) AS matches_2012_2013,
SUM(CASE WHEN m.season = '2013/2014' AND m.home_goal > m.away_goal
THEN 1 ELSE 0 END) AS matches_2013_2014,
SUM(CASE WHEN m.season = '2014/2015' AND m.home_goal > m.away_goal
THEN 1 ELSE 0 END) AS matches_2014_2015
FROM country AS c
LEFT JOIN match AS m
ON c.id = m.country_id
-- Group by country name alias
GROUP BY country;
Instruction
CASE
statements to COUNT
the total number of home team wins, away team wins, and ties. This will allow you to examine the total number of records. You will convert this to an AVG
in the next step.SELECT
c.name AS country,
-- Sum the home wins, away wins, and ties in each country
COUNT(CASE WHEN m.home_goal > m.away_goal
THEN m.id END) AS home_wins,
COUNT(CASE WHEN m.home_goal < m.away_goal
THEN m.id END) AS away_wins,
COUNT(CASE WHEN m.home_goal = m.away_goal
THEN m.id END) AS ties
FROM country AS c
LEFT JOIN matches AS m
ON c.id = m.country_id
GROUP BY country;
Instruction 2
CASE
statement inside AVG
.ties_2013_2014
and ties_2014_2015
, respectively.SELECT
c.name AS country,
-- Calculate the percentage of tied games in each season
AVG(CASE WHEN m.season='2013/2014' AND
m.home_goal = m.away_goal THEN 1
WHEN m.season='2013/2014' AND
m.home_goal != m.away_goal THEN 0 END) AS ties_2013_2014,
AVG(CASE WHEN m.season='2014/2015' AND
m.home_goal = m.away_goal THEN 1
WHEN m.season='2014/2015' AND
m.home_goal != m.away_goal THEN 0 END) AS ties_2014_2015
FROM country AS c
LEFT JOIN matches AS m
ON c.id = m.country_id
GROUP BY country;
Instruction 3
ROUND
function to round to 2 decimal points.SELECT
c.name AS country,
-- Round the percentage of tied games to 2 decimal points
ROUND(AVG(CASE WHEN m.season='2013/2014' AND
m.home_goal = m.away_goal THEN 1
WHEN m.season='2013/2014' AND
m.home_goal != m.away_goal THEN 0
END),2) AS pct_ties_2013_2014,
ROUND(AVG(CASE WHEN m.season='2014/2015' AND
m.home_goal = m.away_goal THEN 1
WHEN m.season='2014/2015' AND
m.home_goal != m.away_goal THEN 0
END),2) AS pct_ties_2014_2015
FROM country AS c
LEFT JOIN matches AS m
ON c.id = m.country_id
GROUP BY country;
Instruction 1:
?column?
in your results.-- Select the average of home + away goals, multiplied by 3
SELECT
3 * AVG(home_goal + away_goal)
FROM matches_2013_2014;
Instruction 2:
SELECT
-- Select the date, home goals, and away goals scored
date,
home_goal,
away_goal
FROM matches_2013_2014
-- Filter for matches where total goals exceeds 3x the average
WHERE (home_goal + away_goal) >
(SELECT 3 * AVG(home_goal + away_goal)
FROM matches_2013_2014);
Instruction:
WHERE
clause that retrieves all unique hometeam_ID
values from the match
table.team_long_name
and team_short_name
from the team
table. Exclude all values from the subquery in the main query.SELECT
-- Select the team long and short names
team_long_name,
team_short_name
FROM team
-- Exclude all values from the subquery
WHERE team_api_id NOT IN
(SELECT DISTINCT hometeam_ID FROM match);
Instruction:
WHERE
clause that retrieves all hometeam_ID
values from match with a home_goal
score greater than or equal to 8.team_long_name
and team_short_name
from the team
table. Include all values from the subquery in the main query.SELECT
-- Select the team long and short names
team_long_name,
team_short_name
FROM team
-- Filter for teams with 8 or more home goals
WHERE team_api_id IN
(SELECT hometeam_ID
FROM match
WHERE home_goal >= 8);
id
) from the match
table.Instruction 1:
SELECT
-- Select the country ID and match ID
country_id,
id
FROM match
-- Filter for matches with 10 or more goals in total
WHERE (home_goal + away_goal) >= 10;
Instruction 2:
SELECT
-- Select country name and the count match IDs
c.name AS country_name,
COUNT(sub.id) AS matches
FROM country AS c
-- Inner join the subquery onto country
-- Select the country id and match id columns
INNER JOIN (SELECT country_id, id
FROM match
-- Filter the subquery by matches with 10+ goals
WHERE (home_goal + away_goal) >= 10) AS sub
ON c.id = sub.country_id
GROUP BY country_name;
Instruction :
SELECT
-- Select country, date, home, and away goals from the subquery
country,
date,
home_goal,
away_goal
FROM
-- Select country name, date, and total goals in the subquery
(SELECT c.name AS country,
m.date,
m.home_goal,
m.away_goal,
(m.home_goal + m.away_goal) AS total_goals
FROM match AS m
LEFT JOIN country AS c
ON m.country_id = c.id) AS subq
-- Filter by total goals scored in the main query
WHERE total_goals >= 10;
Instruction:
home_goal
and away_goal
.home_goal
and away_goal
. This calculates the average goals for each league.SELECT
l.name AS league,
-- Select and round the league's total goals
ROUND(AVG(m.home_goal + m.away_goal), 2) AS avg_goals,
-- Select & round the average total goals for the season
(SELECT round(AVG(home_goal + away_goal), 2)
FROM match
WHERE season = '2013/2014') AS overall_avg
FROM league AS l
LEFT JOIN match AS m
ON l.country_id = m.country_id
-- Filter for the 2013/2014 season
WHERE season = '2013/2014'
GROUP BY league;
Instruction:
SELECT
-- Select the league name and average goals scored
l.name AS league,
ROUND(AVG(m.home_goal + m.away_goal),2) AS avg_goals,
-- Subtract the overall average from the league average
ROUND(AVG(m.home_goal + m.away_goal) -
(SELECT AVG(home_goal + away_goal)
FROM match
WHERE season = '2013/2014'),2) AS diff
FROM league AS l
LEFT JOIN match AS m
ON l.country_id = m.country_id
-- Only include 2013/2014 results
WHERE m.season = '2013/2014'
GROUP BY l.name;
Instruction:
SELECT
subqueries.m.stage
column.SELECT
-- Select the stage and average goals for each stage
m.stage,
ROUND(AVG(m.home_goal + m.away_goal),2) AS avg_goals,
-- Select the average overall goals for the 2012/2013 season
ROUND((SELECT AVG(home_goal + away_goal)
FROM match
WHERE season = '2012/2013'),2) AS overall
FROM match AS m
-- Filter for the 2012/2013 season
WHERE m.season = '2012/2013'
-- Group by stage
GROUP BY stage;
Instruction:
FROM
clause subquery.WHERE
clause that calculates the overall average home goals.stage
and avg_goals
columns from the s
subquery into the main query.SELECT
-- Select the stage and average goals from the subquery
s.stage,
ROUND(s.avg_goals,2) AS avg_goals
FROM
-- Select the stage and average goals in 2012/2013
(SELECT
stage,
AVG(home_goal + away_goal) AS avg_goals
FROM match
WHERE season = '2012/2013'
GROUP BY stage) AS s
WHERE
-- Filter the main query using the subquery
s.avg_goals > (SELECT AVG(home_goal + away_goal)
FROM match WHERE season = '2012/2013');
Instruction:
SELECT
that yields the average goals scored in the 2012/2013 season. Name the new column overall_avg
.FROM
that calculates the average goals scored in each stage during the 2012/2013 season.SELECT
-- Select the stage and average goals from s
s.stage,
ROUND(s.avg_goals,2) AS avg_goal,
-- Select the overall average for 2012/2013
(SELECT AVG(home_goal + away_goal) FROM match WHERE season = '2012/2013') AS overall_avg
FROM
-- Select the stage and average goals in 2012/2013 from match
(SELECT
stage,
AVG(home_goal + away_goal) AS avg_goals
FROM match
WHERE season = '2012/2013'
GROUP BY stage) AS s
WHERE
-- Filter the main query using the subquery
s.avg_goals > (SELECT AVG(home_goal + away_goal)
FROM match WHERE season = '2012/2013');
Instruction:
country_id
, date
, home_goal
, and away_goal
columns in the main query.AVG
value in the subquery.country_id
is matched in the main and subquery.SELECT
-- Select country ID, date, home, and away goals from match
main.country_id,
date,
main.home_goal,
main.away_goal
FROM match AS main
WHERE
-- Filter the main query by the subquery
(home_goal + away_goal) >
(SELECT AVG((sub.home_goal + sub.away_goal) * 3)
FROM match AS sub
-- Join the main query to the subquery in WHERE
WHERE main.country_id = sub.country_id);
country_id
, date
, home_goal
, and away_goal
columns in the main query.country_id
and season
.Instruction 1:
SELECT
-- Select country ID, date, home, and away goals from match
main.country_id,
main.date,
main.home_goal,
main.away_goal
FROM match AS main
WHERE
-- Filter for matches with the highest number of goals scored
(home_goal + away_goal) =
(SELECT MAX(sub.home_goal + sub.away_goal)
FROM match AS sub
WHERE main.country_id = sub.country_id
AND main.season = sub.season);
Instruction 1:
max_goals
.overall_max_goals
.july_max_goals
.SELECT
-- Select the season and max goals scored in a match
season,
MAX(home_goal + away_goal) AS max_goals,
-- Select the overall max goals scored in a match
(SELECT MAX(home_goal + away_goal) FROM match) AS overall_max_goals,
-- Select the max number of goals scored in any match in July
(SELECT MAX(home_goal + away_goal)
FROM match
WHERE id IN (
SELECT id FROM match WHERE EXTRACT(MONTH FROM date) = 07)) AS july_max_goals
FROM match
GROUP BY season;
Instruction 1:
-- Select matches where a team scored 5+ goals
SELECT
country_id,
season,
id
FROM match
WHERE home_goal > 5 OR away_goal > 5;
Instruction 2:
FROM
statement.COUNT
the match id
s generated in the previous step, and group the query by country_id
and season
.-- Count match ids
SELECT
country_id,
season,
count(id) AS matches
-- Set up and alias the subquery
FROM (
SELECT
country_id,
season,
id
FROM match
WHERE home_goal >= 5 OR away_goal >= 5) AS subquery
-- Group by country_id and season
GROUP BY country_id, season;
Instruction 3:
FROM
with the alias outer_s
. Left join it to the country
table using the outer query’s country_id
column.AVG
of high scoring matches per season in the main query.SELECT
c.name AS country,
-- Calculate the average matches per season
AVG(id) AS avg_seasonal_high_scores
FROM country AS c
-- Left join outer_s to country
LEFT JOIN (
SELECT country_id, season,
COUNT(id) AS matches
FROM (
SELECT country_id, season, id
FROM match
WHERE home_goal >= 5 OR away_goal >= 5) AS inner_s
-- Close parentheses and alias the subquery
GROUP BY country_id, season ) AS outer_s
ON c.id = outer_s.country_id
GROUP BY country;
Instruction:
country_id
and match id
from the match
table in your CTE.country_id
.-- Set up your CTE
WITH match_list AS (
SELECT
country_id,
id
FROM match
WHERE (home_goal + away_goal) >= 10)
-- Select league and count of matches from the CTE
SELECT
l.name AS league,
COUNT(match_list.id) AS matches
FROM league AS l
-- Join the CTE to the league table
LEFT JOIN match_list ON l.id = match_list.country_id
GROUP BY l.name;
Instruction:
-- Set up your CTE
WITH match_list AS (
-- Select the league, date, home, and away goals
SELECT
l.name AS league,
m.date,
m.home_goal,
m.away_goal,
(m.home_goal + m.away_goal) AS total_goals
FROM match AS m
LEFT JOIN league as l ON m.country_id = l.id)
-- Select the league, date, home, and away goals from the CTE
SELECT league, date, home_goal, away_goal
FROM match_list
-- Filter by total goals
WHERE total_goals >= 10;
Instruction 1:
country_id
from the match_list
CTE.-- Set up your CTE
WITH match_list AS (
SELECT
country_id,
(home_goal + away_goal) AS goals
FROM match
-- Create a list of match IDs to filter data in the CTE
WHERE id IN (
SELECT id
FROM match
WHERE season = '2013/2014' AND EXTRACT(MONTH FROM date) = 8))
-- Select the league name and average of goals in the CTE
SELECT
l.name,
AVG(match_list.goals)
FROM league AS l
-- Join the CTE onto the league table
LEFT JOIN match_list ON l.id = match_list.country_id
GROUP BY l.name;
Instruction 1:
team
to match
in order to get the identity of the home team. This becomes the subquery in the next step.SELECT
m.id,
t.team_long_name AS hometeam
-- Left join team to match
FROM match AS m
LEFT JOIN team as t
ON m.hometeam_id = team_api_id;
Instruction 2:
FROM
statement to get the away team name, changing only the hometeam_id
. Left join both subqueries to the match
table on the id
column.SELECT
m.date,
-- Get the home and away team names
hometeam,
awayteam,
m.home_goal,
m.away_goal
FROM match AS m
-- Join the home subquery to the match table
LEFT JOIN (
SELECT match.id, team.team_long_name AS hometeam
FROM match
LEFT JOIN team
ON match.hometeam_id = team.team_api_id) AS home
ON home.id = m.id
-- Join the away subquery to the match table
LEFT JOIN (
SELECT match.id, team.team_long_name AS awayteam
FROM match
LEFT JOIN team
-- Get the away team ID in the subquery
ON match.awayteam_id = team.team_api_id) AS away
ON away.id = m.id;
Instruction 1:
SELECT
statement, match the team_api_id
column from team
to the hometeam_id
from match
.SELECT
m.date,
(SELECT team_long_name
FROM team AS t
-- Connect the team to the match table
WHERE t.team_api_id = m.hometeam_id) AS hometeam
FROM match AS m;
Instruction 2:
SELECT
m.date,
(SELECT team_long_name
FROM team AS t
WHERE t.team_api_id = m.hometeam_id) AS hometeam,
-- Connect the team to the match table
(SELECT team_long_name
FROM team AS t
WHERE t.team_api_id = m.awayteam_id) AS awayteam,
-- Select home and away goals
home_goal,
away_goal
FROM match AS m;
Instruction 1:
id
from match
and team_long_name
from team
. Join these two tables together on hometeam_id
in match
and team_api_id
in team
.SELECT
-- Select match id and team long name
m.id,
t.team_long_name AS hometeam
FROM match AS m
-- Join team to match using team_api_id and hometeam_id
LEFT JOIN team AS t
ON t.team_api_id = m.hometeam_id;
Instruction 2:
SELECT
everything from the CTE into the main query. Your results will not change at this step!-- Declare the home CTE
WITH home AS (
SELECT m.id, t.team_long_name AS hometeam
FROM match AS m
LEFT JOIN team AS t
ON m.hometeam_id = t.team_api_id)
-- Select everything from home
SELECT *
FROM home;
Instruction 3:
away
. Join it to the first CTE on the id
column.date
, home_goal
, and away_goal
columns have been added to the CTEs. SELECT
them into the main query.WITH home AS (
SELECT m.id, m.date,
t.team_long_name AS hometeam, m.home_goal
FROM match AS m
LEFT JOIN team AS t
ON m.hometeam_id = t.team_api_id),
-- Declare and set up the away CTE
away AS (
SELECT m.id, m.date,
t.team_long_name AS awayteam, m.away_goal
FROM match AS m
LEFT JOIN team AS t
ON m.awayteam_id = t.team_api_id)
-- Select date, home_goal, and away_goal
SELECT
home.date,
home.hometeam,
away.awayteam,
home.home_goal,
away.away_goal
-- Join away and home on the id column
FROM home
INNER JOIN away
ON home.id = away.id;
Instruction 1:
match
and country
tables.SELECT
-- Select the id, country name, season, home, and away goals
m.id,
c.name AS country,
m.season,
m.home_goal,
m.away_goal,
-- Use a window to include the aggregate average in each row
AVG(m.home_goal + m.away_goal) OVER() AS overall_avg
FROM match AS m
LEFT JOIN country AS c ON m.country_id = c.id;
Instruction:
league
and match
.SELECT
-- Select the league name and average goals scored
l.name AS league,
AVG(m.home_goal + m.away_goal) AS avg_goals,
-- Rank each league according to the average goals
RANK() OVER(ORDER BY AVG(m.home_goal + m.away_goal)) AS league_rank
FROM league AS l
LEFT JOIN match AS m
ON l.id = m.country_id
WHERE m.season = '2011/2012'
GROUP BY l.name
-- Order the query by the rank you created
ORDER BY league_rank;
Instruction 1:
SELECT
-- Select the league name and average goals scored
l.name AS league,
AVG(m.home_goal + m.away_goal) AS avg_goals,
-- Rank leagues in descending order by average goals
RANK () OVER(ORDER BY AVG(m.home_goal + m.away_goal) DESC) AS league_rank
FROM league AS l
LEFT JOIN match AS m
ON l.id = m.country_id
WHERE m.season = '2011/2012'
GROUP BY l.name
-- Order the query by the rank you created
ORDER BY league_rank;
Instruction 1:
id = 8673
.SELECT
date,
season,
home_goal,
away_goal,
CASE WHEN hometeam_id = 8673 THEN 'home'
ELSE 'away' END AS warsaw_location,
-- Calculate the average goals scored partitioned by season
AVG(home_goal) OVER (PARTITION BY season) AS season_homeavg,
AVG(away_goal) OVER (PARTITION BY season) AS season_awayavg
FROM match
-- Filter the data set for Legia Warszawa matches only
WHERE
hometeam_id = 8673
OR awayteam_id = 8673
ORDER BY (home_goal + away_goal) DESC;
8673
) so that the window calculation excludes all teams who did not play against them.Instruction:
SELECT
date,
season,
home_goal,
away_goal,
CASE WHEN hometeam_id = 8673 THEN 'home'
ELSE 'away' END AS warsaw_location,
-- Calculate average goals partitioned by season and month
AVG(home_goal) OVER(PARTITION BY season,
EXTRACT(month FROM date)) AS season_mo_home,
AVG(away_goal) OVER(PARTITION BY season,
EXTRACT(month FROM date)) AS season_mo_away
FROM match
WHERE
hometeam_id = 8673
OR awayteam_id = 8673
ORDER BY (home_goal + away_goal) DESC;
date
.Instruction:
SELECT
date,
home_goal,
away_goal,
-- Create a running total and running average of home goals
SUM(home_goal) OVER(ORDER BY date
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS running_total,
AVG(home_goal) OVER(ORDER BY date
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS running_avg
FROM match
WHERE
hometeam_id = 9908
AND season = '2011/2012';
Instruction 1:
SELECT
-- Select the date, home goal, and away goals
date,
home_goal,
away_goal,
-- Create a running total and running average of home goals
SUM(home_goal) OVER(ORDER BY date
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS running_total,
AVG(home_goal) OVER(ORDER BY date
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS running_avg
FROM match
WHERE
awayteam_id = 9908
AND season = '2011/2012';
Instruction:
CASE
statement that identifies each match as a win, lose, or tie for Manchester United.WHEN
clause in the CASE
statement (equals, greater than, less than).match
, and team_api_id
from team
.SELECT
m.id,
t.team_long_name,
-- Identify matches as home/away wins or ties
CASE WHEN m.home_goal > m.away_goal THEN 'MU Win'
WHEN m.home_goal < m.away_goal THEN 'MU Loss'
ELSE 'Tie' END AS outcome
FROM match AS m
-- Left join team on the home team ID and team API id
LEFT JOIN team AS t
ON m.hometeam_id = t.team_api_id
WHERE
-- Filter for 2014/2015 and Manchester United as the home team
season = '2014/2015'
AND t.team_long_name = 'Manchester United';
Instruction:
CASE
statement syntax.awayteam_id
, and team_api_id
.SELECT
m.id,
t.team_long_name,
-- Identify matches as home/away wins or ties
CASE WHEN m.home_goal > m.away_goal THEN 'MU Loss'
WHEN m.home_goal < m.away_goal THEN 'MU Win'
ELSE 'Tie' END AS outcome
-- Join team table to the match table
FROM match AS m
LEFT JOIN team AS t
ON m.awayteam_id = t.team_api_id
WHERE
-- Filter for 2014/2015 and Manchester United as the away team
season = '2014/2015'
AND t.team_long_name = 'Manchester United';
Instruction:
home
and away
CTEs before your main query.LEFT JOIN
.match
, team names from the CTEs, and home/ away goals from match
in the main query.-- Set up the home team CTE
WITH home AS (
SELECT m.id, t.team_long_name,
CASE WHEN m.home_goal > m.away_goal THEN 'MU Win'
WHEN m.home_goal < m.away_goal THEN 'MU Loss'
ELSE 'Tie' END AS outcome
FROM match AS m
LEFT JOIN team AS t ON m.hometeam_id = t.team_api_id),
-- Set up the away team CTE
away AS (
SELECT m.id, t.team_long_name,
CASE WHEN m.home_goal > m.away_goal THEN 'MU Win'
WHEN m.home_goal < m.away_goal THEN 'MU Loss'
ELSE 'Tie' END AS outcome
FROM match AS m
LEFT JOIN team AS t ON m.awayteam_id = t.team_api_id)
-- Select team names, the date and goals
SELECT DISTINCT
m.date,
home.team_long_name AS home_team,
away.team_long_name AS away_team,
m.home_goal,
m.away_goal
-- Join the CTEs onto the match table
FROM match AS m
LEFT JOIN home ON m.id = home.id
LEFT JOIN away ON m.id = away.id
WHERE m.season = '2014/2015'
AND (home.team_long_name = 'Manchester United'
OR away.team_long_name = 'Manchester United');
Instruction 1:
-- Set up the home team CTE
WITH home AS (
SELECT m.id, t.team_long_name,
CASE WHEN m.home_goal > m.away_goal THEN 'MU Win'
WHEN m.home_goal < m.away_goal THEN 'MU Loss'
ELSE 'Tie' END AS outcome
FROM match AS m
LEFT JOIN team AS t ON m.hometeam_id = t.team_api_id),
-- Set up the away team CTE
away AS (
SELECT m.id, t.team_long_name,
CASE WHEN m.home_goal > m.away_goal THEN 'MU Loss'
WHEN m.home_goal < m.away_goal THEN 'MU Win'
ELSE 'Tie' END AS outcome
FROM match AS m
LEFT JOIN team AS t ON m.awayteam_id = t.team_api_id)
-- Select columns and and rank the matches by date
SELECT DISTINCT
m.date,
home.team_long_name AS home_team,
away.team_long_name AS away_team,
m.home_goal, m.away_goal,
RANK() OVER(ORDER BY ABS(home_goal - away_goal) DESC) as match_rank
-- Join the CTEs onto the match table
FROM match AS m
LEFT JOIN home ON m.id = home.id
LEFT JOIN away ON m.id = away.id
WHERE m.season = '2014/2015'
AND ((home.team_long_name = 'Manchester United' AND home.outcome = 'MU Loss')
OR (away.team_long_name = 'Manchester United' AND away.outcome = 'MU Loss'));