Re: doc question about column name alias - Mailing list pgsql-docs

From Tom Lane
Subject Re: doc question about column name alias
Date
Msg-id 15864.993483354@sss.pgh.pa.us
Whole thread Raw
In response to doc question about column name alias  (Weiping He <laser@zhengmai.com.cn>)
Responses Re: doc question about column name alias  (Tom Ivar Helbekkmo <tih@kpnQwest.no>)
List pgsql-docs
Weiping He <laser@zhengmai.com.cn> 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.  The former
is *also* accepted by Postgres --- but the point the text is trying to
make is that "ORDER BY a" is interpreted as a reference to the original
column "a", not to the output expression that happens to also reference
"a".  Perhaps this equivalent situation is a little more clear:

    SELECT a+b AS c FROM table1 ORDER BY a+b;
    SELECT a+b AS c FROM table1 ORDER BY c;

Both of these are accepted by Postgres; only the second one is SQL92;
the first one could cause a+b to be computed twice per row, the second
only once per row.  (I think that in fact the system will notice that
it has duplicate subexpressions here, but that's an optimization.)

Feel free to propose improved wording for the English docs ...

            regards, tom lane

pgsql-docs by date:

Previous
From: Weiping He
Date:
Subject: doc question about column name alias
Next
From: Bruce Momjian
Date:
Subject: Re: doc question about column name alias