TGViewer
Data Analytics Data Analytics @sqlspecialist ยท 111K subscribers
Post #2904 4.94K
๐—œ๐—ป๐˜๐—ฒ๐—ฟ๐˜ƒ๐—ถ๐—ฒ๐˜„๐—ฒ๐—ฟ:
You have 2 minutes to solve this SQL query.

Find the third highest salary from the employees table without using LIMIT or TOP.

๐— ๐—ฒ: Challenge accepted! ๐Ÿ’ช

SELECT
salary
FROM (
SELECT
salary,
DENSE_RANK() OVER (ORDER BY salary DESC) AS salary_rank
FROM employees
) ranked
WHERE salary_rank = 3;

๐Ÿ’ก Explanation:
This query uses the DENSE_RANK() window function to rank distinct salary values in descending order.

โ€ข ORDER BY salary DESC assigns Rank 1 to the highest salary.
โ€ข DENSE_RANK() ensures duplicate salaries receive the same rank.
โ€ข The outer query filters for salary_rank = 3, returning the third highest distinct salary.

๐ŸŽฏ Expected Output Example
Salary Rank
95,000 1
90,000 2
85,000 3

Output:
Third Highest Salary
85,000

๐Ÿš€ Alternative Solution Using a Correlated Subquery
SELECT DISTINCT salary
FROM employees e1
WHERE 2 = (
SELECT COUNT(DISTINCT salary)
FROM employees e2
WHERE e2.salary > e1.salary
);

This solution avoids window functions and is commonly asked to test your understanding of correlated subqueries.

๐Ÿš€ Questions involving the Nth highest or Nth lowest value are interview favorites. Be comfortable solving them using:

1. DENSE_RANK()
2. RANK()
3. Correlated Subqueries
4. Common Table Expressions (CTEs)

Being able to provide multiple approaches leaves a strong impression on interviewers.

โค๏ธ React with โค๏ธ for more interview challenges!
  • โค 17
More from @sqlspecialist
  1. Oct 8, 2026๐ŸŽ“ ๐— ๐—ถ๐—ฐ๐—ฟ๐—ผ๐˜€๐—ผ๐—ณ๐˜ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€ ๐˜„๐—ถ๐˜๐—ต ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ฒ๐˜€! ๐Ÿš€๐Ÿ”ฅ Upgrโ€ฆ
  2. Oct 7, 2026๐Ÿ“Š Kandinsky 6.0 Video: AI-Powered Content Creation for Analysts The new Kandinsky 6.0 Vidโ€ฆ
  3. Oct 7, 2026Alternatively, depending on the Excel version and requirement, I could use functions suchโ€ฆ
  4. Oct 7, 2026๐Ÿ“Š Data Analyst Interview Series โ€” Part 5 Guys, let's continue our Data Analyst Interviewโ€ฆ
  5. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  6. Oct 7, 2026๐Ÿ”Ÿ What is the difference between UNION and JOIN? Sample Answer: "JOIN combines columns frโ€ฆ
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 โ†’