Solved

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

Posted on 2010-08-17
8
383 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 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
 

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

In this article I will describe the Backup & Restore 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.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now