TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst ยท 76.7K subscribers
Post #2781 951
๐Ÿš€ SQL Roadmap 2026 โ€” Part 14

๐ŸชŸ SQL Window Functions โ€” Advanced Analytics Without Losing Rows

Window Functions are one of the most powerful features in SQL.

They allow you to perform calculations across related rows without collapsing the rows into a single result.

This is the key difference between:

GROUP BY

โ†’ combines rows

Window Function

โ†’ keeps the rows and calculates across them

Window Functions are heavily used for:

โ€ข Rankings

โ€ข Running totals

โ€ข Moving averages

โ€ข Previous/next row comparisons

โ€ข Customer analysis

โ€ข Sales analysis

โ€ข Time-series analysis

โ€ข Percentage calculations

โ€ข Top-N analysis

๐Ÿง  1. The Problem Window Functions Solve

Suppose we have:

order_id| customer_id| amount

1| 101| 500

2| 101| 800

3| 102| 300

4| 102| 700

If we use:

SELECT
customer_id,
SUM(amount) AS total_spending
FROM orders
GROUP BY customer_id;


we get:

customer_id| total_spending

101| 1300

102| 1000

The individual orders disappear.

But what if we want:

order_id| customer_id| amount| customer_total

1| 101| 500| 1300

2| 101| 800| 1300

3| 102| 300| 1000

4| 102| 700| 1000

This is where a Window Function is useful.

๐ŸชŸ 2. Basic Window Function Syntax

The general structure is:

function_name(...) OVER (
PARTITION BY ...
ORDER BY ...
)


Example:

SELECT
order_id,
customer_id,
amount,

SUM(amount) OVER (
PARTITION BY customer_id
) AS customer_total

FROM orders;


The result keeps every order while calculating the customer's total.

๐Ÿ”น 3. What Does OVER() Mean?

"OVER()" tells SQL:

ยซPerform this calculation across a window of rows.ยป

For example:

SUM(amount) OVER ()


means:

ยซCalculate the sum across all rows while keeping every row.ยป

Example:

SELECT
order_id,
amount,
SUM(amount) OVER () AS total_revenue
FROM orders;


Every row will show the overall revenue.

๐Ÿ“Š 4. PARTITION BY

"PARTITION BY" divides the data into logical groups.

Example:

SUM(amount) OVER (
PARTITION BY customer_id
)


means:

ยซCalculate the sum separately for each customer.ยป

All Orders

โ†“

Partition by customer

โ†“

Customer 101 โ†’ calculate separately

Customer 102 โ†’ calculate separately

Customer 103 โ†’ calculate separately

Unlike "GROUP BY", the original rows remain visible.

๐Ÿ†š 5. GROUP BY vs Window Function

GROUP BY

SELECT
customer_id,
SUM(amount) AS total_spending
FROM orders
GROUP BY customer_id;


Result:

1 row per customer

Window Function

SELECT
order_id,
customer_id,
amount,
SUM(amount) OVER (
PARTITION BY customer_id
) AS total_spending
FROM orders;


Result:

1 row per order

Remember:

GROUP BY reduces rows.

Window Functions preserve rows.

๐Ÿ† 6. RANK()

"RANK()" assigns a ranking to rows.

Example:

SELECT
employee_name,
salary,
RANK() OVER (
ORDER BY salary DESC
) AS salary_rank
FROM employees;


If salaries are:

90000

80000

70000

the ranks are:

1

2

3

๐Ÿฅ‡ 7. Ranking with Ties

Suppose salaries are:

90000

80000

80000

70000

Using "RANK()":

90000 โ†’ 1

80000 โ†’ 2

80000 โ†’ 2

70000 โ†’ 4

Notice that rank 3 is skipped.

That's how "RANK()" handles ties.

๐Ÿ”ข 8. DENSE_RANK()

"DENSE_RANK()" also handles ties but does not skip the next rank.

For:

90000

80000

80000

70000

the result is:

90000 โ†’ 1

80000 โ†’ 2

80000 โ†’ 2

70000 โ†’ 3

Difference:

RANK()

1, 2, 2, 4

DENSE_RANK()

1, 2, 2, 3

๐Ÿ”ข 9. ROW_NUMBER()

"ROW_NUMBER()" assigns a unique sequential number to every row.
  • โค 4
More from @sqlanalyst
  1. Oct 7, 2026SQL Interview Series โ€” Part 4 ๐Ÿ“Œ Question 4: Find the Highest Salary in Each Department Suโ€ฆ
  2. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
  3. Sep 29, 2026SQL Interview Series โ€” Part 2 ๐Ÿ“Œ Question 2: Find Duplicate Records Suppose you have an Emโ€ฆ
  4. Sep 29, 2026๐—™๐—ฅ๐—˜๐—˜ ๐—ฅ๐—ฒ๐˜€๐—ผ๐˜‚๐—ฟ๐—ฐ๐—ฒ๐˜€ ๐—ง๐—ผ ๐—Ÿ๐—ฒ๐—ฎ๐—ฟ๐—ป ๐—”๐—œ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ๐Ÿš€ โ€‹ Explore 6 free resourceโ€ฆ
  5. Sep 29, 2026SQL Interview Series โ€” Part 1 Hi guys, let's start a SQL interview series covering frequenโ€ฆ
  6. Sep 28, 2026๐Ÿง  Real-World SQL Scenario-Based Questions & Answers 1. Get the 2nd highest salary from thโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’