Oracle union Union all intersect minus usage)

Source: Internet
Author: User
The set operation has three types of operations: Union, intersection, and difference. Union: returns the union of two query results and removes duplicate rows automatically. Union all is not sorted: the Union set of the two query results is obtained, and duplicate rows are not removed. Intersect will not be sorted either: Get the intersection of the two query results, and sort by the first column of the result set minus: Get the subtraction set of the two query results. The first column is used for sorting. Example: the following are two tables: one is a major curriculum and the other is an optional curriculum. This is the primary course: minors create table minors (minor_id Number Primary Key, minor_name varchar2 (30) not null, credit_hour number (2) insert 3 records: insert into minors values (10101, 'computer principle ', 4) insert into minors values (10201, 'automatic Control Principle', 3) insert into minors values (10301, 'engineering drawing principle ', 4) Create the elective course schedule minors2 create table minors2 (minor_id Number Primary Key, minor_name varchar2 (30) not null, credit_hour number (2) to insert two records: insert into minors2 values (10201, 'automatic control policy', 3) insert into minors2 values (10301, 'engineering drawing policy', 4) (1) two tables use Union all: the following result is obtained: Select minor_id, minor_name, credit_hour from minors Union all select minor_id, minor_name, credit_hour from minors2 order by credit_hour: minor_id minor_name credit_hour ---------- ------------------------------ ----------- 10201 Automatic Control Principle 3 10201 Automatic Control Principle 3 10101 Computer Principle 4 10301 Engineering Drawing Principle 4 10301 engineering drawing principle 4 (2) two tables use Union: the following result is displayed: Select minor_id, minor_name, credit_hour from minors Union select minor_id, minor_name, credit_hour from minors2 order by credit_hour: minor_id minor_name credit_hour ---------- -------------------------------- 10201 Automatic Control Principle 3 10101 Computer Principle 4 10301 engineering drawing principle 4 (3) Two tables use intersect: the following result is displayed: Select minor_id, minor_name, credit_hour from minors intersect select minor_id, minor_name, credit_hour from minors2 result: minor_id minor_name credit_hour ---------- automatic --------- 10201 Automatic Control Principle 3 10301 engineering drawing principle 4 (4) two tables use minus: the following result is displayed: Select minor_id, minor_name, credit_hour from minors minus select minor_id, minor_name, credit_hour from minors2. Result: minor_id minor_name credit_hour -------- ------------------------------ ----------- 10101 computer principle 4.
 from: http://www.douban.com/note/154030850/ 
Related Article

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.