Solved

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

Posted on 2010-08-17
8
408 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

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.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

821 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