top of page

Welcome
to NumpyNinja Blogs

NumpyNinja: Blogs. Demystifying Tech,

One Blog at a Time.
Millions of views. 

The Complete Guide to SQL Window Functions for Data Analysts

Feb 13
5 min read

SQL is often seen as a tool for retrieving data and calculating totals. However, its true analytical power appears when we use window functions. Unlike traditional GROUP BY queries that combine rows into summaries, window functions allow SQL to perform calculations across related rows while keeping every row visible. This makes it possible to analyze patterns, comparisons, and trends directly within a query. Instead of reducing detail, window functions add context closely matching how humans naturally interpret data.


How We Naturally Think About Data

When analyzing information, we rarely think only in totals. We naturally ask:

  • Who is performing better than others?

  • How much have we reached so far?

  • What changed compared to yesterday?

  • How does one value compare to another?

These questions require relationships between rows, not just summaries.


Real-World Use Cases

Window functions are widely used across industries because they solve problems that require both detail and context. Anywhere patterns, comparisons, or progression matter, window functions fit naturally. They answer the "compared to what?" and "what happened next?" questions that make data analysis meaningful rather than just descriptive.


Window functions are widely used in:

  • Customer lifetime value analysis

  • Business dashboards and reporting

  • Healthcare timelines and patient history

  • Trend and performance tracking

  • Purchase and behavioral analysis

Anywhere patterns, comparisons, or progression matter, window functions fit naturally.


What Is a Window Function?

A window function performs a calculation across a set of related rows without collapsing them into a single row.


Basic syntax:

function_name(...) OVER (

    PARTITION BY column

    ORDER BY column

)


GROUP BY vs Window Functions

  • GROUP BY - (loses detail)

SELECT department, SUM(salary) AS total_salary

FROM employees

GROUP BY department;


Output:

This query gives one row per department, but individual employees disappear.








  • Window Function- (keeps detail)

SELECT

employee_name,

department,

salary,

SUM(salary) OVER (PARTITION BY department) AS dept_total_salary

FROM employees;


Output:

Now every employee remains visible, while the department total is added.







Types of Window Functions

Window functions generally fall into several analytical categories.

1. Ranking Functions


Common ranking functions:

  • ROW_NUMBER() → Assigns a unique sequential number to every row within a partition. Even if two rows have identical values, they still receive different numbers because ties are not considered.

  • RANK() → Assigns the same rank to tied values, but skips subsequent ranks. If two employees tie for rank 1, the next rank becomes 3, creating gaps.

  • DENSE_RANK() → Similar to RANK(), but does not create gaps. Tied rows share the same rank, and the next rank increases consecutively (1, 2, 3…).

  • NTILE(n) → Splits rows into n roughly equal groups (buckets) based on the specified ordering. This is commonly used for percentiles, quartiles, and distribution analysis.


Example: Salary Rank Within Department


SELECT

employee_name,

department,

salary,

RANK() OVER (

PARTITION BY department

ORDER BY salary DESC

) AS salary_rank

FROM employees;


Output:

This query assigns a rank to employees within each department based on their salary, with higher salaries receiving better ranks. The PARTITION BY department ensures ranking resets for every department, while ORDER BY salary DESC sorts salaries from highest to lowest.







2. Aggregate Window Functions

Common aggregate window functions:

  • SUM() → Calculates the total of values across the window while keeping every row visible. Often used for totals, running totals, or cumulative metrics.

  • AVG() → Computes the average value within the window, allowing you to compare each row against a group’s typical value (for example, salary vs department average).

  • COUNT() → Returns the number of rows in the window instead of summing values. Useful for understanding group size, frequency, or density.

  • MIN() → Identifies the smallest value within the window without removing rows. Helpful for benchmarks like lowest salary or earliest date.

  • MAX() → Identifies the largest value within the window. Commonly used for detecting peaks such as highest salary, latest event, or maximum measurement.


Example: Department Total Salary


SELECT

employee_name,

department,

salary,

COUNT(*) OVER (PARTITION BY department) AS dept_employee_count

FROM employees;


Output:

This query calculates how many employees belong to each department while keeping every employee row visible. The COUNT(*) OVER (PARTITION BY department) tells SQL to count rows separately for each department, so every employee in the same department sees the same department headcount.




3. Running / Cumulative Calculations

Running / Cumulative calculations are simply calculations that keep adding values as you move from one row to the next. Instead of giving just one final total, SQL shows how the total grows step by step.


Example: Running Salary Total


SELECT

employee_name,

department,

salary,

SUM(salary) OVER (

PARTITION BY department

ORDER BY salary, employee_id

) AS running_total

FROM employees;


Output:

What this answers

“How do salaries accumulate as values increase?”

This type of logic is essential for:

  • Financial tracking

  • Trend analysis

  • Time-series evaluation






4. Value / Navigation Functions

Value (or navigation) window functions allow SQL to look at nearby rows and retrieve values relative to the current row. Instead of summarizing data, these functions help compare how one row relates to another within the same window. They are especially useful when you need to answer questions like: What was the previous value? What comes next? What is the first or last value in a group?


Common navigation functions:

  • LAG() → Previous row value

  • LEAD() → Next row value

  • FIRST_VALUE() → First value in window

  • LAST_VALUE() → Last value in window


Example: Compare Salary With Previous Salary


SELECT

    employee_name,

    department,

    salary,

    LAG(salary) OVER (

        PARTITION BY department

        ORDER BY salary

    ) AS previous_salary

FROM employees;


Output:

This result shows the LAG() window function returning the salary from the previous row within each department based on the specified ordering. If no previous row exists in that department, then it displays NULL.






5. Distribution Functions

Distribution Functions divide rows into groups or calculate relative positions within a dataset, helping you understand how values are spread. They are commonly used for percentiles, rankings, and bucket-based analysis such as quartiles or top percentages.


Common distribution functions:

  • NTILE(n) → Divides rows into buckets (splits the ordered rows into n roughly equal groups).

  • PERCENT_RANK() → Relative rank from 0 to 1

  • CUME_DIST() → Cumulative distribution shows the percentage of rows less than or equal to the current row.


Example: Salary Distribution Bucket

SELECT

employee_name,

department,

salary,

NTILE(4) OVER (

ORDER BY salary DESC

) AS salary_quartile

FROM employees;


Output:

This output shows how the NTILE() window function divides employees into equal groups based on their ordered salaries. SQL sorts the salaries and assigns a bucket number to each row, indicating the salary quartile an employee belongs to. Lower bucket numbers represent higher salaries, while higher numbers correspond to lower salary ranges.



Final Thoughts

For a data analyst, the real value of SQL lies not just in retrieving data, but in understanding relationships, patterns, and context within that data. Window functions enable this deeper level of analysis by allowing calculations that preserve detail while adding meaningful insight. Whether comparing performance, tracking trends, or evaluating distributions, these functions transform raw datasets into analytical narratives. Mastering window functions is therefore not just an advanced SQL skill , it is a fundamental tool for thinking analytically and extracting intelligence from data.









 
 

+1 (302) 200-8320

NumPy_Ninja_Logo (1).png

Numpy Ninja Inc. 8 The Grn Ste A Dover, DE 19901

© Copyright 2025 by Numpy Ninja Inc.

  • Twitter
  • LinkedIn
bottom of page