Solved

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

Posted on 2010-08-17
8
423 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 16

Expert Comment

by:vdr1620
ID: 33457129
0
 
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
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 

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:
bhess1 earned 125 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

733 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