Trips
Customer_id src_id dest_id
c1 1 2
c1 2 1
c2 1 2
c2 2 3
Location
id name
1 Airports
2 Movies
3 Market
Is there any other good way than using EXCEPT
(select cust_id from trips t
join location l
on src_id = id or dest_id=id
where name='Airport' )
except
(
select cust_id from trips t
left join location l
on src_id =l.id
left join location l2
on dest_id = l2.id
where l.name not in('Airport') and l2.name not in('Airport')
)
So the question was 1)Customers who used trips for Airports Follow up question : 2) Customers who used trips for only Airports?
For Question 1 Output would be c1 and c2 but for Question 2 it should be only c1 because c2 used trips for locations other than airports. I used EXCEPT but the interviewer mentioned I can some SQL function to remove c2 record and can be done without additional joins. I want to know if there is any function I am not aware of here.
More broad question: How to create queries to get ONLY records with conditions?