# @aartii.py on Instagram

- **Type:** Video
- **Original URL:** https://www.instagram.com/p/DSki1_rjVzj
- **Gondola URL:** https://gondola.cc/posts/59916551-aartiipy-instagram
- **Thumbnail:** https://img.gondola.cc/tr:w-,h-,fo-auto/postThumbnails/211df1a63c.jpg
- **Posted:** 2025-12-22T15:41:30.000+00:00
- **Account Owner:** Aarti Sinha (@aartii.py) — https://gondola.cc/aartii.py

## Caption

Day 4/7 - SQL Challenge 🎯
TODAY’S QUESTION:
Rank employees by salary within each department.
TABLE:
Employees (id, name, department, salary)
THE SOLUTION:
SELECT 
  name,
 department,
 salary,
 RANK() OVER (
 PARTITION BY department 
  ORDER BY salary DESC
 ) as salary_rank
FROM Employees;
BREAKDOWN:
RANK() - Assigns ranking (1, 2, 3...)
OVER - Defines the window
PARTITION BY department - Separate ranking per department
ORDER BY salary DESC - Highest salary = Rank 1
OUTPUT EXAMPLE:
name | department | salary | salary_rank
-———|————|———|————
Alice | Sales | 90000 | 1
Bob | Sales | 80000 | 2
Charlie | Engineer | 95000 | 1
Diana | Engineer | 85000 | 2
RANK vs ROW_NUMBER vs DENSE_RANK:
— RANK: 1, 2, 2, 4 (skips after tie)
— DENSE_RANK: 1, 2, 2, 3 (no skip)
— ROW_NUMBER: 1, 2, 3, 4 (no ties)
WHEN TO USE EACH:
RANK: When you want to skip ranks after ties
DENSE_RANK: When you want consecutive ranks
ROW_NUMBER: When you need unique numbers
COMMON MISTAKES:
❌ Forgetting PARTITION BY (ranks across entire table)
❌ Wrong ORDER BY direction (ASC vs DESC)
❌ Not understanding RANK vs ROW_NUMBER
WHY THIS IS CRITICAL:
Window functions are in 80% of FAANG SQL interviews. Master these:
RANK, DENSE_RANK, ROW_NUMBER
LAG, LEAD
SUM/AVG OVER (running totals)
INTERVIEW TIP:
If question says “top N per group,” think WINDOW FUNCTIONS.
Day 4/7 complete ✅
Window functions = SQL superpower
#SQLChallenge #Day4 #WindowFunctions #DataScience #SQL FAANG

## Stats

- **Views:** 3,775
- **Likes:** 317
- **Shares:** 0
- **Comments:** 9

## Tags

sqlchallenge, day4, windowfunctions, sql, datascience

---
Copyright (c) Gondola