SQL (Structured Query Language) includes an interesting feature called aliases. In this article, we will examine aliases, when they can be used, and highlight their advantages when executing SQL queries.
An alias in SQL is a feature designed to temporarily assign new names to tables and columns in a database. Aliases can only be used when executing a specific query using the SELECT
statement. The AS
operator is usually used for aliases, although the AS
keyword can be omitted in some databases. The renaming operation is a temporary change, and the actual name of the table/column does not change in the database structure. Aliases, particularly the AS
operator, are supported in all popular databases, including MySQL, PostgreSQL, Oracle Database, Microsoft SQL Server, and IBM Db2. The syntax for using aliases is as follows.
Alias for a table:
SELECT <column_name>
FROM <table_name> AS <table_alias>;
Alias for a column:
SELECT <column_name> AS <column_alias>
FROM <table_name>;
For multiple columns:
SELECT <column_name> AS <column_alias>, <column_name> AS <column_alias>
FROM <table_name>;
Alias names can only use Latin letters, numbers, and the underscore.
Let's consider the use of aliases with practical examples. We will work with PostgreSQL 15.
For example, let's take a database named orders. The database contains a table named list_information
, which holds information about customers in an online store. The table has the following columns:
user_identification_number customer_name customer_address customer_city customer_country postal_code
Let's create the database structure. The commands below were executed using the psql
console utility, which comes with PostgreSQL.
Creating the orders
database:
CREATE DATABASE orders;
Next, connect to the created database:
\c orders
Create the list_information
table and necessary columns:
CREATE TABLE list_information (
user_identification_number INT PRIMARY KEY,
customer_name VARCHAR(100) NOT NULL,
customer_address VARCHAR(255) NOT NULL,
customer_city VARCHAR(100) NOT NULL,
customer_country VARCHAR(100) NOT NULL,
postal_code INT NOT NULL
);
Since the name list_information
is not clear and does not reflect the content stored in the table, let's use an alias when executing the data selection query:
SELECT * FROM list_information AS customer_information;
Where customer_information
is a temporary name assigned to the list_information
table.
As mentioned earlier, in some databases, the AS
keyword can be omitted. In this case, the alias is specified immediately after the table name:
SELECT * FROM list_information customer_information;
This rule also applies to columns.
Now let's use an alias for a column. The principle is the same; only the AS
operator is specified after a specific table column. For example, the list_information
table has a column named user_identification_number
, which stores unique customer IDs. For reporting purposes or to shorten the information, a convenient name can be used for the column, for example, replacing it with user_ID
:
SELECT user_identification_number AS user_ID FROM list_information;
When executing the above query, the SQL console will display the previously assigned alias user_ID
as the column name, while the original column name (user_identification_number
) at the database level will not be changed — the alias is used only temporarily and only at the query execution level.
Aliases can also be used for multiple columns at once:
SELECT user_identification_number AS user_ID, customer_city AS city FROM list_information;
If the alias name contains a space, double quotes must be used. In the example below, an alias is created for the customer_city
column with the name person city
using double quotes:
SELECT customer_city AS "person city" FROM list_information;
An alias can also be used to combine multiple columns of a table. In PostgreSQL, the CONCAT
operator is used for this:
SELECT customer_name, CONCAT(customer_address, customer_city, customer_country, postal_code) AS Address
FROM list_information;
In the query above, 2 columns will be displayed: customer_name
and Address
. The Address
column (which is an alias) will display information from 4 columns at once: customer_address
, customer_city
, customer_country
, and postal_code
. Thus, the columns are combined. This is convenient for reporting.
Despite their simple function, aliases can be used in the following situations:
When working with multiple tables.
When using functions.
When column names do not reflect the current content and/or consist of a large number of characters. Useful for creating reports.
When combining two or more columns.
In this article, we examined aliases in SQL and reviewed practical examples of using them. Despite being easy to use, aliases can be incredibly useful in various scenarios, such as simplifying complex queries, making column names more readable, and improving the clarity of your SQL code. By incorporating aliases into your SQL toolkit, you can enhance the efficiency and readability of your database interactions, making your data management tasks more streamlined and effective.
Hostman provides a SQL cloud database for your needs.