I'm reading through the Postgres documentation, the page on SELECT statements, and I ran across an aspect of aliases that I have never encountered.
In the section on FROM clauses, subheading alias, there is a sentence stating:
If an alias is written, a column alias list can also be written to provide substitute names for one or more columns of the table.
There are no examples given in the documentation that I could find.
I know how to set up output names as aliases, but that doesn't appear to be the same thing.
The synopsis for SELECT includes the lines:
... SELECT [ ALL | DISTINCT [ ON (
expression
[, ...] ) ] ] * |
expression
[ [ AS ]
output_name
] [, ...] [ FROM
from_item
[, ...] ] ...
And defines from_item
as:
where from_item can be one of:
[ ONLY ]
table_name
[ * ] [ [ AS ]
alias
[ (
column_alias
[, ...] ) ] ] (
select
) [ AS ]
alias
[ (
column_alias
[, ...] ) ] ...(other forms omitted)...
Note that from_item
actually includes column_alias
.
It makes sense to me that the ( select )
form can be given column aliases in the FROM clause (rather than only output_names, in the "expressions" of the SELECT statement), since the "columns" of the subquery will have been explicitly chosen in most cases and thus the sequence will be known. So I would imagine in that case the column_alias values could simply be a list of names, and they would be matched up in sequence against the columns returned by the subquery. (Though an example would be nice.)
However, how can column aliases be used for a table_name? Do you have to know the exact sequence of columns defined in the table, or can you set an alias just for one or two of these in the FROM clause?
What if you only want to set a column_alias
for one column with a very long name (and leave the other columns not aliased); is this possible? (If so, is this Postgres specific?)