Oracle set operation function memo

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.