From the 3 tables below I need to find 'John', who has a bike but not a car. I'm trying to use this syntax
Select <> from TableA A left join TableB B on A.Key = B.Key where B.Key IS null
so in practise I create a left join from the two tables but i'm bit confused how the where B.Key IS null
fits in my query.
select t1.name from
(table1 t1 join table3 on table3.table1id = t1.id join table2 t2 on table3.table2id = t2.id)
left join
(table1 t11 join table3 on table3.table1id = t11.id join table2 t22 on table3.table2id = t22.id)
on t1.name = t11.name where t2.name = 'Bike' and t22.name = 'Car';
Table1
ID | NAME |
---|---|
1 | John |
2 | Nick |
Table2
ID | NAME |
---|---|
1 | Bike |
2 | Car |
Table3
table1ID | table2ID |
---|---|
1 | 1 |
2 | 1 |
2 | 2 |