Summary of Window Functions in MySQL | Intermediate MySQL
Summary of "Window Functions in MySQL | Intermediate MySQL"
This video lesson provides an overview of Window Functions in MySQL, demonstrating their power and flexibility compared to traditional grouping methods. The speaker explains how Window Functions allow for calculations over a set of rows while retaining individual row data, unlike the GROUP BY
clause which condenses data into single rows.
Main Ideas and Concepts:
- Window Functions: These functions perform calculations across a set of rows related to the current row, allowing for more granular data analysis without collapsing rows.
- Comparison with
GROUP BY
: The speaker compares Window Functions withGROUP BY
, illustrating how the former retains individual rows while performing calculations. - Key Window Functions:
- Average Salary Calculation: Demonstrated using both
GROUP BY
and a window function, showing how Window Functions can provide average values without aggregating rows. - Partitioning: Using the
PARTITION BY
clause to segment data (e.g., by gender) while still allowing for individual row visibility. - Rolling Total: Explained as a cumulative total that adds values from subsequent rows, showcasing its application in financial contexts.
- Row Number, Rank, and Dense Rank: These functions assign unique identifiers or rankings to rows based on specified criteria, with differences in how they handle duplicates.
- Average Salary Calculation: Demonstrated using both
Methodology/Instructions:
- Using
GROUP BY
:- Write a query to aggregate data (e.g., average salary by gender).
- Example:
SELECT gender, AVG(salary) AS average_salary FROM demographics JOIN salaries ON demographics.employee_id = salaries.employee_id GROUP BY gender;
- Using Window Functions:
- Write a query to calculate average salary without grouping.
- Example:
SELECT gender, AVG(salary) OVER () AS average_salary FROM demographics JOIN salaries ON demographics.employee_id = salaries.employee_id;
- Partitioning with Window Functions:
- Use
PARTITION BY
to segment calculations. - Example:
SELECT gender, AVG(salary) OVER (PARTITION BY gender) AS average_salary FROM demographics JOIN salaries ON demographics.employee_id = salaries.employee_id;
- Use
- Calculating Rolling Totals:
- Add an
ORDER BY
clause to create a Rolling Total. - Example:
SELECT gender, salary, SUM(salary) OVER (PARTITION BY gender ORDER BY employee_id) AS rolling_total FROM demographics JOIN salaries ON demographics.employee_id = salaries.employee_id;
- Add an
- Using Row Number, Rank, and Dense Rank:
- Assign row numbers or ranks based on salary.
- Example for Row Number:
SELECT *, ROW_NUMBER() OVER (PARTITION BY gender ORDER BY salary DESC) AS row_num FROM demographics JOIN salaries ON demographics.employee_id = salaries.employee_id;
- Example for Rank:
SELECT *, RANK() OVER (PARTITION BY gender ORDER BY salary DESC) AS rank_num FROM demographics JOIN salaries ON demographics.employee_id = salaries.employee_id;
- Example for Dense Rank:
SELECT *, DENSE_RANK() OVER (PARTITION BY gender ORDER BY salary DESC) AS dense_rank_num FROM demographics JOIN salaries ON demographics.employee_id = salaries.employee_id;
Featured Speakers/Sources:
The speaker is not named in the subtitles but presents the lesson on Window Functions in MySQL.
Notable Quotes
— 00:00 — « No notable quotes »
Category
Educational