Skip to main content

SQL Interview Question - 1


Consider a table named Employee with the following schema:
CREATE TABLE Employee (
    emp_id INT PRIMARY KEY,
    emp_name VARCHAR(100),
    department VARCHAR(100),
    salary DECIMAL(10, 2),
    hire_date DATE
);

Write an SQL query to find the employee(s) with the highest salary within each department.

Sample Table:


emp_id emp_name department salary hire_date
1 Alice Sales 50000 2020-01-01
2 Bob Sales 55000 2019-05-15
3 Charlie HR 60000 2020-03-20
4 David HR 58000 2018-11-10
5 Emma Marketing 52000 2021-02-28
6 Frank Marketing 53000 2019-08-10

Solution:

SELECT 
    emp_id,
    emp_name,
    department,
    salary
FROM 
    Employee e
WHERE 
    (department, salary) IN (
        SELECT 
            department,
            MAX(salary)
        FROM 
            Employee
        GROUP BY 
            department
    );

 

Explanation:

  • The inner query (SELECT department, MAX(salary) FROM Employee GROUP BY department) finds the maximum salary for each department.
  • The outer query selects all rows from the Employee table where the (department, salary) tuple matches those found by the inner query.
  • This effectively retrieves the employee(s) with the highest salary within each department.

Share your alternate solution in comments.


Check our offerings below!


Success Stories

Real outcomes from learners who followed the process



See all the Success Stories - here
Testimonials - here


You can check out other TakeOff Talent offerings that have helped 8,000+ people land jobs.

Offerings
📄 CV Review
📘 200 most-asked SQL interview questions with detailed solutions
📘 200 most-asked Python interview questions with detailed solutions
📊 SQL Crash Course
✍️ CV Writing for freshers
✍️ CV Writing
🛠️ Portfolio Project
🗣️ English Speaking Practice (Live 1:1)
🎯 Job Search Mentorship Package

  In case of any questions around services above, write to us at vibhanshu@takeofftalent.com

  Connect with our founder on Linkedin - https://www.linkedin.com/in/vibvibgyor/



Video Gallery



Check more videos here>>