SQL -Structured Query Language - Sql Clause - As or Alias Tutorial
To create a short name for a long column or table name temporarily. Then we can use an ' AS ' statement or Alias.
This is mainly used in joins,i.e when using two tables. Suppose the two tables have the same column name ( for e.g id), then it can easy to identify if Alias is used with the column name.
Syntax-
The basic syntax of the AS statement is.–
SELECT short_name.id,short_name.name
FROM
table_name AS short_name;
Syntax using (inner joins)-
SELECT * FROM
table_name1 AS short_name1
inner join
table_name2 AS short_name2
WHERE
short_name1.col_name = short_name2.col_name;
For example( using inner join) -
SELECT e.employee_id,e.first_name,e.email,jh.start_date,jh.end_date
FROM
employees AS e
inner join
job_history AS jh
WHERE
e.employee_id = jh.employee_id;
If two table has the same column such as employee_id, then the alias is very useful to identify column have the same name. Because every time we cannot use the long name to use to identify columns having the same name. Hence we use an alias.
We can also Alias Column Name not only Table.