In PostgreSQL, a column alias is a temporary name given to a column in a query result set. It allows for the renaming of columns to make the output more readable or for disambiguation in cases where multiple tables are joined and has columns with the same name.

One of the most common use cases for column aliases is in SELECT statements, where the column names returned by the query may not be the most descriptive or user-friendly. By using column aliases, the output can be made more readable and understandable for the end user.

How to Use Column Aliases in PostgreSQL

Using column aliases in PostgreSQL is quite simple. The basic syntax is as follows:

				
					SELECT column_name AS alias_name
FROM table_name;

				
			

For example, if we had a table called employees with a column called last_name and wanted to display the results with the column header Surname, we would use the following query:

				
					SELECT last_name AS "Surname"
FROM employees;

				
			

It is important to note that column aliases are only valid for the duration of the query and cannot be used in other parts of the SQL statement or in any subsequent queries.

				
					SELECT first_name, last_name
FROM employees;

				
			

Creating derived columns

Column aliases can also be used to create new columns that are derived from existing ones. This can be useful for performing calculations or for concatenating multiple columns into a single value.

				
					SELECT last_name || ', ' || first_name AS "Full Name"
FROM employees;

				
			

1. Assigning Column Aliases to Columns in PostgreSQL

In PostgreSQL, column aliases can temporarily rename columns in a query result set for making the output more readable or for disambiguation in cases where multiple tables are joined and have columns with the same name.

One of the most common use cases for column aliases is in SELECT statements, where the column names returned by the query may not be the most descriptive or user-friendly. By using column aliases, the output can be made more readable and understandable for the end user.

Example of Assigning a Column Alias

To assign a column alias to a column in PostgreSQL, the AS keyword is followed by the desired alias name.
For example, if we have a table called employees with a column called employee_id and we want to display the results with the column header ID, we would use the following query:

				
					SELECT employee_id AS "ID"
FROM employees;

				
			

This query will display the results of the employee_id column with the column header “ID”. This can make the output more readable and understandable for the end user.

Assigning column aliases to multiple columns in a single query is also possible.
For example, if we want to display the employee_id and employee_name columns with the column headers ID and Name, respectively, we would use the following query:

				
					SELECT employee_id AS "ID", employee_name AS "Name"
FROM employees;

				
			

2. Assigning a column alias to an expression example

In PostgreSQL, assigning column aliases to expressions is possible rather than just individual columns. This can be useful when performing calculations or manipulating data in a query, and the resulting column name may need to be more descriptive and user-friendly.

Example of Assigning a Column Alias to an Expression

For example, if we have a table called sales with columns product_id, quantity, and “price” and we want to display the results with a column for the total cost of each sale, we would use the following query:

				
					SELECT product_id, quantity, price, (quantity * price) AS "Total Cost"
FROM sales;

				
			

This query will display the results of the product_id, quantity, price columns and a new column with the calculation (quantity * price) and the column header Total Cost. This can make the output more readable and understandable for the end user. It’s also possible to assign column aliases to multiple expressions in a query.

For example, if we want to display the product_id, quantity, price columns and columns with the calculation (quantity * price) and the margin (quantity * price * 0.1) and the column headers “Total Cost” and “Margin” respectively, we would use the following query:

				
					SELECT product_id, quantity, price, (quantity * price) AS "Total Cost", (quantity * price * 0.1) AS "Margin"
FROM sales;

				
			

3. Column aliases that contain spaces

In PostgreSQL, it’s possible to assign column aliases that contain spaces, but there are a few things to keep in mind when doing so.

Using Quotes for Column Aliases with Spaces

When assigning a column alias that contains spaces, you need to surround the alias with quotes. This can be either single quotes (‘) or double quotes (“). 

For example, the following query assigns the column alias “Total Cost” to the expression (quantity * price) in the sales table:

				
					SELECT product_id, quantity, price, (quantity * price) AS "Total Cost"
FROM sales;

				
			

Mixing Single and Double Quotes

You can also use single quotes for the column alias and double quotes for the table and column names or vice versa. This allows you to include single or double quotes within the column alias.

For example, the following query assigns the column alias ‘Total Cost’ to the expression (quantity * price) in the sales table:

				
					SELECT product_id, quantity, price, (quantity * price) AS 'Total Cost'
FROM "sales";

				
			

Column Aliases and Case Sensitivity

It’s important to remember that the case sensitivity depends on the column’s collation. If a column is case-insensitive, the column alias will be case-insensitive. If a column is case-sensitive, the column alias will be case-sensitive.

Wrap up

Column aliases in PostgreSQL are a powerful tool that allows you to assign a custom name to a column in a query. This can greatly improve the readability of query output and make it easier to understand. Additionally, column aliases can assign a name to an expression, making the output more meaningful.

It’s important to keep in mind that quotes are required for column aliases containing spaces and that case sensitivity depends on the column’s collation. By understanding and effectively utilizing column aliases in your queries, you can greatly enhance the functionality and usability of your PostgreSQL database.


Thanks for reading. Happy coding!