In this article I will share how we can assign a column alias and it's in the PostgreSQL.
Let's get started...
Column alias is used to assign a temporary name to a column or an expression in the PostgreSQL. It exists only temporarily during the execution of query.
Syntax of using a Column Alias
SELECT column_name AS alias_name
FROM table_name;
As from the syntax, it can be seen that column_name
is assigned an alias alias_name
. Here AS
key is used to assign the alias name. AS
key is optional to use. We can omit it as well. The syntax will be then as:
SELECT column_name alias_name
FROM table_name;
Example
Suppose we have the customer
table with following attributes in the database:
1. Assigning a column alias to a column:
SELECT
first_name,
last_name AS surname
FROM customer;
The above query assigned the surname
as column alias to the last_name
. If we have not used column alias then the name of the column would have appeared as "last_name".
2. Assigning a column alias to an expression:
SELECT
first_name || ' ' || last_name AS full_name
FROM
customer;
The above query returns full names of all the customers. The expression first_name || ' ' || last_name AS full_name
constructs the full name by concatenating the first name, space and the last name.
Conclusion
In this tutorial we learnt about how we can assign temporary names to the queries using AS
in our query statement.
Top comments (0)