TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst ยท 76.7K subscribers
Post #2206 6.81K
โœ… SQL Window Functions ๐ŸชŸ๐Ÿ“Š

Window functions perform calculations across rows related to the current row without collapsing data, unlike GROUP BY.

1๏ธโƒฃ ROW_NUMBER()
Assigns a unique number to each row within 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() & LEAD()
Access previous or 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 vs previous/next values
(e.g., salary change, stock price movement).

4๏ธโƒฃ NTILE(n)
Divides rows into *n* equal buckets.

SELECT name,
NTILE(4) OVER (ORDER BY salary DESC) AS quartile
FROM employees;


๐Ÿ“Œ Use case: Quartiles & percentile-based analysis.


5๏ธโƒฃ Aggregates with OVER()
Running totals & partition-wise calculations.

SELECT name, dept_id, salary,
SUM(salary) OVER (PARTITION BY dept_id) AS dept_total
FROM employees;


๐Ÿง  Interview Q&A

Q1๏ธโƒฃ GROUP BY vs OVER()?
โ€ข GROUP BY โ†’ Collapses rows (one row per group)
โ€ข OVER() โ†’ Keeps all rows and adds calculated columns

Q2๏ธโƒฃ When to use LAG()?
To compare current row with previous data
(e.g., daily revenue change, previous month balance).

Q3๏ธโƒฃ No PARTITION BY used?
The function runs over the entire result set.

Q4๏ธโƒฃ Can we use ORDER BY inside OVER()?
โœ… Yes. Required for ranking, LAG/LEAD, running totals.

๐Ÿ’ฌ Double tap โค๏ธ & share for more SQL tips! ๐Ÿš€
  • โค 11
  • ๐Ÿ‘ 1
More from @sqlanalyst
  1. Oct 13, 2026SQL Interview Series โ€” Part 8 ๐Ÿ“Œ Question 8: Find Customers Who Have Never Placed an Orderโ€ฆ
  2. Oct 13, 2026๐—ง๐—ข๐—ฃ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ข๐—จ๐—ฅ๐—ฆ๐—˜๐—ฆ ๐—ง๐—ข ๐—•๐—ข๐—ข๐—ฆ๐—ง ๐—ฌ๐—ข๐—จ๐—ฅ ๐—ฆ๐—ž๐—œ๐—Ÿ๐—Ÿ๐—ฆ ๐—œ๐—ก ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ! Explorโ€ฆ
  3. Oct 12, 2026SQL Interview Series โ€” Part 6 ๐Ÿ“Œ Question 6: Find the Top 2 Highest-Paid Employees in Eachโ€ฆ
  4. Oct 12, 2026๐Ÿš€ ๐—ช๐—ต๐—ถ๐—ฐ๐—ต ๐—ง๐—ฒ๐—ฐ๐—ต ๐—ฆ๐—ธ๐—ถ๐—น๐—น ๐—ช๐—ถ๐—น๐—น ๐—ฌ๐—ผ๐˜‚ ๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ? Want to builโ€ฆ
  5. Oct 9, 2026SQL Interview Series โ€” Part 5 ๐Ÿ“Œ Question 5: Find Employees Who Earn More Than Their Managโ€ฆ
  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 โ†’