1

This query:

select dept, job
from organizations
where (dept = 'Marketing' and job <> 'Sam Wise')
or    (dept = 'Marketing' and job <> 'The Precious')

Keeps giving records that have jobs showing 'The Precious' when it shouldn't. But if I write the query this way:

select dept, job
from organizations
where (dept = 'Marketing' and job <> 'Sam Wise')
and   (dept = 'Marketing' and job <> 'The Precious')

Then I get the proper records. No jobs as 'Sam Wise' or 'The Precious'. So I'm wondering if my understanding of AND and OR in T-SQL is confused or is something wonky with my sql server.

In short, why isn't the first query removing all records that have 'Sam Wise' or 'The Precious' as a job, but the second query is?

2
  • 3
    Do you understand how the boolean OR operator works?
    – Oded
    Feb 7, 2012 at 21:11
  • 4
    I think you need (dept = 'Marketing) and (job <> 'Sam Wise') and (job <> 'The Precious') Feb 7, 2012 at 21:14

7 Answers 7

14

That's because if either one of your parenthetical OR clauses is found to be true, then the row is included. Your two conditions ORed together logically mean the union of all rows where job is either NOT 'Sam Wise' or NOT 'The Precious'. And all rows meet those criteria, since for example 'Sam Wise' is not 'The Precious' and so meets the second condition.

Here's what I think you were trying for:

select dept, job
from organizations
where
   dept = 'Marketing'
   and not (
      job = 'Sam Wise'
      or job = 'The Precious'
   )

And note you can simply use ANDs:

select dept, job
from organizations
where
   dept = 'Marketing'
   and job <> 'Sam Wise'
   and job <> 'The Precious'

This is a good example of a logic law. To denote the same condition (A = X OR B = Y) but express it negatively (you want a NOT in front) it always transforms to NOT (A <> X AND B <> Y). It works the same with (A = X AND B = Y) => NOT (A <> X OR B <> Y). In your case, A and B are the same, so your query conditions on job are equivalent to NOT (A = X AND A = Y), which some thought will show you will always return true since A = X AND A = Y can never be true.

Since IN() can be used to express many OR conditions together, you can simplify the query this way: (though not the work the engine has to do, that is identical).

select dept, job
from organizations
where
   dept = 'Marketing'
   and job not in ('Sam Wise', 'The Precious')

I realized here a bit later that the translation of A = X => A <> X is simply negation, in the special query sense of the word. In its simplest form, the logic law goes like this, given two propositions P1 and P2:

  • NOT (P1 AND P2) => (NOT P1 OR NOT P2)
  • NOT (P1 OR P2) => (NOT P1 AND NOT P2)

Note that these hold true, too:

  • (P1 AND P2) => NOT (NOT P1 OR NOT P2)
  • (P1 OR P2) => NOT (NOT P1 AND NOT P2)

These are transformations that can be really helpful as well.

3
  • 1
    Flashbacks to Digital Logic 101 and De Morgan's laws. Stop the madness!
    – HABO
    Feb 7, 2012 at 22:30
  • 1
    @user92546 Thank you for the name! I just knew the law intuitively from experience.
    – ErikE
    Feb 8, 2012 at 4:56
  • 1
    +1 Worth noting that De Morgan's laws do hold in SQL despite its three-valued logic.
    – onedaywhen
    Feb 8, 2012 at 8:29
9

Those two clauses are considered independently. Maybe try:

WHERE dept = 'Marketing' AND job NOT IN ('Sam Wise', 'The Precious');
0
4

This is simply how OR works, it will return true if at least one part is true. Lets see how first query will work for row where Job == 'Sam Wise'.

(dept = 'Marketing' and 'Sam Wise' <> 'Sam Wise') -- FALSE
OR    
(dept = 'Marketing' and 'Sam Wise' <> 'The Precious') -- TRUE

We got FALSE OR TRUE so entire WHERE returns TRUE. AND returns TRUE if both parts are TRUE this is why second query returns expected results.

3

The first group of rules is matching "The Precious" and the 2nd group of rules is matching "Sam Wise"

In other words, dept = 'Marketing' AND job <> 'Sam Wise' is true for rows where dept = 'Marketing' and job = 'The Precious', so that row is selected. Likewise, dept = 'Marketing' AND job <> 'The Precious' is true for rows where dept = 'Marketing' and job = 'Sam Wise', so that row is also selected.

A better query would be as such:

SELECT dept, job
FROM organizations
WHERE
   dept = 'Marketing'
   and job <> 'Sam Wise'
   and job <> 'The Precious'
3

Let's say you've got a record that's a Sam Wise job for the Marketing department.

It evaluates thusly:

(dept = 'Marketing' and job <> 'Sam Wise') 
OR (dept = 'Marketing' and job <> 'The Precious')

(TRUE and FALSE) 
OR (TRUE and TRUE)

(FALSE)
OR (TRUE)

(TRUE)

So that document is included. Same thing if it's The Precious. Your first query is essentially just ensuring that it's for the marketing department.

2

The part of the select statement:

where (dept = 'Marketing' and job <> 'Sam Wise')
or    (dept = 'Marketing' and job <> 'The Precious')

means that it will return queries if one or the other is true, or both. That is why you are getting unwanted results.

statement1 = true OR statemet2 = true ...result true
statement1 = false OR statemet2 = true ...result true
statement1 = true OR statemet2 = false...result true
statement1 = false OR statemet2 = false ...result false

Using AND however will return true only when both statements are true:

statement1 = true AND statemet2 = true ...result true
statement1 = false AND statemet2 = true ...result false
statement1 = true AND statemet2 = false...result false
statement1 = false AND statemet2 = false ...result false
1

This concept applies to math and logic and is not strictly a coding thing. Its just the way AND and OR work in logical terms.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.