In this section, we are going to understand the working of PostgreSQL Column Aliasing, which is used to give a temporary name to the column in the particular command.
What is PostgreSQL Column Alias?
In PostgreSQL, a Column alias is used to give a short name to the column in the select list of a SELECT command in a particular statement. The column aliasing works as a nickname for the column name, making the column name more readable and shorter.
It exists momentarily throughout the implementation of the command. The column aliasing is very useful when the column name is not user-friendly in real-time.
Syntax of PostgreSQL Column Alias
The syntaxes of the PostgreSQL Column alias are given below:
Syntax1
SELECT column_name AS alias_name
FROM table_name
Conditions...;
Syntax2
In the below syntax, we ignore the AS keyword because it is optional, and the column_name is given to an alias alias_name.
SELECT column_name alias_name
FROM table_name;
Syntax3
The below syntax is used to display how we can set an alias for expression within the SELECT condition.
SELECT expression AS alias_name
FROM table_name;
In the above syntaxes, we have the following parameters:
Parameter | Description |
---|---|
column_name | The column name is used to define the column’s original name, where we want to perform alias. |
alias_name | The alias name is used to describe the temporary name, which is given to the column. |
table_name | The table name parameter is used to define the name of the particular table. |
AS | The AS is an optional keyword used by most developers while aliasing a column name, but not used when performing the table aliasing. |
Note:
- The main objective of column aliases is to make the title of the output of command more expressive.
- The alias_name parameter is only valid within the possibility of the SQL commands.
- It is acceptable to use spaces when we are aliasing a column name.
- We must enclose the alias_name in quotes (‘ ‘) if the alias_name contains spaces.
Examples of PostgreSQL Column aliasing
Let see some examples for our better understanding of PostgreSQL Column aliasing.
We’ll use the employee table from the Javatpoint database to display how to work with column aliases.
- Example of Assigning a column alias to a column
To return the emp_fname and emp_lname of all employees from the employee table, we will use the following command:
SELECT emp_fname, emp_lname
FROM employee;
Output
After executing the above command, we will get the following output:

if we want to rename the emp_lname header name then we can give it a new name with the help of column alias as we can see in the below command:
SELECT emp_fname,
emp_lname AS Surname
FROM employee;
As we can observe in the above command, the emp_lname column name has been renamed by Surname with the help of AS keyword:
Output
On executing the above statement, we will get the following result:

Or we can remove the AS keyword and make it more readable as we can observe in the below statement:
SELECT emp_fname,
emp_lname Surname
FROM employee;
Output
After implementing the above command, we will get a similar output as above:

Example of Assigning a column alias to an expression
In the below example, we will use to assign a column alias to an expression.
To fetch the full names of all employees, we will use the concatenation operator to concatenate the first name, space, and the last name of the employee as we can see in the below command:
SELECT
emp_fname || ' ' || emp_lname
FROM employee;
Note: In PostgreSQL, we can also use the (||) operator as the concatenation operator, which helps us to combine multiple strings into a single string.
Output
On executing the above command, we will get the following output:

As we can see in the above screenshot, the header of the column is not appropriately understood?column?.
To solve this, we can provide the expression as emp_fname || ‘ ‘ || emp_lname a column alias. For example, Full_Name
SELECT
emp_fname || ' ' || emp_lname AS Full_Name
FROM employee;
Output
After executing the above command, we will get the following output:

- Example of Column aliases having spaces
If a column alias having one or more spaces, we will need to use it with double quotes (” “) such as column_name AS “column alias”
In the below example, we will use the Full_Name as “Full Name”:
SELECT
emp_fname || ' ' || emp_lname "Full Name"
FROM employee;
Output
On executing the above command, we will get the below command:

Overview
In the PostgreSQL column Alias section, we have learned the following topics:
- We will use to provide a column or an expression of a column alias with the help of the syntax column_name AS alias_name or expression AS alias_name.
- The AS keyword is optional.
- We will use double quotes (” “) for a column alias, which has spaces.
Leave a Reply