Oracle minus Keywords

Source: Internet
Author: User

Oracle minus Keywords
The minus keyword in sql
There is a minus keyword in SQL that uses two SQL statements to find out the results of the first SQL statement, and then see if the results are in the results of the second SQL statement. If so, the record is removed, not in the final result. If the result of the second SQL statement does not exist in the result of the first SQL statement, the data is discarded with the following syntax:
[SQL Segment 1]
Minus
[SQL Segment 2]
--------------------------------------------
CREATE TABLE 1
CREATE TABLE Test1
(
Name varchar (10),
Sex varchar (10),
);

INSERT into test1 values (' Test ', ' female ');
INSERT into test1 values (' test1 ', ' female ');
INSERT into test1 values (' test1 ', ' female ');
INSERT into test1 values (' test11 ', ' female ');
INSERT into test1 values (' test111 ', ' female ');

CREATE TABLE 2
CREATE TABLE Test2
(
Name varchar (10),
Sex varchar (10),
);
INSERT into test1 values (' Test ', ' female ');
INSERT into test1 values (' test2 ', ' female ');
INSERT into test1 values (' test2 ', ' female ');
INSERT into test1 values (' test22 ', ' female ');
INSERT into test1 values (' test222 ', ' female ');
-------------------------------------------

SELECT * from Test1 minus select * from Test2;

Results:

NAME SEX
---------- ----------
Test1 Female
test11 Female
test111 Female
-----------------------------------------------------------

SELECT * from Test2 minus select * from Test1;

Results:

NAME SEX
---------- ----------
Test2 Female
test22 Female
test222 Female

Conclusion: Minus returns always the data from the left table, which returns the difference set. Note: Minus has a shaving effect

==========================================================
Here is my experiment, it is clear that minus efficiency, made_order a total of 230,000 records, charge_detail a total of 170,000 records

Performance comparison:
SELECT order_id from Made_order
Minus
SELECT order_id from Charge_detail
1.14 sec
  
SELECT a.order_id from Made_order a
WHERE NOT EXISTS (
SELECT order_id
From Charge_detail
WHERE order_id = a.order_id
)
18.19 sec
  
SELECT order_id from Made_order
WHERE order_id Not IN (
SELECT order_id
From Charge_detail
)

Oracle minus Keywords

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.