Note that I have similar topics with different questions,
I'm learning MySQL
. I came across some multiple-choice questions.
If I doubted what the answer was or was not convinced it's correct I started searching google, stackoverflow and the mysql site.
However for some I still couldn't confirm what the correct answer was.
Therefore I ask the question here.
Question:
Table Employees
is created. You want to retrieve the information of those employees who have at least one person reporting to them. Which of the following queries will you execute to accomplish the task?
A.
SELECT employee_id, last_name, job_id, department_id FROM Employees WHERE employee_id EXISTS(SELECT manager_id WHERE manager_id is NULL);
B.
SELECT employee_id, last_name, job_id, department_id FROM Employees HAVING employee_id IN(SELECT manager_id FROM Employees WHERE manager_id is NOT NULL);
C.
SELECT employee_id, last_name, job_id, department_id FROM Employees outer WHERE EXISTS (SELECT 'x' FROM Employees WHERE manager_id = outer.employee_id);
D.
SELECT employee_id, last_name, job_id, department_id FROM Employees HAVING employee_id WHERE employee_id IN (SELECT manager_id WHERE manager_id is NOT NULL);
- A and D don't have a
FROM
clause in thesubquery
. I guess this is invalid (or just a typo in the question?) - The proposed answer is C. However I believe that this can't work because the reserved keyword "outer" is used as an alias here. Please correct me if I'm wrong.
- So actually only B is left here.
- Answer D order is incorrect. (
WHERE
should be beforeHAVING
) - Answer A is invalid because the
subquery
queries forIS NULL
.
- A and D don't have a
If you have any remark about this question/answer or for any of my remarks I'm happy to hear it!