postgresql_16:3.4.3.2 Column Labels
PostgreSQL 16 > The SQL Language > Queries > Select Lists > Column Labels
The entries in the select list can be assigned names for subsequent processing, such as for use in an ORDER BY clause or for display by the client application. For example: SELECT a AS value, b + c AS sum FROM ...
If no output column name is specified using AS, the system assigns a default column name. For simple column references, this is the name of the referenced column. For function calls, this is the name of the function. For complex expressions, the system will generate a generic name.
The AS key word is usually optional, but in some cases where the desired column name matches a PostgreSQL key word, you must write AS or double-quote the column name in order to avoid ambiguity. ((see postgresql_16:9.3) shows which key words require AS to be used as a column label.) For example, FROM is one such key word, so this does not work: SELECT a from, b + c AS sum FROM ... but either of these do: SELECT a AS from, b + c AS sum FROM ... SELECT a "from", b + c AS sum FROM ... For greatest safety against possible future key word additions, it is recommended that you always either write AS or double-quote the output column name.
Note
The naming of output columns here is different from that done in the FROM clause (see (see postgresql_16:3.4.2.1)). It is possible to rename the same column twice, but the name assigned in the select list is the one that will be passed on.