Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

PostgreSQL AS


Aliases

SQL aliases are used to give a table, or a column in a table, a temporary name.

Aliases are often used to make column names more readable.

An alias only exists for the duration of that query.

An alias is created with the AS keyword.

Example

Using aliases for columns:

SELECT customer_id AS id
FROM customers;
Run Example »

AS is Optional

Actually, you can skip the AS keyword and get the same result:

Example

Same result without AS:

SELECT customer_id id
FROM customers;
Run Example »

Concatenate Columns

The AS keyword is often used when two or more fields are concatenated into one.

To concatenate two fields use ||.

Example

Concatenate two fields and call them product:

SELECT product_name || unit AS product
FROM products;
Run Example »

Note: In the result of the example above we are missing a space between product_name and unit. To add a space when concatenating, use || ' ' ||.

Example

Concatenate, with space:

SELECT product_name || ' ' || unit AS product
FROM products;
Run Example »

Using Aliases With a Space Character

If you want your alias to contain one or more spaces, like "My Great Products", surround your alias with double quotes.

Example

Surround your alias with double quotes:

SELECT product_name AS "My Great Products"
FROM products;
Run Example »

PostgreSQL Exercises

Test Yourself With Exercises

Exercise:

Make an alias for the field customer_name so that it will be dispalyed as Company:

SELECT customer_name  FROM customers;
        

Start the Exercise


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.