TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst ยท 76.7K subscribers
Post #2365 4.11K
๐Ÿš€ Window Functions โญ

Window functions are one of the most powerful SQL features used in data analytics, reporting, and advanced SQL interviews.

๐Ÿ‘‰ They allow you to perform calculations across rows without collapsing them like GROUP BY.

In simple words:
GROUP BY โ†’ reduces rows

Window Functions โ†’ keep rows but add calculated values

๐Ÿง  Basic Syntax of Window Functions
SELECT column, window_function() 
OVER (
PARTITION BY column
ORDER BY column
)
FROM table;

Components:
- OVER() โ†’ defines the window
- PARTITION BY โ†’ splits data into groups
- ORDER BY โ†’ defines calculation order

๐Ÿ”น 1๏ธโƒฃ ROW_NUMBER()

Assigns a unique sequential number to rows.
SELECT name, salary, ROW_NUMBER() OVER(ORDER BY salary DESC) AS rank 
FROM employees;

Result:
name | salary | rank
Rahul | 90000 | 1
Priya | 85000 | 2
Amit | 85000 | 3

๐Ÿ‘‰ Even if salaries are same, numbers stay unique.

๐Ÿ”น 2๏ธโƒฃ RANK()

Assigns rank but skips numbers when ties occur.
SELECT name, salary, RANK() OVER(ORDER BY salary DESC) AS rank 
FROM employees;

Result:
name | salary | rank
Rahul | 90000 | 1
Priya | 85000 | 2
Amit | 85000 | 2
Neha | 80000 | 4

Notice rank 3 is skipped.

๐Ÿ”น 3๏ธโƒฃ DENSE_RANK()

Similar to RANK but does not skip numbers.
SELECT name, salary, DENSE_RANK() OVER(ORDER BY salary DESC) AS rank 
FROM employees;

Result:
name | salary | rank
Rahul | 90000 | 1
Priya | 85000 | 2
Amit | 85000 | 2
Neha | 80000 | 3

๐Ÿ”น 4๏ธโƒฃ PARTITION BY

Used to divide rows into groups before calculation.

Example: Rank employees within each department
SELECT name, department, salary, 
RANK() OVER(
PARTITION BY department
ORDER BY salary DESC
) AS dept_rank
FROM employees;

๐Ÿ‘‰ Each department gets its own ranking.

๐Ÿ”น 5๏ธโƒฃ LAG()

Used to access previous row values.

Example: Compare sales with previous day.
SELECT date, sales, LAG(sales) OVER(ORDER BY date) AS previous_sales 
FROM sales;

๐Ÿ”น 6๏ธโƒฃ LEAD()

Used to access next row values.
SELECT date, sales, LEAD(sales) OVER(ORDER BY date) AS next_sales 
FROM sales;

โญ Real Data Analyst Examples

Top 3 highest salaries
SELECT ** 
FROM (
SELECT name, salary, ROW_NUMBER() OVER(ORDER BY salary DESC) AS rn
FROM employees
) t
WHERE rn <= 3;

Running total of sales
SELECT date, sales, SUM(sales) OVER(ORDER BY date) AS running_total 
FROM sales;

Rank products by category
SELECT product_name, category, price, 
RANK() OVER(PARTITION BY category ORDER BY price DESC) AS rank
FROM products;

๐ŸŽฏ Common Interview Questions
โœ” Difference between ROW_NUMBER, RANK, DENSE_RANK
โœ” Find Nth highest salary
โœ” Running totals using window functions
โœ” Compare current row with previous row
โœ” Rank employees by department

๐Ÿš€ Mini Practice Tasks
Task 1: Assign row numbers to employees by salary.
Task 2: Rank employees by salary.
Task 3: Find top 3 highest salaries using window functions.
Task 4: Calculate running total of sales.

๐Ÿ’ผ What You Must Master
โœ… ROW_NUMBER()
โœ… RANK()
โœ… DENSE_RANK()
โœ… PARTITION BY
โœ… LAG() / LEAD()
โœ… Running totals

These functions are used heavily in real analytics queries and SQL interviews.

Double Tap โ™ฅ๏ธ For More
  • โค 12
More from @sqlanalyst
  1. Oct 12, 2026๐Ÿš€ ๐—ช๐—ต๐—ถ๐—ฐ๐—ต ๐—ง๐—ฒ๐—ฐ๐—ต ๐—ฆ๐—ธ๐—ถ๐—น๐—น ๐—ช๐—ถ๐—น๐—น ๐—ฌ๐—ผ๐˜‚ ๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ? Want to builโ€ฆ
  2. Oct 9, 2026SQL Interview Series โ€” Part 5 ๐Ÿ“Œ Question 5: Find Employees Who Earn More Than Their Managโ€ฆ
  3. Oct 9, 2026๐Ÿ‡ฎ๐Ÿ‡ณ ๐—š๐—ข๐—ฉ๐—˜๐—ฅ๐—ก๐— ๐—˜๐—ก๐—ง ๐—ข๐—™ ๐—œ๐—ก๐——๐—œ๐—” โ€” ๐—”๐—œ๐—–๐—ง๐—˜ ๐—œ๐—ก๐—ง๐—˜๐—ฅ๐—ก๐—ฆ๐—›๐—œ๐—ฃ๐—ฆ ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ ๐Ÿš€โ€ฆ
  4. Oct 8, 2026๐ŸŽ“ ๐— ๐—ถ๐—ฐ๐—ฟ๐—ผ๐˜€๐—ผ๐—ณ๐˜ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€ ๐˜„๐—ถ๐˜๐—ต ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ฒ๐˜€! ๐Ÿš€๐Ÿ”ฅ Upgrโ€ฆ
  5. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  6. Oct 7, 2026SQL Interview Series โ€” Part 4 ๐Ÿ“Œ Question 4: Find the Highest Salary in Each Department Suโ€ฆ
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 โ†’