8.4. Working with Aliases and Self-Joins

Aliases are supported by almost every sophisticated database. Aliases are not only comfortable for writing shorter queries; they can also be used to build more complex queries where one table is treated as a set of tables.

8.4.1. Using Aliases

Before you learn about self-joins, it is time to have a closer look at aliases. An alias is an alternative name for an object.

Take a look at an example:

phpbook=# SELECT a.* FROM stock AS a LIMIT 1;
   name   |    pday    | price
----------+------------+-------
 Cybertec | 2002-06-12 | 23.40
(1 row)

a is an alias for stock and can be used just like the original name. To define an alias, the keyword AS and the name of the alias must be added to the FROM clause. In this example ...

Get PHP and PostgreSQL: Advanced Web Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.