TGViewer
Data Analytics Data Analytics @sqlspecialist · 111K subscribers
Post #2338 6.66K
Interviewer: Show me top 3 highest-paid employees per department.

Me: Sure, let’s use ROW_NUMBER() for this!

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


✅ I used a window function to rank employees by salary within each department.
Then filtered the top 3 using a subquery.

🧠 Key Concepts:
- ROW_NUMBER()
- PARTITION BY → resets ranking per department
- ORDER BY → sorts by salary (highest first)

📝 Real-World Tip:
These kinds of queries help answer questions like:
– Who are the top earners by team?
– Which stores have the best sales staff?
– What are the top-performing products per category?

💬 Tap ❤️ for more!
  • ❤ 19
More from @sqlspecialist
  1. Oct 13, 2026📊 Data Analyst Interview Series — Part 9 Guys, let's continue our Data Analyst Interview…
  2. Oct 13, 2026𝗧𝗢𝗣 𝗙𝗥𝗘𝗘 𝗖𝗢𝗨𝗥𝗦𝗘𝗦 𝗧𝗢 𝗕𝗢𝗢𝗦𝗧 𝗬𝗢𝗨𝗥 𝗦𝗞𝗜𝗟𝗟𝗦 𝗜𝗡 𝟮𝟬𝟮𝟲! Explor…
  3. Oct 12, 20268️⃣ You receive a dataset with inconsistent date formats. How would you clean it? Sample A…
  4. Oct 12, 2026📊 Data Analyst Interview Series — Part 7 Guys, let's continue our Data Analyst Interview…
  5. Oct 12, 2026🚀 𝗪𝗵𝗶𝗰𝗵 𝗧𝗲𝗰𝗵 𝗦𝗸𝗶𝗹𝗹 𝗪𝗶𝗹𝗹 𝗬𝗼𝘂 𝗠𝗮𝘀𝘁𝗲𝗿 𝗶𝗻 𝟮𝟬𝟮𝟲? Want to buil…
  6. Oct 9, 2026“Here, the data is sorted by the second column in descending order and the first five rows…
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 →