What's the difference between 'ANY' and 'EXISTS' in sql-server

Viewed 2511

When I'm running

select code from account where exists (select account from store)
except 
select code from account where code = any (select account from store)

it brings me 2 results which means that they are not the same. From my research, I haven't found any difference between them. 'Any' is rarely used from what I can find. Can anyone help me on this?

1 Answers

The two queries are quite different.

The first query returns all rows or no rows depending on whether the subquery returns any rows at all or no rows.

You intend a correlated subquery:

select code from account where exists (select 1 from store where store.account = account.code)

These should be equivalent.

Related