๐๐ป๐๐ฒ๐ฟ๐๐ถ๐ฒ๐๐ฒ๐ฟ:
You have 2 minutes to solve this SQL query.
Find employees whose salary is higher than their manager's salary.
Assume the table structure is:
employees(employee_id, employee_name, manager_id, salary)
๐ ๐ฒ: Challenge accepted! ๐ช
SELECT
e.employee_id,
e.employee_name,
e.salary AS employee_salary,
m.employee_name AS manager_name,
m.salary AS manager_salary
FROM employees e
JOIN employees m
ON e.manager_id = m.employee_id
WHERE e.salary > m.salary;
๐ก Explanation:
This query uses a self join because both employees and managers are stored in the same table.
โข e represents the employee.
โข m represents the manager.
โข The join matches each employee with their manager using manager_id.
โข The WHERE clause filters employees whose salary is greater than their manager's salary.
This question tests your understanding of:
โ
Self Joins
โ
Aliases (e and m)
โ
Comparing values across related rows
๐ฏ Expected Output Example
Employee Employee Salary Manager Manager Salary
John 90,000 David 80,000
Sarah 85,000 Michael 75,000
๐ Self joins are one of the most frequently asked SQL interview topics. Practice scenarios involving employees, managers, organizational hierarchies, categories, and parent-child relationships.
โค๏ธ React with โค๏ธ for more SQL interview challenges!
Post #2894
5.03K
- โค 17