Tutorial on SQL Alias Command
In this tutorial we are going to focus on the use of aliases.
There are two kinds of aliases that are used most often: column alias and table alias.
In brief, column aliases exist to help organizing output.
We can envision cases where the column heading might be complicated (particularly if it involves several arithmetic operations).
A use of a column alias makes the output much more readable and meaningful.
The table alias is another type of alias which is accomplished by putting an alias after the table name in the FROM clause. This is a convenient approach where you wish to obtain information from two different tables (the technical term is ‘perform joins’).
The benefit of using a table alias while performing joins is readily apparent when we talk about joins.
The syntax for both the column and table aliases is as follows:
SELECT “TableAlias”.”ColumnName1″ “ColumnAlias”
FROM “TableName” “TableAlias”;
In short, both categories of aliases are placed directly after the item they alias for, isolated by a white space. Let’s understand it via applying in table, Store Information:
Table Name : StoreInformation
We have applied both the column alias and the table alias:
SELECT A1.StoreName Store, SUM(A1.Sales) “Total Sales”
FROM StoreInformation A1
GROUP BY A1.StoreName;
Observe that difference in the result: the column titles are now different. That is the organized outcome of using the column alias.
So, rather than, somewhat cryptic “Sum(Sales)”, we now have “Total Sales”, that is much more understandable, as the column header.