pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jonathan Moules <jonathan-lists@lightpear.com>
To: pgsql-sql@postgresql.org
Subject: Not getting the expected results for a simple where not in
Date: Wed, 07 Jun 2017 13:20:13 +0100
Message-ID: <15c827e9305.ae02346553193.1322341002046903374@lightpear.com> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-sql>

Hi List,
I'm a little confused by what seems like it should be a simple query and was hoping someone could explain what's going on.
Using PG 9.4.x

CREATE TABLE aaa.testing_nulls
(
  str character varying(10),
  status character varying(2)
)

Data:
"first";"aa"
"second";"aa"
"third";null
"fourth";"bb"
null;"aa"
null;"bb"

If I run:
    select
        str
    from
        aaa.testing_nulls
        where
            status in ('aa')
        
Against the table, I get the expected result:
"first"
"second"
null

But I want to get the items that don't have a value of 'aa'. Obviously in this case I can simply add "not" to the "where status in" but that's not suitable for my actual use-case (which is where this problem came to light). Instead, I'm nesting the original as a subquery:

select
    *
from
    aaa.testing_nulls
where
    str not in 
    (
        select
            str
        from
            aaa.testing_nulls
            where
                status in ('aa')
    )

Conceptually to me at least, this should work. I expect to get the values:
"third"
"fourth"
But instead when I run it I get 0 results.

It seems to relate to the nulls. If I change the above and add "and str is not null" into the subquery:

select
    *
from
    aaa.testing_nulls
where
    str not in 
    (
        select
            str
        from
            aaa.testing_nulls
            where
                status in ('aa')
                and str is not null
    )

It now gives the expected results.
Why is this?
(I tested this in SQLite too, and get the same behaviour, so I guess it's a generic SQL thing I've never encountered before.)
Thanks,
Jonathan

view thread (3+ messages)  latest in thread

Message-ID: <15c827e9305.ae02346553193.1322341002046903374@lightpear.com>
Permalink:  ../15c827e9305.ae02346553193.1322341002046903374@lightpear.com/
Also on:    postgresql.org/message-id/15c827e9305.ae02346553193.1322341002046903374@lightpear.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: jonathan-lists@lightpear.com
  Subject: Re: Not getting the expected results for a simple where not in
  In-Reply-To: <15c827e9305.ae02346553193.1322341002046903374@lightpear.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox