-3

Got this question from a job interview via skype. I managed to take a screenshot of the question.

enter image description here I imagine it's the reverse of a RIGHT JOIN, but I can't figure it out.

EdwardC
  • 103
  • 9
  • 2
    Have you tried a query yet? – Tim Biegeleisen Apr 15 '18 at 07:46
  • 1
    A right join is just a left join written back to front – Strawberry Apr 15 '18 at 08:17
  • Possible duplicate of https://stackoverflow.com/q/2686254/3404097 – philipxy Apr 18 '18 at 03:11
  • Hi. Read & act on [ask], the downvote arrow mouseover text & hits googling 'stackexchange homework'. Always google many clear, concise & specific versions/phrasings of your question/problem/goal & read many answers. Add relevant keywords you discover to your searches. If you don't find an answer then post, using use one variant search for your title & keywords for your tags. Please [use text, not images/links, for text (including code, tables & ERDs)](https://meta.stackoverflow.com/q/285551/3404097). Use an image only for convenience to supplement text and/or for what can't be given in text. – philipxy Apr 18 '18 at 03:19

3 Answers3

2

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);
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

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
    );
0

Try this:

SELECT *
FROM department_table D
WHERE 
NOT EXISTS (SELECT NULL
            FROM student_table B
            WHERE A.department_id=B.department_id);
cdaiga
  • 4,861
  • 3
  • 22
  • 42