Write an SQL query to display Employee Name, Department Name, and Salary, and another query to find the third highest salary from the Employee table.
💡 Model Answer
To display the employee name, department name, and salary, you need to join the Employee and Department tables on dept_id. The query is:
SELECT e.emp_name, d.dept_name, e.salary
FROM Employee e
JOIN Department d ON e.dept_id = d.dept_id;
For the third highest salary, you can use a subquery that orders distinct salaries in descending order and limits the result to the third row. One concise way is:
SELECT DISTINCT salary
FROM Employee
ORDER BY salary DESC
LIMIT 1 OFFSET 2;
Alternatively, you can use a window function to rank salaries and then filter for rank = 3:
SELECT salary
FROM (
SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rnk
FROM Employee
) AS ranked
WHERE rnk = 3;
Both approaches run in O(n log n) time due to the sort, and they handle duplicate salaries correctly by using DISTINCT or DENSE_RANK().
This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.
🎤 Get questions like this answered in real-time
Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.
Get Assisting AI — Starts at ₹500