Re: Column alias in where clause? - Mailing list pgsql-general

From Tom Lane
Subject Re: Column alias in where clause?
Date
Msg-id 20619.1218676915@sss.pgh.pa.us
Whole thread Raw
In response to Re: Column alias in where clause?  (Martijn van Oosterhout <kleptog@svana.org>)
List pgsql-general
Martijn van Oosterhout <kleptog@svana.org> writes:
> You can't. Conceptually, the result of the SELECT is not visible until
> *after* the WHERE clause has executed, so having the where clause
> depend on the select won't work.

It's not only conceptual.  Consider

    SELECT 1/x FROM mytab WHERE x <> 0;

You'd be unhappy if this query failed with a divide-by-zero error
(and it would be a violation of the SQL spec, too).  So per spec,
the SELECT list must not be evaluated until after WHERE completes,
and that's why it's not reasonable for WHERE to refer to the
SELECT list.

(Now, we've kinda broken this rule by allowing GROUP BY to refer
to SELECT items, but that's a matter for another discussion.)

            regards, tom lane

pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: automatic REINDEX-ing
Next
From: Tom Lane
Date:
Subject: Re: In-place conversion of type bool