postgresql
  1. postgresql-column-alias

Column Alias - (PostgreSQL Table)

Columns are the fundamental components of tables in PostgreSQL databases. Sometimes, the names of columns are too long or have characters that are difficult to work with. That's where column aliases come in. In this tutorial, we'll show you how to use column aliases in PostgreSQL tables.

Syntax

SELECT column_name AS alias_name
FROM table_name
  • column_name: The column name that you want to rename.
  • alias_name: The new name that you want to assign to the column.

Example

Let's take a look at a basic example of a column alias in PostgreSQL.

SELECT first_name AS fname, last_name AS lname FROM employees;

This query selects the columns first_name and last_name from the employees table and renames them to fname and lname, respectively.

Output

fname    |   lname
---------+---------
John     |   Smith
Jane     |   Doe
Joe      |   Johnson

Explanation

In the above example, we used the SELECT statement to select the first_name and last_name columns from the employees table, and gave them the aliases fname and lname, respectively. This way, the output of the query displays the column names with their alias.

Use

Column aliases can be used to create more readable and understandable queries. They are especially useful when working with tables and column names that are lengthy or have special characters.

Important Points

  • When using column aliases, make sure to use a space character between the alias name and the AS keyword.
  • Column aliases can also be used in GROUP BY, ORDER BY, and HAVING clauses.
  • Column aliases are not case sensitive.

Summary

In this tutorial, we discussed column aliases in PostgreSQL tables. We showed you the syntax, example, output, explanation, use, and important points of using column aliases in PostgreSQL. With this knowledge, you can rename columns in your PostgreSQL tables and create more readable and understandable queries.

Published on: