SQL Programming
June 5, 2025 at 01:51 PM
Today, let's move to the next topic in the SQL Learning Series:
📊 *SQL Interview Series*
🧠 *Find the Second Highest Salary*
👨💻 *Sample Table: employees*
| id | name | salary |
|----|---------|--------|
| 1 | Alice | 5000 |
| 2 | Bob | 7000 |
| 3 | Charlie | 6000 |
| 4 | David | 7000 |
✅ *Method 1: Using LIMIT & DISTINCT (MySQL)*
```sql
SELECT DISTINCT salary
FROM employees
ORDER BY salary DESC
LIMIT 1 OFFSET 1;
```
🔹 *DISTINCT* removes duplicates
🔹 *OFFSET 1* skips the highest to fetch the second highest
✅ *Method 2: Using Subquery*
```sql
SELECT MAX(salary)
FROM employees
WHERE salary < (
SELECT MAX(salary) FROM employees
);
```
🔹 Inner query gets highest salary (7000)
🔹 Outer gets highest < 7000 ⇒ *6000*
*React with ❤️ if you're ready for the next quiz.*
SQL Learning Series: https://whatsapp.com/channel/0029VanC5rODzgT6TiTGoa1v/1075
❤️
👍
❤
🙏
😢
🇮🇳
😮
♥
🌂
😂
151