Alias in PostgreSQL With Examples

PostgreSQL offers the concept of “ALIASES”, which provides ease to users while working with complex queries. ALIASES are used in Postgres to provide a temporary name to the columns, tables, etc., while executing/writing Postgres queries. ALIASES increase the readability of a table, column, etc.

This write-up will teach you how to use PostgreSQL's table and column aliases with practical examples. So let’s begin!

How Do I Use ALIAS for Long Table Names?

You can use the alias for a table that has a long name, such as “this_is_example_table”. Adding aliases to tables will improve query readability and user experience. To avail the functionality of alias, the “AS” keyword is used in PostgreSQL:

SELECT column_list
FROM tab_name AS alias_name;

The above syntax shows that an “AS” keyword is used between the table and alias names. However, it is optional you can skip it as follows:

SELECT column_list
FROM tab_name alias_name;

The above query will work the same way as the previous one.

Example: How Does the Table Alias Work in Postgres?

We have created a table named “article_details”, whose data is shown in the below snippet:

SELECT * FROM article_details;
img

The above snippet shows the data of the “article_details” table. Suppose we want to fetch those articles whose id is greater than 6. To make the query easily readable, we can use the table aliases as follows:

SELECT ad.article_id, ad.article_title, ad.published_date
FROM article_details AS ad
WHERE ad.article_id >= 6;
img

This is how the table ALIAS works in Postgres.

How Do I Use ALIAS for Table Columns?

The below-mentioned syntax must be followed to use the column alias in Postgres:

SELECT col_name AS alias_name
FROM tbl_name;

The “AS” keyword is used in the above syntax for the column alias. It is optional and can be skipped, as shown in the below syntax:

SELECT col_name alias_name
FROM tbl_name;

The column will be assigned a new temporary name that can be used throughout the given query.

Example 1: How Does the Column Alias Work in Postgres?

In this example, we will explain the working of the column alias using the article_info table, whose data is shown in the below snippet:

SELECT * FROM article_info;
img

In the below snippet, we will utilize the column alias for the article_title column:

SELECT article_title AS title
FROM article_info;
img

This is how the column alias works in Postgres.

Example 2: How to Use Column Alias With Built-in Functions?

We have created a table named “addition_example”, whose data is shown in the following snippet:

SELECT * FROM addition_example;
img

Let’s perform addition on the given column using the built-in SUM() function. We will use the column alias to assign a temporary name to the resultant column:

SELECT SUM(val) AS total
FROM addition_example;
img

This way, you can use the column alias on built-in functions in Postgres.

Conclusion

ALIASES are used in Postgres to provide a temporary name to the columns, tables, etc., while executing/writing Postgres queries. The aliases in Postgres improve query readability and enhance the user experience. To avail the functionality of alias, the “AS” keyword is used in PostgreSQL. PostgreSQL's table and column ALIASES were explained using practical examples in this write-up.