Course Content
Introduction: How to Become a Data Analyst
How to Become a Data Analyst
0/1
Installing MySQL and create a database for Windows, MacOS, and Linux
How to Installing MySQL and create a database.
0/1
SELECT Statement and Where Clause in MySQL
Starting your Data Analysis Properly
0/2
LIMIT` + ALIASING` Group by+ Order By, Having Vs Where in MySQL
LIMIT` + ALIASING`
0/3
JOINS
Joins in MySQL
0/1
Unions in MySQL
Unions in MySQL
0/1
Window functions in MySQL
Window functions:- in MySQL
0/1
Common Table Expressions (CTEs) in MySQL and Temp Tables
Common Table Expressions (CTEs) in MySQL
0/2
stored procedures
stored procedures.
0/1
Triggers and Events in MySQL
Triggers and Events
0/1
Data Cleaning in MySQL
Data Cleaning in MySQL
0/1
MSQL EXPLORATORY DATA ANALYSIS
MSQL EXPLORATORY DATA ANALYSIS
0/1
Data Analyst Resume
Data Analyst Resume
0/1
How To Download Your Data Analyst Bootcamp Certification (Congrats!!)
How To Download Your Data Analyst Bootcamp Certification (Congrats!!)
0/1
Guide to Data Analysis for Beginners
About Lesson

Window functions:- in MySQL are powerful tools used to perform calculations across a set of table rows related to the current row, without grouping the results into a single output row. They are particularly useful for tasks like ranking, calculating running totals, moving averages, and more. Window functions work with the **OVER()** clause to define the partition and ordering of the data.

 

Window functions are essential for sophisticated data analysis and reporting in MySQL, enabling users to perform advanced calculations on their data

 

Key Components

 

  1. PARTITION BY:- Divides the result set into partitions to which the window function is applied.
  2. ORDER BY: Specifies the order of rows within each partition.
  3. Frame Specification: Defines a subset of rows in the partition, which can be specified with clauses like `ROWS` or `RANGE`.

 Syntax

<function_name>(expression) OVER (

    [PARTITION BY expr_list]

    [ORDER BY order_list]

    [frame_clause]

)

Examples

 

  1. Ranking Rows:-

    SELECT

        employee_id,

        salary,

        RANK() OVER (ORDER BY salary DESC) AS salary_rank

    FROM

        employees;

    This query ranks employees based on their salary in descending order.

 

  1. Running Total:-

    SELECT

        order_id,

        order_amount,

        SUM(order_amount) OVER (ORDER BY order_date) AS running_total

    FROM

        orders;

    This query calculates a running total of order amounts ordered by order date.

  1. Average with Partition

    SELECT

        department_id,

        employee_id,

        salary,

        AVG(salary) OVER (PARTITION BY department_id) AS avg_department_salary

    FROM

        employees;

    This query calculates the average salary of employees within each department.

 

 Advantages

-Flexible Data Analysis: Window functions provide more flexibility than traditional aggregate functions by allowing calculations without collapsing rows.

– Ease of Use: They can be used alongside regular SELECT queries without modifying how rows are grouped.

– Enhanced Performance: Efficient handling of complex calculations and analytics within the database engine.

Common Window Functions

– `ROW_NUMBER()`: Assigns a unique sequential integer to rows within a partition.

– `RANK()`: Provides the rank of each row within the partition, with gaps.

– `DENSE_RANK()`: Similar to `RANK()`, but without gaps.

– `LEAD()` and `LAG()`: Access data from subsequent or previous rows, respectively.

– `NTILE()`: Divides rows into a specified number of approximately equal groups.