SQL > SQL Commands > Alias

Alias refers to the practice of using a different temporary name (usually a short name or a name with a logical meaning) 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_Name  Sales  Txn_Date 
 Los Angeles  1500  Jan-05-1999 
 San Diego  250  Jan-07-1999 
 Los Angeles  300  Jan-08-1999 
 Boston  700  Jan-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:

Store Total Sales
Los Angeles 1800
San Diego 250
Boston 700

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

This page was last updated on May 07, 2022.




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