Basic Oracle Tutorial: Set Operations

Source: Internet
Author: User

Set Operations
Used to merge results of multiple select statements
 
Union deduplication
Union all union
Intersect Intersection
Minus difference set
 
Union
Merge set A and Set B, but remove the duplicate parts of the Two Sets and sort them.
SCOTT @ ora10g> select deptno, ename from emp where deptno in (20, 30)
2 union
3 select deptno, ename from emp where deptno in (20, 10)
4;
DEPTNO ENAME
--------------------
10 CLARK
10 KING
10 MILLER
20 ADAMS
20 FORD
20 JONES
20 SCOTT
20 SMITH
30 ALLEN
30.
30 JAMES
30 MARTIN
30 TURNER
30 WARD

14 rows selected.

SCOTT @ ora10g>

Union all
Merge set A and Set B without duplicates or sorting

SCOTT @ ora10g> select deptno, ename from emp where deptno in (20, 30)
2 union all
3 select deptno, ename from emp where deptno in (20, 10)
4 *
SCOTT @ ora10g>/

DEPTNO ENAME
--------------------
20 SMITH
30 ALLEN
30 WARD
20 JONES
30 MARTIN
30.
20 SCOTT
30 TURNER
20 ADAMS
30 JAMES
20 FORD
20 SMITH
20 JONES
10 CLARK
20 SCOTT
10 KING
20 ADAMS
20 FORD
10 MILLER

19 rows selected.

SCOTT @ ora10g>

Intersect
The intersection of the two sets, sorted and de-duplicated

SCOTT @ ora10g> select deptno, ename from emp where deptno in (20, 30)
2 intersect
3 select deptno, ename from emp where deptno in (20, 10)
4 *
SCOTT @ ora10g>/

DEPTNO ENAME
--------------------
20 ADAMS
20 FORD
20 JONES
20 SCOTT
20 SMITH

SCOTT @ ora10g>

Minus
Take the difference set of the two sets, which exists in the set and does not exist in the B set (take the data that does not exist in the B set in the set) de-duplicate
SCOTT @ ora10g> select deptno, ename from emp where deptno in (20, 30)
2 minus
3 select deptno, ename from emp where deptno in (20, 10)
4 *
SCOTT @ ora10g>

DEPTNO ENAME
--------------------
30 ALLEN
30.
30 JAMES
30 MARTIN
30 TURNER
30 WARD

6 rows selected.

SCOTT @ ora10g>

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.