Combining Results of two or SELECT Statements using UNION OPERATOR:
UNION Operator is used to combine the results of two or more SELECT statements. But UNION operator does not return duplicate rows so to return duplicate rows UNION ALL operator is used.
Some important points while using UNION Operator:
- All SELECT statement must have same
number of columns to be fetched .
- All SELECT statement columns to be
fetched must have similar dataypes .
Syntax for UNION operator:
SELECT columnname(s)
FROM tablename
WHERE [condition]
UNION
SELECT columnname(s)
FROM tablename
WHERE [condition];
Syntax for UNION ALL operator:
SELECT columnname(s)
FROM tablename
WHERE [condition]
UNION ALL
SELECT columnname(s)
FROM tablename
WHERE [condition];
Example:
SELECT City FROM Employee
UNION
SELECT City FROM Trainee;
The above query will fetch all different cities from Employee and Trainee tables.
0 Comment(s)