๐จ SQL Interview Challenge (Most Candidates Get This Wrong!)
Ques:
Can you write a query to find employees who earn more than the average salary of their own department?
๐ Sounds simpleโฆ but this is where many people slip.
Ans:
SELECT e.*
FROM employees e
JOIN (
SELECT department_id, AVG(salary) AS avg_salary
FROM employees
GROUP BY department_id
) d
ON e.department_id = d.department_id
WHERE e.salary > d.avg_salary;
๐ Why interviewers love this:
It tests your understanding of correlated logic, aggregation, and joins.
๐ก Key insight:
The comparison is done within each department, not across the entire table.
๐ If this clarified a tricky concept, react with ๐๐ฅ
๐ฒ Follow this channel for more advanced, query-based SQL interview questions ๐
Post #2014
1.72K
- โค 3