List employees who earn more than their department manager's salary from the employees table (manager_id references employee id).
๐ ๐ฒ: Challenge accepted!
SELECT e1.name, e1.department, e1.salary
FROM employees e1
JOIN employees e2 ON e1.department = e2.department AND e1.manager_id = e2.id
WHERE e1.salary > e2.salary;
I self-joined the employees table on department and manager_id to compare each employee's salary against their manager's in the same department. The WHERE clause filters for employees earning more. This tests join mastery for hierarchical data, common in org charts.
๐ง๐ถ๐ฝ ๐ณ๐ผ๐ฟ ๐ฆ๐ค๐ ๐๐ผ๐ฏ ๐ฆ๐ฒ๐ฒ๐ธ๐ฒ๐ฟ๐:
Self-joins shine for intra-table relationships like managers/employees or siblingsโpractice with employee hierarchies to impress in data engineering interviews!
React with โค๏ธ for more