Summary -
In this topic, we described about the UNION Operator with detailed example.
UNION operator is used to combine SELECT statements of two or more of different tables in result set. UNION operator fetching DISTINCT rows from SELECT statements. By using UNION operator we get first SELECT statement column names in the result set. The columns used in all SELECT statements with UNION operator should be in the same order of DATA TYPE and the column names may vary. The number of columns in all SELECT statements with UNION operator also should be same. We can use n number of SELECT statements with UNION operator.
Syntax -
SELECT Expression1 [AS Alias_name],Expressio2,…
FROM table1 [WHERE condition]
UNION
SELECT Expression1, Expressio2,… FROM table2 [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 |
010 | Employee6 | Analyst | Employee5 | 2019-07-11 | 27000.00 | 1000 |
011 | Employee8 | Salesman | Employee5 | 2019-09-09 | 10000.00 | 1000 |
department_details -
dept_id | dept_name | dept_location |
---|---|---|
1000 | Office | Location1 |
2000 | Sales | Location2 |
3000 | Office | Location1 |
Scenario – Getting DISTINCT rows from existing tables by using UNION operator.
Requirement – Getting DISTINCT rows from existing employee_details and department_details tables by using UNION operator. The query was as follows -
SELECT emp_name, designation FROM employee_details
UNION
SELECT dept_name, dept_location FROM department_details;
By executing above query we can get DISTINCT rows from both employee_details and department_details tables by using UNION operator. The output was as follows –
emp_name | designation |
---|---|
Employee1 | Director |
Employee2 | Director |
Employee3 | Manager |
Employee4 | Manager |
Employee5 | Analyst |
Employee6 | Analyst |
Employee7 | Clerk |
Employee8 | Salesman |
Employee9 | Salesman |
Office | Location1 |
Sales | Location2 |
Scenario – Getting single column with alias name by using UNION from existing tables.
Requirement – Getting single column values with alias name from both employee_details and department_details tables by using UNION operator. The query was as follows –
SELECT emp_name AS NAME FROM employee_details
UNION
SELECT dept_name FROM department_details;
By executing above query we can get single column values like emp_name, dept_name from both employee_details and department_details tables by using UNION operator. The output was as follows –
NAME |
---|
Employee1 |
Employee2 |
Employee3 |
Employee4 |
Employee5 |
Employee6 |
Employee7 |
Employee8 |
Employee9 |
Office |
Sales |
Scenario – Getting all rows from existing tables by using UNION operator with WHERE condition.
Requirement – Getting all rows from existing employee_details and department_details tables by using UNION operator with WHERE condition. The query was as follows –
SELECT emp_name,designation FROM employee_details WHERE dept_id = 1000
UNION
SELECT dept_name,dept_location FROM department_details WHERE dept_id = 1000;
By executing above query we can get all rows from both employee_details and department_details tables by using UNION operator whose dept_id is 100.The output was as follows -
emp_name | designation |
---|---|
Employee1 | Director |
Employee3 | Manager |
Employee5 | Analyst |
Employee6 | Analyst |
Employee7 | Clerk |
Office | Location1 |