Customers who likes banana but not apple - Improve Query

You can use one condition directly thus removing one of sub-queries:

SELECT * 
FROM @SampleTable
WHERE Likes = 'Banana'
AND CustomerNumber NOT IN
(SELECT CustomerNumber FROM @SampleTable WHERE Likes = 'Apple')

Or do the same with EXISTS and correlated sub-query:

SELECT * 
FROM SampleTable t1
WHERE Likes = 'Banana'
AND NOT EXISTS 
(SELECT 1 
 FROM SampleTable t2 WHERE t2.Likes = 'Apple' 
                       AND t1.CustomerNumber=t2.CustomerNumber);

DEMO (based on MySQL)


Another way, but not necessarily better

with nuts as (
 select *,
  Max(case when likes='apple'  then 1 else 0 end) over(partition by customernumber) apple,
  Max(case when likes='banana' then 1 else 0 end) over(partition by customernumber) banana
 from SampleTable
)
select customernumber, likes, random
from nuts
where banana=1 and apple=0