TGViewer
Data Analytics Data Analytics @sqlspecialist ยท 111K subscribers
Post #2459 9.36K
โœ… SQL Window Functions ๐ŸชŸ๐Ÿ“Š

Window functions perform calculations across rows related to the current row without collapsing them like GROUP BY does.

1๏ธโƒฃ ROW_NUMBER()
Gives a unique number to each row in a partition.
SELECT name, dept_id,
ROW_NUMBER() OVER (
PARTITION BY dept_id
ORDER BY salary DESC
) AS rank
FROM employees;

๐Ÿ“Œ Use case: Rank employees by salary within each department.

2๏ธโƒฃ RANK() vs DENSE_RANK()
โฆ RANK() โ†’ Skips numbers on ties (1, 2, 2, 4)
โฆ DENSE_RANK() โ†’ No gaps (1, 2, 2, 3)
SELECT name, salary,
RANK() OVER (ORDER BY salary DESC) AS rnk,
DENSE_RANK() OVER (ORDER BY salary DESC) AS dense_rnk
FROM employees;


3๏ธโƒฃ LAG() and LEAD()
Access previous/next row values.
SELECT name, salary,
LAG(salary) OVER (ORDER BY id) AS prev_salary,
LEAD(salary) OVER (ORDER BY id) AS next_salary
FROM employees;

๐Ÿ“Œ Use case: Compare current row to previous/next (e.g., salary or stock change).

4๏ธโƒฃ NTILE(n)
Divides rows into n buckets.
SELECT name,
NTILE(4) OVER (ORDER BY salary DESC) AS quartile
FROM employees;

๐Ÿ“Œ Use case: Quartiles/percentile-style grouping.

5๏ธโƒฃ SUM(), AVG(), COUNT() with OVER()
Running totals, partition-wise aggregates, moving stats.
SELECT name, dept_id, salary,
SUM(salary) OVER (PARTITION BY dept_id) AS dept_total
FROM employees;



๐Ÿง  Interview Q&A

Q1: Difference between GROUP BY and OVER()?
โฆ GROUP BY โ†’ Collapses rows into groups; one row per group.
โฆ OVER() โ†’ Keeps all rows; adds an extra column with the aggregate.

Q2: When would you use LAG()?
To compare current row values with previous ones (e.g., dayโ€‘toโ€‘day revenue change, previous monthโ€™s balance).

Q3: What happens if no PARTITION BY is used?
The function runs over the entire result set as a single partition.

Q4: Can you sort inside OVER()?
Yes, ORDER BY inside OVER() defines the calculation order (needed for ranking, LAG/LEAD, running totals).

๐Ÿ’ฌ Double Tap โค๏ธ for more!
  • โค 18
  • ๐Ÿ‘ 3
More from @sqlspecialist
  1. Oct 12, 20268๏ธโƒฃ You receive a dataset with inconsistent date formats. How would you clean it? Sample Aโ€ฆ
  2. Oct 12, 2026๐Ÿ“Š Data Analyst Interview Series โ€” Part 7 Guys, let's continue our Data Analyst Interviewโ€ฆ
  3. Oct 12, 2026๐Ÿš€ ๐—ช๐—ต๐—ถ๐—ฐ๐—ต ๐—ง๐—ฒ๐—ฐ๐—ต ๐—ฆ๐—ธ๐—ถ๐—น๐—น ๐—ช๐—ถ๐—น๐—น ๐—ฌ๐—ผ๐˜‚ ๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ? Want to builโ€ฆ
  4. Oct 9, 2026โ€œHere, the data is sorted by the second column in descending order and the first five rowsโ€ฆ
  5. Oct 9, 2026๐Ÿ“Š Data Analyst Interview Series โ€” Part 6 Guys, let's continue our Data Analyst Interviewโ€ฆ
  6. Oct 9, 2026๐Ÿ‡ฎ๐Ÿ‡ณ ๐—š๐—ข๐—ฉ๐—˜๐—ฅ๐—ก๐— ๐—˜๐—ก๐—ง ๐—ข๐—™ ๐—œ๐—ก๐——๐—œ๐—” โ€” ๐—”๐—œ๐—–๐—ง๐—˜ ๐—œ๐—ก๐—ง๐—˜๐—ฅ๐—ก๐—ฆ๐—›๐—œ๐—ฃ๐—ฆ ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ ๐Ÿš€โ€ฆ
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 โ†’