If you have very long or complex tables name or columns name than it can be denoted with their alias.
There are two types of aliases that are used most frequently:
· Column alias and
· Table alias.
Syntax for Tables Alias:
SELECT column_name(s) |
Example:
SELECT s.first_name,s.Last_Name FROM student_details s; |
Note: In the above query, alias 's' is defined for the table student_details and the column first_name and Last_Name is selected from the table.
Syntax for Columns Alias:
SELECT column_name AS alias_name |
Example:
SELECT first_name AS Name FROM student_details; |
Result will look like this:
Name |
Priya Chandra |
Anjali Bhagwat |
Rahul Sharma |
An Example of both Column alias and Table alias
Suppose we have a table called "tbl_Employee" and another table called "tbl_Department". We will give the table aliases of "e" and "d" respectively.
Now we want to list all the Employees with there Departments.
We use the following SELECT statement:
SELECT e.EmpID, e.EmpName, d.DeparmentName FROM tbl_Employee AS e, tbl_Department AS d WHERE e.EmpID=d.EmpID_fk |
The same SELECT statement without aliases:
SELECT tbl_Employee.EmpID, tbl_Employee.EmpName, tbl_Department.DeptName FROM tbl_Employee, tbl_Department WHERE tbl_Employee.EmpID=tbl_Department.EmpID_fk |
As you'll see from the two SELECT statements above; aliases can make queries easier to both write and to read.
Advantages of Alias:
1. There are more than one tables involved in a query,
2. Functions are used in the query,
3. The column names are big or not readable,
4. More than one columns are combined together.
No comments:
Post a Comment