SQL WHERE excluding records using AND and NOT

sqlsyntax

Here's a query I'm working on:

SELECT  TBL_SUB_KEY AS port
      , poe.[TBL_COMPANY]
      , poe.[TBL_DIVISION_1]
FROM    dbo.TMVKTAB AS poe
WHERE   ( TBL_NUMBER = '8A' )
        AND ( TBL_SUB_KEY <> '' )
        AND ( poe.[TBL_COMPANY] <> '011'
              AND poe.[TBL_DIVISION_1] <> '11'
            )

What I want returned are all the records that are not in Company = '011'/Division_1' = '11'.

I thought combining the company/division in the () would achieve this, but it does not. It eliminates all the company '011' records and it eliminates all division '11' records.

However, when I do this:

SELECT  TBL_SUB_KEY AS port
      , poe.[TBL_COMPANY]
      , poe.[TBL_DIVISION_1]
FROM    dbo.TMVKTAB AS poe
WHERE   ( TBL_NUMBER = '8A' )
        AND ( TBL_SUB_KEY <> '' )
        AND NOT ( poe.[TBL_COMPANY] = '011'
              AND poe.[TBL_DIVISION_1] = '11'
            )

it seems to pull the correct results. Why is this?

Best Solution

It's a boolean logic issue:

NOT (A and B) <=> NOT A  OR NOT B

i.e.:

NOT ( poe.[TBL_COMPANY] = '011' AND poe.[TBL_DIVISION_1] = '11')

Is equivalent to:

( poe.[TBL_COMPANY] <> '011' OR poe.[TBL_DIVISION_1] <> '11')

Read:

http://en.wikipedia.org/wiki/Boolean_logic