pg.ddx.io  pgsql-docs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Ivar Helbekkmo <tih@kpnQwest.no>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Weiping He <laser@zhengmai.com.cn>
Cc: pgsql-docs@postgresql.org
Subject: Re: doc question about column name alias
Date: 26 Jun 2001 17:25:14 +0200
Message-ID: <867kxzjmat.fsf@athene.i.eunet.no> (raw)
In-Reply-To: <15864.993483354@sss.pgh.pa.us>
References: <3B374CF6.C1AB82F0@zhengmai.com.cn>
	<15864.993483354@sss.pgh.pa.us>

Tom Lane <tgl@sss.pgh.pa.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.



view thread (5+ messages)  latest in thread

Message-ID: <867kxzjmat.fsf@athene.i.eunet.no>
Permalink:  ../867kxzjmat.fsf@athene.i.eunet.no/
Also on:    postgresql.org/message-id/867kxzjmat.fsf@athene.i.eunet.no

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-docs@postgresql.org
  Cc: tih@kpnQwest.no, tgl@sss.pgh.pa.us, laser@zhengmai.com.cn
  Subject: Re: doc question about column name alias
  In-Reply-To: <867kxzjmat.fsf@athene.i.eunet.no>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox