DEV Community

Malik M
Malik M

Posted on

Column Alias in PostgreSQL

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;
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

Example
Suppose we have the customer table with following attributes in the database:

Image description

1. Assigning a column alias to a column:

SELECT 
   first_name, 
   last_name AS surname
FROM customer;
Enter fullscreen mode Exit fullscreen mode

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".

OUTPUT:
Image description

2. Assigning a column alias to an expression:

SELECT 
   first_name || ' ' || last_name AS full_name 
FROM 
   customer;
Enter fullscreen mode Exit fullscreen mode

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.

OUTPUT:
Image description

Conclusion
In this tutorial we learnt about how we can assign temporary names to the queries using AS in our query statement.

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay