Re: Missing numbers - Mailing list pgsql-general

From Tom Lane
Subject Re: Missing numbers
Date
Msg-id 14247.1117639406@sss.pgh.pa.us
Whole thread Raw
In response to Re: Missing numbers  (Simon Riggs <simon@2ndquadrant.com>)
List pgsql-general
Simon Riggs <simon@2ndquadrant.com> writes:
> On Wed, 2005-06-01 at 00:27 -0400, Alvaro Herrera wrote:
>>> SELECT g.num
>>> FROM generate_series ((SELECT min(doc_numero) FROM bdocs),
>>> (SELECT max(doc_numero) FROM bdocs)) AS g(num)
>>> LEFT JOIN bdocs ON bdocs.doc_numero = g.num
>>> WHERE bdocs.doc_numero IS NULL

> I wonder what the SQL spec should happen in this case? It depends upon
> whether the NOT NULLs are excluded before or after the join takes
> place.

The spec says that WHERE is logically applied after the join.  In some
cases it is possible to push the condition down to occur before the join
without changing the results ... but not in the above case.

> Either way, I still prefer my phrasing of the SQL, which seems clearer,
> but I would say that wouldn't I?

Maybe so.  The outer-join-and-test-for-null is a pretty common idiom
though, so I'd expect experienced SQL programmers to recognize it on
sight.

            regards, tom lane

pgsql-general by date:

Previous
From: "Jim Buttafuoco"
Date:
Subject: Re: SQL question.
Next
From: "Gerald D. Anderson"
Date:
Subject: Old problem needs solution