TGViewer
Data Analytics Data Analytics @sqlspecialist ยท 111K subscribers
Post #2448 11.6K
๐Ÿ“Š ๐—œ๐—ป๐˜๐—ฒ๐—ฟ๐˜ƒ๐—ถ๐—ฒ๐˜„๐—ฒ๐—ฟ: How do you get the 2nd highest salary in SQL?

๐Ÿ‘‹ ๐— ๐—ฒ: Use ORDER BY with LIMIT or OFFSET, or a subquery.

MySQL / PostgreSQL (with LIMIT & OFFSET):
SELECT salary  
FROM employees
ORDER BY salary DESC
LIMIT 1 OFFSET 1;


Using Subquery (Works on most databases):
SELECT MAX(salary)  
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);


๐Ÿง  Logic Breakdown:
- First method sorts and skips the top result
- Second method finds the highest salary below the max

๐Ÿ’ก Tip: Use DENSE_RANK() if multiple employees share the same salary rank

๐Ÿ’ฌ Tap โค๏ธ for more!
  • โค 29
  • ๐Ÿ‘Œ 2
More from @sqlspecialist
  1. Oct 12, 20268๏ธโƒฃ You receive a dataset with inconsistent date formats. How would you clean it? Sample Aโ€ฆ
  2. Oct 12, 2026๐Ÿ“Š Data Analyst Interview Series โ€” Part 7 Guys, let's continue our Data Analyst Interviewโ€ฆ
  3. Oct 12, 2026๐Ÿš€ ๐—ช๐—ต๐—ถ๐—ฐ๐—ต ๐—ง๐—ฒ๐—ฐ๐—ต ๐—ฆ๐—ธ๐—ถ๐—น๐—น ๐—ช๐—ถ๐—น๐—น ๐—ฌ๐—ผ๐˜‚ ๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ? Want to builโ€ฆ
  4. Oct 9, 2026โ€œHere, the data is sorted by the second column in descending order and the first five rowsโ€ฆ
  5. Oct 9, 2026๐Ÿ“Š Data Analyst Interview Series โ€” Part 6 Guys, let's continue our Data Analyst Interviewโ€ฆ
  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 โ†’