I have a table named foobar with columns name and location. I want use SQL to get all the names of people that have been to New York but have not been to San Francisco.
So far have:
select name
from foobar
where location = "New York" and location != "San Francisco"
group by name
SELECT f.name
FROM foobar f
WHERE f.location = 'New York'
AND NOT EXISTS(SELECT NULL
FROM foobar f2
WHERE f2.name = f.name
AND f2.location = 'San Francisco')
You could also do this with a LEFT JOIN:
SELECT f.name
FROM foobar f
LEFT JOIN foobar f2
ON f.name = f2.name
AND f2.location = 'San Francisco'
WHERE f.location = 'New York'
AND f2.name IS NULL
select name
from foobar
where location = "New York"
and name not in (select name
from foobar
where location = "San Francisco")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With