Search code examples
sqloracleselectoracle-sqldeveloperqsqlquery

oracle sql query select


table tourist

enter image description here

table stay

enter image description here

table hotel

enter image description here

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


Solution

  • 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'  
        )