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:

Methodology/Instructions:

  1. 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;
  2. 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;
  3. 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;
  4. 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;
  5. 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

Video