I’m doing some practice problems on another site as extra work to supplement my coursework here, and I’ve run into a minor issue. I was given this problem:
Write a query to find the name (first_name, last_name) of the employees who are not supervisors.
The sample table they have you use is built as follows:
(relevant) columns: first_name, last_name, manager_id
The way I handled this was first approaching it with the central logic that in order for an employee to not be a supervisor, their employee_id cannot be someone else’s manager_id, as manager_id is referring to that employee’s supervisor.
Given that, my solution was as follows:
SELECT first_names, last_names FROM employees
WHERE employee_id NOT IN (SELECT manager_id FROM employees);
The site’s solution was as follows
SELECT b.first_names, b.Last_names FROM employees b
WHERE NOT EXISTS (SELECT ‘X’ FROM employees a
WHERE a.manager_id = b.employee.id
After looking through the results of both, my solution got to the same result as the website’s suggested solution. My question to you is this: Are both of these really the same? Am I okay solving it the way I did? Because my method seems less complex and thus would take a shorter time to execute, but if this is a case where I used the wrong method to get to the right answer, I’d like to know.
Thanks in advance for the help! I just really need some people to talk to and bounce ideas off of.