TExcelTAD
Home › PostgreSQL › PostgreSQL AS (Aliases)

PostgreSQL AS (Aliases)

An alias is a temporary name. Column aliases rename the columns of a result; table aliases give tables a short nickname inside one query. Neither changes anything in the database.

Column aliases

Computed columns get unhelpful names such as ?column?. AS fixes that:

pgsql · try it yourself
SELECT title, price * stock FROM books WHERE id = 1;

SELECT title, price * stock AS stock_value FROM books WHERE id = 1;
     title     | ?column?
---------------+----------
 The Salt Road |   179.88
(1 row)

     title     | stock_value
---------------+-------------
 The Salt Road |      179.88
(1 row)

The keyword AS is optional for column aliases (price * stock stock_value works), but writing it makes queries easier to read.

Aliases with spaces or capitals

Alias names follow the identifier rules: unquoted names are folded to lower case. Use double quotes for spaces, capitals or reserved words:

pgsql · try it yourself
SELECT first_name || ' ' || last_name AS "Customer Name",
       joined AS Joined,
       city AS "from"
FROM customers
WHERE id <= 3;
 Customer Name |   joined   |  from
---------------+------------+--------
 Anna Kowalski | 2023-01-15 | Warsaw
 Ben Carter    | 2023-02-03 | Leeds
 Chloé Dubois  | 2023-03-22 | Lyon
(3 rows)

Joined became joined; "Customer Name" and "from" were kept exactly.

Table aliases

In queries with several tables you have to say which table a column belongs to. Short aliases keep that readable. These two queries are identical:

pgsql · try it yourself
SELECT books.title, authors.name
FROM books
JOIN authors ON authors.id = books.author_id
WHERE books.category_id = 3;

SELECT b.title, a.name AS author
FROM books AS b
JOIN authors AS a ON a.id = b.author_id
WHERE b.category_id = 3;
       title        |     name
--------------------+--------------
 Murder at Low Tide | Daniel Reyes
 The Quiet Witness  | Daniel Reyes
 The Map Thief      | Daniel Reyes
(3 rows)

       title        |    author
--------------------+--------------
 Murder at Low Tide | Daniel Reyes
 The Quiet Witness  | Daniel Reyes
 The Map Thief      | Daniel Reyes
(3 rows)

Once a table has an alias, you must use the alias; the full name is no longer visible in that query:

pgsql · try it yourself
SELECT books.title FROM books AS b;
ERROR:  invalid reference to FROM-clause entry for table "books"
LINE 1: SELECT books.title FROM books AS b;
               ^
HINT:  Perhaps you meant to reference the table alias "b".

Where can you use a column alias?

PostgreSQL evaluates WHERE before the select list, so a column alias does not exist yet in WHERE. ORDER BY runs after it, so the alias works there:

pgsql · try it yourself
SELECT title, price * stock AS stock_value
FROM books
WHERE stock_value > 300;

SELECT title, price * stock AS stock_value
FROM books
WHERE price * stock > 300
ORDER BY stock_value DESC;
ERROR:  column "stock_value" does not exist
LINE 3: WHERE stock_value > 300;
              ^
           title           | stock_value
---------------------------+-------------
 Algorithms on a Napkin    |      385.00
 Learning SQL the Calm Way |      359.10
 Clockwork Garden          |      315.00
(3 rows)
Clause Column alias allowed?
WHERE no, repeat the expression
GROUP BY yes (PostgreSQL extension)
HAVING no
ORDER BY yes

Aliases for subqueries

A subquery in FROM must have an alias, and you can rename its columns in the same place:

pgsql · try it yourself
SELECT t.cat, t.n
FROM (SELECT category_id, count(*) FROM books GROUP BY category_id) AS t (cat, n)
WHERE t.n >= 4
ORDER BY t.cat;
 cat | n
-----+---
   1 | 5
   4 | 4
(2 rows)

Test yourself

What column name does SELECT price AS Price produce?

  1. Price
  2. price
  3. PRICE
  4. "Price"

Why does WHERE stock_value > 300 fail when stock_value is a select-list alias?

  1. Aliases are read-only
  2. WHERE is evaluated before the select list
  3. Aliases only work on tables
  4. 300 must be quoted

After FROM books AS b, how should you refer to the title column?

  1. books.title
  2. b.title
  3. AS.title
  4. Either works