Summary -
In this topic, we described about the AS Condition with detailed example.
AS condition is used to rename the column name in the result set or table name as alias name in the query. AS condition is used to rename the column or table name in result-set for the clear understanding. Alias exists only during the execution of the query.
Syntax -
SELECT column1, column2, ..., columnN
FROM table_name AS alias_name
[WHERE condition];
- column1, column2 – Specifies the column names that are used to fetch from table.
- alias_name – Specifies the user defined name used as alias.
Syntax for column-name –
SELECT column1 AS alias_name1,
column2 AS alias_name2,
...,
columnN AS alias_nameN
FROM table_name
[WHERE condition];
Example -
Let us consider below table(s) as an example table(s) to frame the SQL query for getting the desired results.
employee_details -
emp_id | emp_name | designation | manager_id | date_of_hire | salary | dept_id |
---|---|---|---|---|---|---|
001 | Employee1 | Director | 2019-07-11 | 45000.00 | 1000 | |
002 | Employee2 | Director | 2019-07-11 | 40000.00 | 2000 | |
003 | Employee3 | Manager | Employee1 | 2019-07-11 | 27000.00 | 1000 |
004 | Employee4 | Manager | Employee2 | 2019-10-08 | 25000.00 | 2000 |
005 | Employee5 | Analyst | Employee3 | 2019-07-11 | 20000.00 | 1000 |
006 | Employee6 | Analyst | Employee3 | 2019-10-08 | 18000.00 | 1000 |
007 | Employee7 | Clerk | Employee3 | 2019-07-11 | 15000.00 | 1000 |
008 | Employee8 | Salesman | Employee4 | 2019-09-09 | 14000.00 | 2000 |
009 | Employee9 | Salesman | Employee4 | 2019-10-08 | 13000.00 | 2000 |
department_details -
dept_id | dept_name | dept_location |
---|---|---|
1000 | Office | Location1 |
2000 | Sales | Location2 |
Scenario – Renaming the columns in the result-set.
Requirement – Fetch emp_id, salary from employee_details table and rename emp_id as Employee_Id. The query was as follows-
SELECT emp_id AS employee_id, salary FROM employee_details;
By executing above query, we can get results as shown below –
employee_id | salary |
---|---|
001 | 45000.00 |
002 | 40000.00 |
003 | 27000.00 |
004 | 25000.00 |
005 | 20000.00 |
006 | 18000.00 |
007 | 15000.00 |
008 | 14000.00 |
009 | 13000.00 |
Requirement – Calculate the total salary and display as "Total salary". The query was as follows –
SELECT SUM(salary) AS "Total salary" FROM employee_details;
By executing above query, we can get results as shown below -
Total salary |
---|
217000.00 |
Scenario – Renaming the tables in the result-set.
Requirement – Get emp_id, salary, dept_id from employee_details and corresponding dept_location from department_details tables. The query was as follows-
SELECT e.emp_id, e.salary, e.dept_id, d.dept_location
FROM employee_details as e, department_details as d
WHERE e.dept_id = d.dept_id;
By executing above query, we can get results as shown below –
emp_id | salary | dept_id | dept_location |
---|---|---|---|
001 | 45000.00 | 1000 | Location1 |
002 | 40000.00 | 2000 | Location2 |
003 | 27000.00 | 1000 | Location1 |
004 | 25000.00 | 2000 | Location2 |
005 | 20000.00 | 1000 | Location1 |
006 | 18000.00 | 1000 | Location1 |
007 | 15000.00 | 1000 | Location1 |
008 | 14000.00 | 2000 | Location2 |
009 | 13000.00 | 2000 | Location2 |
To resolve this issue,we have to specify the dept_id as emp loyee_details.dept_id, department_details.dept_id.Sometimes,the table names are too long to specify.So,we create alias names and use them to refer the common columns.