Now we focus here in SQL Tutorial on the use of aliases of SQL tables. There are two types
of aliases that are used most frequently: column alias and table alias.
In short, column aliases exist to help organizing output.
In the previous example, whenever we see total salary, it is
listed as SUM(Salary). While this is comprehensible, we can
envision cases where the column heading can be complicated
(especially if it involves several arithmetic operations).
Using a column alias would greatly make the output much more
readable.
The second type of alias is the table alias. This is
accomplished by putting an alias directly after the
table name in the FROM clause. This is convenient when
you want to obtain information from two separate tables
(the technical term is 'perform joins'). The advantage of
using a table alias when doing joins is readily apparent
when we talk about joins.
Before we get into joins, though, let's look at the syntax
for both the column and table aliases:
Briefly, both types of aliases are placed directly after the item they alias
for, separate by a white space. We again use our table of Employees.
Table
Employees_Salary
| FirstName |
Month |
Salary |
| Austin |
January |
1000$ |
| Creston |
January |
1000$ |
| Kate |
January |
1000$ |
| Angela |
January |
1900$ |
| Bobbi |
January |
1000$ |
| Cute |
January |
1000$ |
| Austin |
February |
1100$ |
| Creston |
February |
1100$ |
| Kate |
February |
1100$ |
| Angela |
February |
1000$ |
| Bobbi |
February |
1200$ |
| Cute |
February |
1300$ |
We use the same example as that in the SQL GROUP
BY section of our SQL Tutorial, except that we have put in both the
column alias and the table alias:
Important note is that difference in the result: the column titles are
now different from the original stored in database. That is the result of using the column alias.
Notice that instead of the somewhat hidden "Sum(Salary)", we
now have "Total Salary of Month", which is much more understandable, as
the column header. The advantage of using an SQL table alias is not apparent in
this example. However, we will learn more about the table alias in
next SQL Tutorial sections while using the complex kind of joins.