PostgreSQL Column AliasIn 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 AliasThe syntaxes of the PostgreSQL Column alias are given below: Syntax1 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. Syntax3 The below syntax is used to display how we can set an alias for expression within the SELECT condition. In the above syntaxes, we have the following parameters:
Note:
Examples of PostgreSQL Column aliasingLet 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.
To return the emp_fname and emp_lname of all employees from the employee table, we will use the following command: 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: 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: Output After implementing the above command, we will get a similar output as above: Example of Assigning a column alias to an expressionIn 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: 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 Output After executing the above command, we will get the following output:
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": 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:
Next TopicPostgreSQL Sequence |