[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2010-08-17
8
Medium Priority
?
444 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 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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

649 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