What are SQL aliases?
With SQL aliases, you can temporarily assign an alternative name to a column or table for a query. This can, in many cases, help to improve the readability of the code.
What are SQL aliases used for?
While there are numerous SQL commands that are essential for creating and managing data in the Structured Query Language, the command for creating aliases primarily exists to make your life easier. You can create SQL aliases to temporarily rename a column or table. Aliases are usually shorter and simpler than the names they are temporarily replacing. Aliases only exist for the duration of the query and have no impact on the actual name in the database. You can initiate an alias in SQL using the keyword AS
, although using the keyword is optional. In most cases, the use of aliases serves to improve the readability of code.
- Unlimited traffic
- Fast SSD NVMe storage
- Free Plesk Web Host Edition
What is the syntax for SQL aliases?
The syntax of SQL aliases differs depending on whether you want to create an alias for a single column or an entire table. To assign an alias to a column, use the following command:
In the code above, replace name_of_column
with the name of the column as it appears in your database. For alternative_name
, type the name that you want to temporarily use for the column. This will be the column’s alias. You also need to specify which table the column is in so the column can be found.
If you want to give an entire table an alias, use the following syntax:
The syntax is similar. This time though, you need to select the column(s) in the table, specify the current name of the table and then assign an alias to it.
SQL alias examples
To illustrates how SQL aliases work and what benefits they offer, let’s create a small sample database that contains customer data:
Now let’s add some data to it:
Next, we’re going to create a second table called “Orders”:
Now, let’s add some values to this table:
If we want to create an alias for this column, we can do so using the AS
command. In the following example, we’re going to give the column “OrderNumber” the alias “Number”:
When using SQL aliases for tables, you can retrieve data from different tables and join them together. Here, we’re going to use the alias “C” for the Customer table and “O” for Orders:
What alternatives to SQL aliases are there?
In some cases, a good alternative to SQL aliases is the database object SYNONYM, which can also be used to create simpler names. Unlike aliases though, you can use SYNONYM to exchange objects without negatively impacting the code.
The perfect database management system for your needs! With SQL server hosting from IONOS, you not only have the choice of MSSQL, MySQL or MariaDB, you also benefit from a robust security architecture and personalized, expert advice.