SQL UNION Clause
SQL UNION Clause
The Union Clause is used to combine two separate select statements and
produce the result set as a union of both the select statements.
NOTE:
1. The fields to be used in both the select statements must be in same
order, same number and same data type.
2. The Union clause produces distinct values in the result set, to fetch the
duplicate values too UNION ALL must be used instead of just UNION.
Basic Syntax:
SELECT column_name(s) FROM table1 UNION SELECT column_name(s) FROM
table2;
Queries
To fetch distinct ROLL_NO from Student and Student_Details table.
SELECT ROLL_NO FROM Student UNION SELECT ROLL_NO FROM
Student_Details;
Output:
R
O
L
L
_
N
O
To fetch ROLL_NO from Student and Student_Details table including
duplicate values.
SELECT ROLL_NO FROM Student UNION ALL SELECT ROLL_NO FROM
Student_Details;
Output:
R
O
L
L
_
N
O
R
O
L
L
_
N
O
2 Computer Science
4 SURESH