UNION
The SQL UNION operator is used to combine the result sets of 2 or more SELECT statements. It removes duplicate rows between the various SELECT statements. Each SELECT statement within the UNION must have the same number of fields in the result sets with similar data types.
The basic syntax is as follows:
SELECT Column1, column2, ... column_n FROM tables WHERE conditions UNION SELECT Column1, column2, ... column_n FROM tables WHERE conditions; |
Parameters or Arguments
- A function such as SUM, COUNT, MIN, MAX, or AVG functions.
- The columns that are not encapsulated within an aggregate function and must be included in the GROUP BY clause.
The condition that is used to restrict the groups of returned rows. Only those groups whose condition evaluates to TRUE will be included in the result set.
|
|
Syntax For MySQL:
SELECT Dept_id FROM Employee UNION SELECT Dept_id FROM Department |
After Execute:
Dept_id |
2 |
4 |
1 |
3 |
8 |
0 |
5 |
UNION ALL
The UNION ALL operator is used to combine the results of two SELECT statements including duplicate rows. Each SELECT statement within the UNION ALL must have the same number of fields in the result sets with similar data types.
The basic syntax is as follows:
SELECT Column1, column2, ... column_n FROM tables WHERE conditions UNION ALL SELECT Column1, column2, ... column_n FROM tables WHERE conditions; |
SQL syntax:
SELECT Dept_id FROM Employee WHERE Salary > 60000 UNION ALL SELECT Dept_id FROM Department WHERE Dept_Name ='Sandeep'; |
After Execute:
Dept_id |
1 |
1 |
1 |
No comments:
Post a Comment