SQL > SQL Commands > Alias

Alias refers to the practice of using a different temporary name to a database table or a column in a table.

The main advantage of using an alias is to help make the SQL statement more concise and readable. In addition, the output of the SQL statement can become more understandable with the use of an alias.

Syntax

The syntax for a table alias and a column alias is as follows:

SELECT "table_alias"."column_name1" "column_alias"
FROM "table_name" "table_alias";

Both types of aliases are placed directly after the item they alias for, separate by a white space.

Example

We use the following table for our example.

Table Store_Information

Store_NameSalesTxn_Date
Los Angeles1500Jan-05-1999
San Diego250Jan-07-1999
Los Angeles300Jan-08-1999
Boston700Jan-08-1999

We use the same SQL query as Example 1 in the SQL GROUP BY section, except that we have put in both the column alias and the table alias:

SELECT A1.Store_Name Store, SUM(A1.Sales) "Total Sales"
FROM Store_Information A1
GROUP BY A1.Store_Name;

Result:

StoreTotal Sales
Los Angeles1800
San Diego250
Boston700

Notice that difference in the result: the column titles are now different. That is the result of using the column alias. Instead of the somewhat cryptic "Sum(Sales)", we now have "Total Sales", which is much more understandable, as the column header. The advantage of using a table alias is not apparent in this example. However, they will become evident when we look at join operations in SQL.

Next: SQL AS




Copyright © 2017   1keydata.com   All Rights Reserved     Privacy Policy     About   Contact