TGViewer
Data Analytics Data Analytics @sqlspecialist ยท 111K subscribers
Post #2752 6.54K
๐Ÿ”ฅ Now, letโ€™s move to the next topic:

โœ… Window Functions

๐Ÿง  1. What are Window Functions?

Window functions perform calculations without grouping rows

๐Ÿ‘‰ Difference:
โ€ข GROUP BY โ†’ reduces rows
โ€ข Window Function โ†’ keeps all rows + adds extra column

๐Ÿ“Š Example Table

name โ†’ Amit, Ravi, Neha
department โ†’ IT, IT, HR
salary โ†’ 60000, 70000, 40000

โšก 2. Basic Syntax
SELECT column,
FUNCTION() OVER (PARTITION BY column ORDER BY column)
FROM table;

๐Ÿ”ฅ 3. ROW_NUMBER()

Assigns unique rank to each row

SELECT name, department, salary,
ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS rank
FROM employees;

โœ” Rank employees within each department

๐Ÿฅ‡ 4. RANK() vs DENSE_RANK()

๐Ÿ‘‰ RANK() (skips numbers)

SELECT name, salary,
RANK() OVER (ORDER BY salary DESC) AS rank
FROM employees;

๐Ÿ‘‰ DENSE_RANK() (no skipping)

SELECT name, salary,
DENSE_RANK() OVER (ORDER BY salary DESC) AS rank
FROM employees;

๐Ÿ“Š Visual Difference

If salaries are 100, 90, 90, 80:
โ€ข RANK() gives: 1, 2, 2, 4
โ€ข DENSE_RANK() gives: 1, 2, 2, 3

โšก 5. PARTITION BY (Very Important)

๐Ÿ‘‰ Splits data into groups (like GROUP BY but without collapsing rows)

SELECT department, name, salary,
AVG(salary) OVER (PARTITION BY department) AS avg_salary
FROM employees;

โœ” Shows avg salary per department for each row

๐ŸŽฏ 6. Practice Tasks
1. Rank employees by salary
2. Rank employees within each department
3. Find highest salary per department
4. Add average salary column per department
5. Find second highest salary using window function

โœ… Practice Tasks Solution

โœ… 1. Rank employees by salary

SELECT name, salary,
ROW_NUMBER() OVER (ORDER BY salary DESC) AS rank
FROM employees;

โœ… 2. Rank employees within each department

SELECT name, department, salary,
ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS rank
FROM employees;

โœ… 3. Find highest salary per department

SELECT name, department, salary
FROM (
SELECT name, department, salary,
ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS rn
FROM employees
) t
WHERE rn = 1;

โœ… 4. Add average salary column per department

SELECT name, department, salary,
AVG(salary) OVER (PARTITION BY department) AS avg_salary
FROM employees;

โœ… 5. Find second highest salary using window function

SELECT name, salary
FROM (
SELECT name, salary,
DENSE_RANK() OVER (ORDER BY salary DESC) AS rnk
FROM employees
) t
WHERE rnk = 2;

โšก Mini Challenge ๐Ÿ”ฅ
๐Ÿ‘‰ Get top 2 highest paid employees in each department

โšก Mini Challenge Solution ๐Ÿ”ฅ

SELECT name, department, salary
FROM (
SELECT name, department, salary,
ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS rn
FROM employees
) t
WHERE rn <= 2;

๐Ÿ”ฅ Pro Tip:
โ€ข Use ROW_NUMBER() โ†’ unique ranking
โ€ข Use DENSE_RANK() โ†’ handle ties

Double Tap โค๏ธ For More
  • โค 23
More from @sqlspecialist
  1. Oct 9, 2026โ€œHere, the data is sorted by the second column in descending order and the first five rowsโ€ฆ
  2. Oct 9, 2026๐Ÿ“Š Data Analyst Interview Series โ€” Part 6 Guys, let's continue our Data Analyst Interviewโ€ฆ
  3. Oct 9, 2026๐Ÿ‡ฎ๐Ÿ‡ณ ๐—š๐—ข๐—ฉ๐—˜๐—ฅ๐—ก๐— ๐—˜๐—ก๐—ง ๐—ข๐—™ ๐—œ๐—ก๐——๐—œ๐—” โ€” ๐—”๐—œ๐—–๐—ง๐—˜ ๐—œ๐—ก๐—ง๐—˜๐—ฅ๐—ก๐—ฆ๐—›๐—œ๐—ฃ๐—ฆ ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ ๐Ÿš€โ€ฆ
  4. Oct 8, 2026๐ŸŽ“ ๐— ๐—ถ๐—ฐ๐—ฟ๐—ผ๐˜€๐—ผ๐—ณ๐˜ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€ ๐˜„๐—ถ๐˜๐—ต ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ฒ๐˜€! ๐Ÿš€๐Ÿ”ฅ Upgrโ€ฆ
  5. Oct 7, 2026๐Ÿ“Š Kandinsky 6.0 Video: AI-Powered Content Creation for Analysts The new Kandinsky 6.0 Vidโ€ฆ
  6. Oct 7, 2026Alternatively, depending on the Excel version and requirement, I could use functions suchโ€ฆ
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 โ†’