The set operator is used to merge the results of multiple select statements, including UNION, UNIONALL, INTERSECT, and MINUS. When using the set operator, make sure the difference
The set operator is used to merge the results of multiple select statements, including UNION, union all, INTERSECT, and MINUS. When using the set operator, make sure the difference
The set operator is used to merge the results of multiple select statements, including UNION, union all, INTERSECT, and MINUS. When using the set operator, make sure that the number of columns in Different queries matches the data type.
The set operator has the following considerations:
* The set operator is not applicable to LOB, VARRAY, and nested table columns.
* The UNION, INTERSECT, and MINUS operators are not used for LONG columns.
* If the selection list contains expressions or functions, the column alias must be defined for the expression or function.
1. UNION (UNION-free): When UNION is executed, duplicate rows in the result set are automatically removed and sorted in ascending order based on the results in the first column.
2. union all (with UNION set): duplicate rows are not removed and the result set is not sorted.
3. INTERSECT (intersection): obtains the intersection of the two result sets and sorts the results in ascending order in the first column.
Select id, name, job from worker
INTERSECT
Select empno, ename, job from emp;
4. MINUS (difference set): Data that exists only in the first set and does not exist in the second set. The results in the first column are sorted in ascending order.
5. In addition, you can use order
Order by must be placed after the last select statement. When the column names are the same, you can directly sort them by column names. If they are different, you can sort them by position or use aliases to make them the same.
Select id, name x from new_emp
Union all
Select empno, ename x from emp order by x; // alias sorting is not used for column names.
Select id, name ename from new_emp
Union all
Select empno, ename from emp order by ename; // The column names are sorted after the same alias is used.
Select id, name ename from new_emp
Union all
Select empno, ename from emp; // after merging, the column name is displayed as the original table.