//  home   //  advanced search   //  news   //  categories   //  sql build chart   //  downloads   //  statistics
 ASP FAQ 
Home
ASP FAQ Tutorials

   8000XXXX Errors
   Alerts
   ASP.NET 2.0
   Classic ASP 1.0
   Databases
      Access DB & ADO
      General SQL Server & Access Articles
      MySQL
      Other Articles
      Schema Tutorials
      Sql Server 2000
      Sql Server 2005
   General Concepts
   Search Engine Optimization (SEO)

Contact Us
Site Map

Search

Web
aspfaq.com
tutorials.aspfaq.com
databases.aspfaq.com

ASP FAQ Tutorials :: Databases :: General SQL Server & Access Articles :: Why do I get weird results when using both AND and OR in a query?


Why do I get weird results when using both AND and OR in a query?

Like grade school mathematics and the chicken and the egg, several fundamental concepts involving order of operations also apply to SQL statements. Take the following, for example: 
 
SELECT columns 
    FROM table 
    WHERE 
        a=1 
        AND b=2 
        OR a=2 
 
I get confused reading that query, even after translating it to English: 
 
GIVE ME the rows 
    OUT OF this table 
    WHEN  
        a is 1 
        AND b is 2 
        OR a is 2 
 
So, even with that straightforward English, which of these rows would meet the criteria? 
 
id     a       b 
------ ------- ------- 
 1     1       2 
 2     2       2 
 3     2       1
 
Tricky, isn't it? The SQL engine, like me, has a hard time determining which clause(s) the AND applies to, and which clause(s) the OR applies to. It really depends on what you mean by AND and what you mean by OR. You can separate and group distinct clauses by using parentheses. So, as an example, if you mean: 
 
GIVE ME the rows 
    OUT OF this table 
    WHEN  
        a is 1 *AND* b is 2, 
        *OR* 
        WHEN a is 2 
 
Then your SQL query would be: 
 
SELECT columns 
    FROM table 
    WHERE 
        (a=1 AND b=2) 
        OR a=2 
 
And if you mean: 
 
GIVE ME rows 
    OUT OF this table 
    WHEN  
        a is 1 
        *AND* 
        WHEN b is 2 *OR* a is 2 
 
Then your SQL query would be: 
 
SELECT columns 
    FROM table 
    WHERE 
        a=1 
        AND (b=2 OR a=2) 

Related Articles

Can I fix this mm/dd/yyyy <-> dd/mm/yyyy confusion once and for all?
Could I get some help with JOINs?
How can I tell which version of MDAC I'm running?
How do I access MIN, MAX, SUM, COUNT values from SQL statements?
How do I change column order in a table structure?
How do I change the order of columns in a table?
How do I concatenate strings from a column into a single row?
How do I convert columns of values into a single list?
How do I determine if a database exists?
How do I document / compare my SQL Server database(s)?
How do I get the IDENTITY / AUTONUMBER value for the row I inserted?
How do I solve 'ADO Could Not Find The Specified Provider'?
Should I use BETWEEN in my database queries?
Why can't I use the * wildcard in a database search?
Why do I get 'Syntax Error in INSERT INTO Statement' with Access?
Why do some SQL strings have an 'N' prefix?
Why does AbsolutePosition return as -1?
Why doesn't SQL Server allow me to separate DATE and TIME?
Why is Query Analyzer only returning 255 characters?
Why should I avoid NULLs in my database?
How do I deal with an apostrophe (') in a SQL statement?

 

 


Created: 5/30/2001 | Last Updated: 12/16/2003 | broken links | helpful | not helpful | statistics
© Copyright 2006, UBR, Inc. All Rights Reserved. (73)

 

Copyright 1999-2006, All rights reserved.
Finding content
Finding content.  An error has occured...