Re: doc question about column name alias

From: Tom Ivar Helbekkmo <tih(at)kpnQwest(dot)no>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Weiping He <laser(at)zhengmai(dot)com(dot)cn>, pgsql-docs(at)postgresql(dot)org
Subject: Re: doc question about column name alias
Date: 2001-06-26 15:25:14
Message-ID: 867kxzjmat.fsf@athene.i.eunet.no
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-docs

Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> writes:

>> SELECT a AS b FROM table1 ORDER BY a;
>> I think it should be:
>> SELECT a AS b FROM table1 ORDER BY b;
>
> You are correct that the latter is the SQL-standard version.

In fact, this becomes pretty obvious when you consider the theoretical
ordering of the evaluation of the various parts of the statement. The
ORDER BY is executed *after* the SELECT part, so it only knows about
the column name 'b', and doesn't know what 'a' refers to. Actually,
ORDER BY isn't even part of the SELECT statement, really, but hides an
implicit CURSOR operation taking place during data output. Allowing
the first version to work is of questionable value, since it might
cause errors and confusion when code is modified. Consider the case
where you have "SELECT a AS b, b AS a ...". I've done it! :-)

The order of evaluation is: FROM, WHERE, GROUP BY, HAVING, SELECT,
and, finally, ORDER BY.

-tih
--
The basic difference is this: hackers build things, crackers break them.

In response to

Responses

Browse pgsql-docs by date

  From Date Subject
Next Message He Weiping 2001-06-27 03:28:28 Re: doc question about column name alias
Previous Message Tom Lane 2001-06-25 15:35:54 Re: doc question about column name alias