table tourist
table stay
table hotel
i want to print the names of the tourists who have been to hilton but not to continental so the tourists who have been to hilton and continental both shouldnt be printed
You can use exists
and not exists
:
select t.*
from tourists t
where
exists (
select 1
from stay s
inner join hotels h on h.hcode = s.hcode
where s.tcode = t.tcode and h.name = 'Hilton'
) and not exists (
select 1
from stay s
inner join hotels h on h.hcode = s.hcode
where s.tcode = t.tcode and h.name = 'Continental'
)