"Go" Oracle Set operation functions: Union, intersect, minus

Source: Internet
Author: User

The set operator is specifically used to combine the results of multiple SELECT statements, including: Union, union All, INTERSECT, minus. When you use the SET operator, you must ensure that the number of columns in different queries matches the data type.

The collection operator has the following considerations:

    • Collection operators are not available for LOB, Varray, and nested table columns.
    • The UNION, INTERSECT, minus operators are not used in long columns.
    • If the selection list contains an expression or function, you must define a column alias for the expression or function.

1. Union (no counterweight): When the union is executed, the duplicate rows in the result set are automatically removed and sorted in ascending order with the results of the first column.

2, UNION all (with heavy unions): do not remove duplicate rows, and do not sort the result set.

3, INTERSECT (intersection): take the intersection of two result sets, and the results of the first column in ascending order.

Select Id,name,job from Worker
INTERSECT
Select Empno,ename,job fromemp;

4, minus (difference set): displays only data that exists in the first collection that does not exist in the second collection. And the results of the first column are sorted in ascending order.

5. Alternatively, you can use ORDER by

The order by must be placed after the last SELECT statement, and when the column name is the same, it can be sorted directly with the column name, or the alias can be used if the difference can be sorted by location.

Select ID, name x from New_emp
UNION ALL
Select Empno, ename x from emp ORDER by x;//column names are not used with alias ordering

Select ID, name ename from New_emp
UNION ALL
Select Empno, ename from emp ORDER by ename;//column names do not use aliases at the same time to sort after

Select ID, name ename from New_emp
UNION ALL
Select Empno,ename from EMP; // Merge column name displays the previous table as the primary.

"Go" Oracle Set operation functions: Union, intersect, minus

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.