Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

SQL Server QUERY - WHERE CLAUSE - HOW ARE "AND" anD "OR" HANDLED, ...

Posted on 2010-08-17
8
Medium Priority
?
451 Views
Last Modified: 2012-05-10
Hi, everyone

I would like to get a good grasp of how "AND" and "OR" are HANDLED, in a ..WHERE CLAUSE -

I will appreciate someone sharing the basics and then demo using the fragment below - using parenthesis ... please.

Thank you.


WHERE a.city LIKE'los angeles%'
AND a.lp >=100000 AND a.lp <=2000000 AND a.yb >=1990 AND a.yb <=2008
AND a.sf>=500 AND a.sf <=2500 AND a.br>=2 AND a.ba>=1 AND a.pkg_num>=2
AND a.lsz>=4500 AND a.sp>=150000 AND o.option1Value  = 1
AND o.option2Value  = 1 AND o.option3Value  = 1 AND a.property_type IN(0,1)
AND a.status =(5) OR a.status =(30) OR a.status =(10) AND statusDat >= xyz
OR a.status =(20) AND statusDat < m
0
Comment
Question by:dteshome
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 15

Expert Comment

by:gplana
ID: 33457167
AND is supposed to have more priority than OR, however, you should read your RDBMS manual (SQL-server manual, Oracle manual, MySQL manual, or whatever manual from the RDBMS you are using).

I recommend you to use parenthesis ( ) to ensure priority:

WHERE (a>1 AND a<7) OR (a<-1 AND a>-7)

hope it helps.
0
 
LVL 15

Expert Comment

by:gplana
ID: 33457181
also, in your example I would modify

a.status =(5) OR a.status =(30) OR a.status =(10)

for

a.status IN (5,30,10)
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 

Author Comment

by:dteshome
ID: 33457510
Thank you for your comments; in regards to using the multiple ORs instead of ...IN, it is intentional.  I was hopting that someone will go ahead and place the parenthesis where they "belong" per SQL Server ... to show me how it is supposed to work ...

Having said that, is my assumption (below) correct?

WHERE (a.city LIKE'los angeles%' AND a.lp >=100000 AND a.lp <=2000000 AND a.yb >=1990 AND a.yb <=2008
AND a.sf>=500 AND a.sf <=2500 AND a.br>=2 AND a.ba>=1 AND a.pkg_num>=2
AND a.lsz>=4500 AND a.sp>=150000 AND o.option1Value  = 1
AND o.option2Value  = 1 AND o.option3Value  = 1 AND a.property_type IN(0,1)
AND a.status =(5) OR a.status =(30) ) -- does this set of ANDs first
OR  -- does this OR fourth
(a.status =(10) AND statusDat >= xyz) -- does this AND second
OR -- does this OR fifth
(a.status =(20) AND statusDat < m )  -- does this AND third
0
 
LVL 16

Expert Comment

by:vdr1620
ID: 33457642
Your assumption is correct.. sql evaluates statements with AND before OR.. but you might want to check your braces

WHERE (a.city LIKE'los angeles%' AND a.lp >=100000 AND a.lp <=2000000 AND a.yb >=1990 AND a.yb <=2008
AND a.sf>=500 AND a.sf <=2500 AND a.br>=2 AND a.ba>=1 AND a.pkg_num>=2
AND a.lsz>=4500 AND a.sp>=150000 AND o.option1Value  = 1
AND o.option2Value  = 1 AND o.option3Value  = 1 AND a.property_type IN(0,1)
AND ((a.status =(5) OR a.status =(30) ) OR (a.status =(10) AND statusDat >= xyz) OR (a.status =(20) AND statusDat < m ))
0
 
LVL 15

Expert Comment

by:gplana
ID: 33457658
Yes, I think te behaviur is the same on every SQL-server version. The best is to read the manual and make some tests.
 I would put the parenthesis as this
WHERE (a.city LIKE'los angeles%' AND a.lp >=100000 AND a.lp <=2000000 AND a.yb >=1990 AND a.yb <=2008
AND a.sf>=500 AND a.sf <=2500 AND a.br>=2 AND a.ba>=1 AND a.pkg_num>=2
AND a.lsz>=4500 AND a.sp>=150000 AND o.option1Value  = 1
AND o.option2Value  = 1 AND o.option3Value  = 1 AND a.property_type IN(0,1)
AND a.status =(5) )-- does this set of ANDs first

OR a.status =(30)
OR  -- does this OR fourth
(a.status =(10) AND statusDat >= xyz) -- does this AND second
OR -- does this OR fifth
(a.status =(20) AND statusDat < m )  -- does this AND third
 
I do recommend to enforce order usng parenthesis and avoid any supposition.
0
 
LVL 32

Accepted Solution

by:
Brendt Hess earned 500 total points
ID: 33457678
Not quite.  Consider your first block of ANDs, which includes one OR statement.  The conditional check there will be true in either of these two cases:

Case 1:
a.city LIKE'los angeles%'
    AND a.lp >=100000
    AND a.lp <=2000000
    AND a.yb >=1990
    AND a.yb <=2008
    AND a.sf>=500
    AND a.sf <=2500
    AND a.br>=2
    AND a.ba>=1
    AND a.pkg_num>=2
    AND a.lsz>=4500
    AND a.sp>=150000
    AND o.option1Value  = 1
    AND o.option2Value  = 1
    AND o.option3Value  = 1
    AND a.property_type IN(0,1)
    AND a.status =(5)

Case 2:
 a.status =(30)

So, as you can see from the above, an OR statement treats each side of the OR statement independently.  This is different from an AND statement, which chains all of the ANDed conditions together.  This is how the boolean AND/OR logic works ... for AND to be true, both sides must be true.  For OR to be true, at least one side must be true.


 To modify the first group of conditions to do what I believe you want it to do, you would use this version:

a.city LIKE'los angeles%'
    AND a.lp >=100000
    AND a.lp <=2000000
    AND a.yb >=1990
    AND a.yb <=2008
    AND a.sf>=500
    AND a.sf <=2500
    AND a.br>=2
    AND a.ba>=1
    AND a.pkg_num>=2
    AND a.lsz>=4500
    AND a.sp>=150000
    AND o.option1Value  = 1
    AND o.option2Value  = 1
    AND o.option3Value  = 1
    AND a.property_type IN(0,1)
    AND (
        a.status =(5)
        OR a.status =(30)
    )

The other OR conditions appear to be correctly handled.
0
 

Author Closing Comment

by:dteshome
ID: 33459667
Thank UUUUUUU!
0

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question