Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL IN

Author  Topic 

scmay
Starting Member

22 Posts

Posted - 2007-07-06 : 03:02:38
Dear all, Just wondering if there is any difference between using IN keyword as oppose to the WHERE = in an SQL statement (given in sql1 and sql2). In sql1, does it look for LA AND SD or either LA OR SD? My google search tells me its AND, but just double checking.

sql1:
SELECT * FROM Store_Information WHERE store_name IN ('Los Angeles', 'San Diego')

sql2:
SELECT * FROM Store_Information WHERE store_name ='Los Angeles' AND store_name= 'San Diego'

PS: Would appreciate if you could tell me the best practice too, when to use either of the statements in real world. Thanks!

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-07-06 : 03:12:40
The two statements are not the same.
An IN check is just a collection of OR checks.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-07-06 : 03:13:50
Did you try running both of these queries? I would be surprised if you get output for the second query at all.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-07-06 : 03:16:01
I prefer IN whan having more WHERE checks to do..

SELECT ...
FROM ...
WHERE Col1 IN ('a', 'b')
AND Col2 = 'Peso'
AND Col3 IN (1, 3, 4, 5, 10)

instead of

SELECT ...
FROM ...
WHERE (Col1 = 'a' OR Col1 = 'b')
AND Col2 = 'Peso'
AND (Col3 = 1 OR Col3 = 3 OR Col3 = 4 OR Col3 = 5 OR Col3 = 10)


Peter Larsson
Helsingborg, Sweden
Go to Top of Page
   

- Advertisement -