UNION and UNION ALL
The UNION operator combines the result sets of 2 or more SELECT statements and removes duplicate rows between the various SELECT statements.
The UNION ALL operator combines the result sets of 2 or more SELECT statements and returns all rows from the query. It does not remove duplicate rows between the various SELECT statements.
Each SELECT statement within the UNION operator must have the same number of fields in the result sets with similar data types.
The syntax for the UNION ALL operator is as follows:
points_scored_current_week, that consists of these columns: id, first_half, and second_half.
Next, suppose that we have a second table,
points_scored_last_week, that consists of these columns: id, first_half, and second_half.
Here is an example that uses the UNION operator:
UNION and UNION ALL operators are both supported for streaming queries.
INTERSECT
The INTERSECT operator combines the result sets of 2 or more SELECT statements and returns only the rows that are common to all the SELECT statements. It removes duplicate rows from the final result set.
Each SELECT statement within the INTERSECT operator must have the same number of fields in the result sets with similar data types.
The syntax for the INTERSECT operator is as follows:
FROM clause.
WHERE conditions are optional. These conditions must be met for the records to be selected.
Suppose that we have a table,points_scored_current_week, that consists of these columns: id, first_half, and second_half.
Next, suppose that we have a second table,
points_scored_last_week, that consists of these columns: id, first_half, and second_half.
Here is an example that uses the
INTERSECT operator:
INTERSECT operator returned the rows that are common to both the points_scored_current_week and points_scored_last_week tables. If there were no common rows, the INTERSECT operator would return an empty set.
INTERSECT operator is supported for streaming queries.CORRESPONDING in set operations
Set operations (UNION, INTERSECT, and EXCEPT) typically require:
- Both queries to return the same number of columns
- Columns to match in left-to-right order (by column index)
CORRESPONDING keyword to match columns by name instead of order. This approach:
- Only combines columns that exist in both sets
- Ignores columns not present in both sets
- Considers columns matching if they have the same name or alias
CORRESPONDING gives you more flexibility when performing set operations, as it doesn’t rely on strict column ordering.
The syntax for using CORRESPONDING is as below:
<operation> is one of the below operations:
CORRESPONDING BY specification. Only columns that are specified and exist on both sides will be overlaid.
Here is a simple example. First, let’s create two tables employees and managers, and insert some data. Then you can use the CORRESPONDING keyword.