Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sql

sql-server

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?

like image 313
Nissus Avatar asked Sep 16 '25 14:09

Nissus


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.

like image 164
Gordon Linoff Avatar answered Sep 19 '25 06:09

Gordon Linoff