Got this question from a job interview via skype. I managed to take a screenshot of the question.
I imagine it's the reverse of a RIGHT JOIN, but I can't figure it out.
Got this question from a job interview via skype. I managed to take a screenshot of the question.
I imagine it's the reverse of a RIGHT JOIN, but I can't figure it out.
One simple way of doing this via an anti-join:
SELECT d.*
FROM departments d
LEFT JOIN students s
ON d.DEPARTMENT_ID = s.DEPARTMENT_ID
WHERE
s.DEPARTMENT_ID IS NULL;
We can also use an EXISTS
query:
SELECT d.*
FROM departments d
WHERE NOT EXISTS (SELECT 1 FROM students s WHERE s.DEPARTMENT_ID = d.DEPARTMENT_ID);
You can do this by a using a subquery. Here are two example of MySQL query. You can try this.
SELECT *
FROM DEPARTMENT
WHERE
DEPERTMENT_ID NOT IN (
SELECT DEPERTMENT_ID
FROM STUDENTS
GROUP BY DEPERTMENT_ID)
Another way using where not exists:
SELECT d.*
FROM departments d
WHERE NOT EXISTS (
SELECT 1 FROM students s WHERE s.DEPARTMENT_ID = d.DEPARTMENT_ID
);
Try this:
SELECT *
FROM department_table D
WHERE
NOT EXISTS (SELECT NULL
FROM student_table B
WHERE A.department_id=B.department_id);