How to avoid duplicates of a column using distinct?
I want to get the passports from the table RELATION that doesn't exist to the table PERSON. This it works using the NOT EXISTS.
But I also want to print the relID (it doesn't matter which from each passport).
these are my tables:
RELATION
relID passport date
1 400000V 21/07/2019
2 400000V 31/02/2019
3 400000V 31/07/2011
4 342342X 31/12/2012
5 342342X 11/10/2011
6 823972O 31/07/2019
Table PERSON:
id passport
1 342342X
2 3333333T
3 1111111W
This is the result I want.
relID passport
1 400000V
6 823972O
Thats my query:
select
distinct passport
from RELATION
where not exists(select 1 from PERSON where PERSON.passport= RELATION.passport)
If I add the column relID to the select i got duplicates values from passport.
I only want to get unique PASSPORT with one of their relID .