In this post explains how to use ALIASES in SQL Server (Transact-SQL) with syntax and examples.
SQL Server (Transact-SQL) ALIASES can be used to create a temporary name for columns or tables.
The syntax to alias a column in SQL Server (Transact-SQL) is:
OR
The syntax to alias a table in SQL Server (Transact-SQL) is:
The original name of the column that you wish to alias.
The original name of the table that you wish to alias.
Optional. Most programmers will specify the AS keyword when aliasing a column name, but not when aliasing a table name. Whether you specify the AS keyword or not has no impact on the alias in MySQL. It is a personal choice in MySQL, unlike other databases. (Our examples will use AS when aliasing a column name but omit AS when aliasing a table name.)
The temporary name to assign to the column or table.
Generally, aliases are used to make the column headings in your result set easier to read. For example, when concatenating fields together, you might alias the result.
For example:
In this example, we've aliased the second column (ie: first_name and last_name concatenated) as NAME. As a result, NAME will display as the heading for the second column when the result set is returned. Because our alias_name did not include any spaces, we are not required to enclose the alias_name in quotes.
However, it would have been perfectly acceptable to write this example using quotes as follows:
Next, let's look at an example where we are required to enclose the alias_name in quotes.
For example:
In this example, we've aliased the second column (ie: first_name and last_name concatenated) as "EMPLOYEE NAME". Since there are spaces in this alias_name, "EMPLOYEE NAME" must be enclosed in quotes.
When you create an alias on a table, it is either because you plan to list the same table name more than once in the FROM clause (ie: self join), or you want to shorten the table name to make the SQL statement shorter and easier to read.
Let's look at an example of how to alias a table name.
For example:
In this example, we've created an alias for the products table called p. Now within this SQL statement, we can refer to the products table as p.
When creating table aliases, it is not necessary to create aliases for all of the tables listed in the FROM clause. You can choose to create aliases on any or all of the tables.
For example, we could modify our example above and create an alias for the inventory table as well.
Now we have an alias for inventory table called inv as well as the alias for the products table called p.