How do you write a query to find all employees who have the same job title as their managers?
Posted by AliceWk
Last Updated: June 07, 2024
To find all employees who have the same job title as their managers, you need to compare the job title of each employee with the job title of their respective manager. Assuming you have a table named employees with the following relevant columns: - employee_id: Unique identifier for each employee. - manager_id: Identifier for the employee's manager (which corresponds to another employee_id). - job_title: The job title of the employee. Here is a SQL query that achieves this:
SELECT e.employee_id, e.job_title, m.employee_id AS manager_id, m.job_title AS manager_job_title
FROM employees e
JOIN employees m ON e.manager_id = m.employee_id
WHERE e.job_title = m.job_title;
Explanation:
1. Table Aliases: We use aliases (e for employees and m for managers) to distinguish between the employee and manager rows in the same table. 2. JOIN Clause: We perform an inner join on the employees table to link each employee with their manager. The condition for the join is that the manager_id of the employee matches the employee_id of the manager. 3. WHERE Clause: This condition checks if the job_title of the employee (e.job_title) matches the job_title of the manager (m.job_title). 4. SELECT Clause: We select necessary columns to show which employees have the same job title as their managers. You may want to adjust the columns in the SELECT clause based on the specific details you want to retrieve.
Related Content