SQL Widow Function Notes Class
SQL Widow Function Notes Class
SQL Widow Function Notes Class
I N T E R M E D I AT E S Q L
Mona Khalil
Data Scientist, Greenhouse So ware
Working with aggregate values
Requires you to use GROUP BY with all non-aggregate
columns
SELECT
country_id,
season,
date,
AVG(home_goal) AS avg_home
FROM match
GROUP BY country_id;
INTERMEDIATE SQL
Introducing window functions!
Perform calculations on an already generated result set (a
window)
Aggregate calculations
Similar to subqueries in SELECT
INTERMEDIATE SQL
What's a window function?
How many goals were scored in each match in 2011/2012, and
how did that compare to the average?
SELECT
date,
(home_goal + away_goal) AS goals,
(SELECT AVG(home_goal + away_goal)
FROM match
WHERE season = '2011/2012') AS overall_avg
FROM match
WHERE season = '2011/2012';
INTERMEDIATE SQL
What's a window function?
How many goals were scored in each match in 2011/2012, and
how did that compare to the average?
SELECT
date,
(home_goal + away_goal) AS goals,
AVG(home_goal + away_goal) OVER() AS overall_avg
FROM match
WHERE season = '2011/2012';
INTERMEDIATE SQL
Generate a RANK
What is the rank of matches based on number of goals
scored?
SELECT
date,
(home_goal + away_goal) AS goals
FROM match
WHERE season = '2011/2012';
| date | goals |
|------------|-------|
| 2011-07-29 | 3 |
| 2011-07-30 | 2 |
| 2011-07-30 | 4 |
| 2011-07-30 | 1 |
INTERMEDIATE SQL
Generate a RANK
What is the rank of matches based on number of goals
scored?
SELECT
date,
(home_goal + away_goal) AS goals,
RANK() OVER(ORDER BY home_goal + away_goal) AS goals_rank
FROM match
WHERE season = '2011/2012';
INTERMEDIATE SQL
Generate a RANK
What is the rank of matches based on number of goals
scored?
SELECT
date,
(home_goal + away_goal) AS goals,
RANK() OVER(ORDER BY home_goal + away_goal DESC) AS goals_rank
FROM match
WHERE season = '2011/2012';
INTERMEDIATE SQL
Key differences
Processed a er every part of query except ORDER BY
Uses information in result set rather than database
INTERMEDIATE SQL
Let's practice!
I N T E R M E D I AT E S Q L
Window Partitions
I N T E R M E D I AT E S Q L
Mona Khalil
Data Scientist, Greenhouse So ware
OVER and PARTITION BY
Calculate separate values for di erent categories
INTERMEDIATE SQL
Partition your data
How many goals were scored in each match, and how did
that compare to the overall average?
SELECT
date,
(home_goal + away_goal) AS goals,
AVG(home_goal + away_goal) OVER() AS overall_avg
FROM match;
INTERMEDIATE SQL
Partition your data
How many goals were scored in each match, and how did
that compare to the season's average?
SELECT
date,
(home_goal + away_goal) AS goals,
AVG(home_goal + away_goal) OVER(PARTITION BY season) AS season_avg
FROM match;
INTERMEDIATE SQL
PARTITION by Multiple Columns
SELECT
c.name,
m.season,
(home_goal + away_goal) AS goals,
AVG(home_goal + away_goal)
OVER(PARTITION BY m.season, c.name) AS season_ctry_avg
FROM country AS c
LEFT JOIN match AS m
ON c.id = m.country_id
INTERMEDIATE SQL
PARTITION BY considerations
Can partition data by 1 or more columns
INTERMEDIATE SQL
Let's practice!
I N T E R M E D I AT E S Q L
Sliding windows
I N T E R M E D I AT E S Q L
Mona Khalil
Data Scientist, Greenhouse So ware
Sliding windows
Perform calculations relative to the current row
INTERMEDIATE SQL
Sliding window keywords
ROWS BETWEEN <start> AND <finish>
PRECEDING
FOLLOWING
UNBOUNDED PRECEDING
UNBOUNDED FOLLOWING
CURRENT ROW
INTERMEDIATE SQL
Sliding window example
-- Manchester City Home Games
SELECT
date,
home_goal,
away_goal,
SUM(home_goal)
OVER(ORDER BY date ROWS BETWEEN
UNBOUNDED PRECEDING AND CURRENT ROW) AS running_total
FROM match
WHERE hometeam_id = 8456 AND season = '2011/2012';
INTERMEDIATE SQL
Sliding window frame
-- Manchester City Home Games
SELECT date,
home_goal,
away_goal,
SUM(home_goal)
OVER(ORDER BY date
ROWS BETWEEN 1 PRECEDING
AND CURRENT ROW) AS last2
FROM match
WHERE hometeam_id = 8456
AND season = '2011/2012';
INTERMEDIATE SQL
Let's practice!
I N T E R M E D I AT E S Q L
Bringing it all
Together
I N T E R M E D I AT E S Q L
Mona Khalil
Data Scientist, Greenhouse So ware
What you've learned so far
CASE statements
Simple subqueries
Window functions
INTERMEDIATE SQL
Let's do a case study!
Who defeated Manchester United in the 2013/2014 season?
INTERMEDIATE SQL
Steps to construct the query
Get team names with CTEs
INTERMEDIATE SQL
Getting the database for yourself
Full European Soccer Database
INTERMEDIATE SQL
Let's practice!
I N T E R M E D I AT E S Q L