Window Functions

Data Manipulation in SQL

Mona Khalil

Data Scientist, Greenhouse Software

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;
ERROR: column "match.season" must appear in the GROUP BY 
clause or be used in an aggregate function
Data Manipulation in SQL

Introducing window functions!

  • Perform calculations on an already generated result set (a window)
  • Aggregate calculations
    • Similar to subqueries in SELECT
    • Running totals, rankings, moving averages
Data Manipulation in 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';
| date       | goals | overall_avg       |
|------------|-------|-------------------|
| 2011-07-29 | 3     | 2.71646           |
| 2011-07-30 | 2     | 2.71646           |
| 2011-07-30 | 4     | 2.71646           |
| 2011-07-30 | 1     | 2.71646           |
Data Manipulation in 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';
| date       | goals | overall_avg       |
|------------|-------|-------------------|
| 2011-07-29 | 3     | 2.71646           |
| 2011-07-30 | 2     | 2.71646           |
| 2011-07-30 | 4     | 2.71646           |
| 2011-07-30 | 1     | 2.71646           |
Data Manipulation in 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     |
Data Manipulation in 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';
| date       | goals | goals_rank |
|------------|-------|------------|
| 2012-04-28 | 0     | 1          |
| 2011-12-26 | 0     | 1          |
| 2011-09-10 | 0     | 1          |
| 2011-08-27 | 0     | 1          |
| 2012-01-07 | 0     | 1          |
Data Manipulation in 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';
| date       | goals | goals_rank |
|------------|-------|------------|
| 2011-11-06 | 10    | 1          |
| 2011-08-28 | 10    | 1          |
| 2012-05-12 | 9     | 3          |
| 2012-02-12 | 9     | 3          |
| 2012-03-09 | 9     | 3          |
Data Manipulation in SQL

Key differences

  • Processed after every part of query except ORDER BY
    • Uses information in result set rather than database
  • Available in PostgreSQL, Oracle, MySQL, SQL Server...
    • ...but NOT SQLite
Data Manipulation in SQL

Let's practice!

Data Manipulation in SQL

Preparing Video For Download...