PostgreSQLAliases

Aliases

An alias gives a column or a table a temporary name for the duration of a query. Aliases make output columns more readable and make joins with long or repeated table names much easier to write.
Column aliases
Use AS to rename a selected column or expression in the result set. The keyword AS is optional — PostgreSQL accepts a bare name right after the expression too.

Renaming a computed column

SQL
SELECT name,
       price,
       price * 0.9 AS discounted_price
FROM products;

AS is optional

SQL
SELECT name,
       price * 0.9 discounted_price   -- same result, AS omitted
FROM products;
Most teams keep AS for clarity even though it is optional — it makes the intent obvious at a glance, especially when scanning a long select list.
Table aliases

Table aliases shorten references to tables, which becomes especially valuable once a query joins several tables together.

Shorter references in a join

SQL
SELECT o.order_id, o.order_date, c.first_name, c.last_name
FROM orders AS o
JOIN customers AS c ON c.customer_id = o.customer_id;
As with column aliases, AS is optional for table aliases too — FROM orders o works exactly the same as FROM orders AS o. Once a table has an alias, you must use that alias (not the original table name) everywhere else in the query.
Quoting aliases with spaces or mixed case

An alias that contains spaces, punctuation, or intentional mixed case must be wrapped in double quotes.

An alias containing a space

SQL
SELECT price * 0.9 AS "Discounted Price"
FROM products;
PostgreSQL folds unquoted identifiers to lowercase
PostgreSQL automatically lowercases unquoted identifiers, including aliases. SELECT 1 AS MyColumn is silently stored and returned as mycolumn, not MyColumn. If you need the case preserved exactly as written, you must double-quote it.
A classic case-sensitivity gotcha
SELECT 1 AS "MyColumn" preserves the mixed case exactly, and afterward you must always refer to it as "MyColumn" (quotes and all) elsewhere in the query. But SELECT 1 AS MyColumn — without quotes — quietly becomes mycolumn, which can cause confusing “column does not exist” errors if your application code or ORM later tries to reference the alias using its original mixed case.
  • Column aliases: SELECT expr AS alias — AS is optional

  • Table aliases: FROM table AS alias — AS is optional, and other clauses must use the alias once it is defined

  • Double-quote an alias to preserve spaces, punctuation, or mixed case

  • Unquoted identifiers are folded to lowercase — a frequent source of confusion