AS command in SQL - sql - sql tutorial - learn sql
Tags : sql tutorial , pl sql tutorial , mysql tutorial , oracle tutorial , learn sql , sql server tutorialSQL Aliases:
SQL Aliases:
- SQL aliases are used to give a table, or a column in a table, a temporary name.
- The keyword AS is used to assign an alias to the column or a table.
- Aliases are often used to make column names more readable.
- An alias only exists for the duration of the query.
- It is inserted between the column name & the column alias or between the table name & the table alias.
Syntax:
Alias Column Syntax
SELECT column_name AS alias_name
FROM table_name;
Alias Table Syntax
SELECT column_name(s)
FROM table_name AS alias_name;
Tags : sql tutorial , pl sql tutorial , mysql tutorial , oracle tutorial , learn sql , sql server tutorialExample:
Example:
- Consider the following two tables.
Table 1 − Employee Table is as follows.
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
1 | Ramesh | 32 | Ahmedabad | 2000.00 |
2 | Khilan | 25 | Delhi | 1500.00 |
3 | kaushik | 23 | Kota | 2000.00 |
4 | Chaitali | 25 | Mumbai | 6500.00 |
5 | Hardik | 27 | Bhopal | 8500.00 |
6 | Komal | 22 | MP | 4500.00 |
7 | Muffy | 24 | Indore | 10000.00 |
Table 2 − ORDERS Table is as follows.
OID | DATE | Employee_ID | AMOUNT |
---|---|---|---|
102 | 2009-10-08 00:00:00 | 3 | 3000 |
100 | 2009-10-08 00:00:00 | 3 | 1500 |
101 | 2009-11-20 00:00:00 | 2 | 1560 |
103 | 2008-05-20 00:00:00 | 4 | 2060 |
- The following code block shows the usage of a table alias.
- This would produce the following result.
ID | NAME | AGE | AMOUNT |
---|---|---|---|
3 | kaushik | 23 | 3000 |
3 | kaushik | 23 | 1500 |
2 | Khilan | 25 | 1560 |
4 | Chaitali | 25 | 2060 |
- Following is the usage of a column alias.
SQL> SELECT ID AS Employee_NAME AS Employee_NAME
FROM Employee
WHERE SALARY IS NOT NULL;
- This would produce the following result.
Employee_ID | Employee_NAME |
---|---|
1 | Ramesh |
2 | Khilan |
3 | kaushik |
4 | Chaitali |
5 | Hardik |
6 | Komal |
7 | Muffy |