๐ Q1. How to find the 2nd highest salary from a table?
โ Answer:
SELECT MAX(salary) FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);
๐ Q2. How to find duplicate values in a column?
โ Answer:
SELECT name, COUNT(*) FROM employees
GROUP BY name
HAVING COUNT(*) > 1;
๐ Q3. How to select records that exist in one table but not in another?
โ Answer:
SELECT * FROM employees
WHERE id NOT IN (SELECT employee_id FROM payroll);
๐ Q4. How to get the top 3 highest salaries? (MySQL)
โ Answer:
SELECT DISTINCT salary FROM employees
ORDER BY salary DESC
LIMIT 3;
๐ Q5. How to fetch employees with the same salary?
โ Answer:
SELECT * FROM employees e1
WHERE EXISTS (
SELECT 1 FROM employees e2
WHERE e1.salary = e2.salary AND e1.id <> e2.id
);
๐ Q6. How to get the department-wise highest salary?
โ Answer:
SELECT department, MAX(salary) AS max_salary
FROM employees
GROUP BY department;
๐ฌ Tap โค๏ธ for more!