Dejan Dejan - 5 months ago 8
SQL Question

SQL query- ID not exist in another table or exist but with all records are in history?

i have table T1

ID
1
2
3


and table T2

ID HISTORY
1 1
1 1
2 1
2 0


I must select from T1 all records which does not exist in T2 or exists but all records are in history (history flag =1)

So for this my result will be

1
3


What is the correct SQL query for this?
Thanks

Answer

try this:

select * from T1
where ID not in(select ID from T2 where HISTORY_FLG!=1)


SQL Fiddle demo