Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL: select all unique values in table A which are not in table B

Tags:

sql

mysql

I have table A

Id  | Name      | Department
-----------------------------
0   | Alice     | 1
0   | Alice     | 2
1   | Bob       | 1

and table B

Id  | Name
-------------
0   | Alice     

I want to select all unique Ids in table A which do not exist in table B. how can I do this?

like image 541
an0neemus Avatar asked Nov 15 '10 16:11

an0neemus


Video Answer


1 Answers

select distinct id 
from TableA a
where not exists (
    select id 
    from TableB 
    where id = a.id
)
like image 103
D'Arcy Rittich Avatar answered Oct 07 '22 00:10

D'Arcy Rittich