SQL Alias
Alias is used to give an alias name to a table or a column. This is quite useful in case of large or complex queries. Alias is mainly used for giving a short alias name for a column or a table with complex names.
Syntax of Alias for table names,
SELECT column-name from table-name as alias-name
Following is an Example using Alias,
SELECT * from Employee_detail as ed;
Alias syntax for columns will be like,
SELECT column-name as alias-name from table-name
Example using alias for columns,
SELECT customer_id as cid from Emp;
Example of Alias in SQL Query
Consider the following two tables,
The class table,
| ID | Name |
|---|---|
| 1 | abhi |
| 2 | adam |
| 3 | alex |
| 4 | anu |
| 5 | ashish |
The class_info table,
| ID | Address |
|---|---|
| 1 | DELHI |
| 2 | MUMBAI |
| 3 | CHENNAI |
| 7 | NOIDA |
| 8 | PANIPAT |
Below is the Query to fetch data from both the tables using SQL Alias,
SELECT C.id, C.Name, Ci.Address from Class as C, Class_info as Ci where C.id=Ci.id;
Result table look like,
| ID | Name | Address |
|---|---|---|
| 1 | abhi | DELHI |
| 2 | adam | MUMBAI |
| 3 | alex | CHENNAI |
No comments:
Post a Comment