Skip site navigation (1) Skip section navigation (2)

Re: doc question about column name alias

From: He Weiping <laser(at)zhengmai(dot)com(dot)cn>
To: Tom Ivar Helbekkmo <tih(at)kpnQwest(dot)no>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-docs(at)postgresql(dot)org
Subject: Re: doc question about column name alias
Date: 2001-06-27 03:28:28
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-docs
Tom Ivar Helbekkmo wrote:

> 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.

Thanks all for these info. & examples, they help me much to clearify
the question, the present wording is a little bit hard to understand, but
accurate and
efficent, and I think it's better to add a few lines to make it clear, like

"...but the SQL standard require (or "only permit") us to do:
select a as b from table1 order by b;"

    regards    laser

In response to

pgsql-docs by date

Next:From: LinuxeroDate: 2001-06-27 07:36:33
Subject: hello
Previous:From: Tom Ivar HelbekkmoDate: 2001-06-26 15:25:14
Subject: Re: doc question about column name alias

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group