๐ ๐ ๐ฒ: 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!