EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials Oracle Tutorial MINUS in Oracle
Secondary Sidebar
Oracle Tutorial
  • Oracle SET Operators
    • UNION in Oracle
    • Oracle UNION ALL
    • INTERSECT in Oracle
    • MINUS in Oracle
  • Basic
    • Oracle Marketing Cloud
    • What is Oracle?
    • Career in Oracle
    • How to Install Oracle
    • Oracle Versions
    • What Is Oracle Database
    • Oracle Data Warehousing
    • Oracle Warehouse Builder
    • Career In Oracle Database Administrator
    • Career In Oracle DBA
    • What is Oracle RAC
    • Oracle DBA
    • Oracle? Vanderbilt
    • What is RMAN Oracle
    • Oracle Database Administration
    • Oracle Operators
    • Oracle Constraints
    • Oracle number
    • Oracle Data Types
    • Oracle UNIQUE Constraint
    • Oracle Check Constraint
  • Joins
    • Joins in Oracle
    • Inner Join in Oracle
    • Oracle Cross Join
    • Left Join in Oracle
    • OUTER Join in Oracle
    • Oracle Full Outer Join
    • Natural Join in Oracle
    • Oracle Self Join
    • Oracle hash join
    • Oracle? Update with Join
  • Advanced
    • Timestamp to Date in Oracle
    • Oracle Golden Gate
    • Oracle Virtual Machine
    • Oracle Describe Table
    • Oracle Clauses
    • Oracle Having Clause
    • Oracle?Primavera
    • Oracle FOREIGN Key
    • PIVOT in Oracle
    • Oracle Alter Table
    • Oracle Queries
    • Oracle Views
    • Oracle Window Functions
    • Oracle String Functions
    • Oracle Date Functions
    • Oracle Analytic Functions
    • Oracle Aggregate Functions
    • Select in Oracle
    • INSERT in Oracle
    • DISTINCT in Oracle
    • Function in Oracle
    • Oracle GROUP_CONCAT
    • Oracle INSTR()
    • Oracle CONVERT
    • Oracle LENGTH()
    • Oracle EXISTS
    • Oracle REPLACE()
    • Oracle MERGE
    • Oracle LEAD()
    • Oracle EXTRACT()
    • Oracle LISTAGG()
    • Oracle SYS_CONTEXT()
    • Oracle COALESCE
    • Oracle NVL()
    • Oracle SYSDATE()
    • Oracle?Date Format
    • Oracle SYS_GUID()
    • Oracle WILDCARDS
    • Oracle Synonyms
    • Oracle Subquery
    • BETWEEN in Oracle
    • FETCH in Oracle
    • Oracle Index
    • Oracle Function-based Index
    • Oracle UNIQUE Index
    • Oracle Bitmap Index
    • Oracle Column
    • Oracle Triggers
    • Oracle Procedures
    • Sample Database for Oracle
    • Oracle LIKE Operator
    • ORDER BY in Oracle
    • Oracle ORDER BY DESC
    • GROUP BY in Oracle
    • Oracle GROUP BY HAVING
    • Oracle Aliases
    • Table in Oracle
    • Oracle Temporary Table
    • Oracle? Table Partition
    • Oracle rename table
    • Oracle CTE
    • Cursor in Oracle
    • Oracle LOCK TABLE
    • Oracle Tablespace
    • Oracle CARDINALITY
    • Oracle REGEXP
    • Oracle REGEXP_REPLACE
    • Oracle to_date
    • JSON in Oracle
    • Oracle COMMIT
    • Oracle GRANT
    • Oracle MD5
    • Oracle ROLLBACK
    • Oracle Users
    • Oracle TIMESTAMP
    • IF THEN ELSE in Oracle
    • Oracle While Loop
    • Oracle Clone Database
    • Oracle Backup Database
    • Oracle? XML
    • Oracle XMLAGG
    • Oracle XMLTABLE
    • Oracle Performance Tuning
    • Oracle B Tree Index
    • Oracle fusion
    • Oracle ebs
    • Oracle GRC
    • Oracle ERP
    • Oracle ASM
    • Oracle Cloud
    • Oracle HCM Cloud
    • Oracle Integration Cloud
    • Oracle Jinitiator
    • Oracle pathfinder
    • Oracle VirtualBox
    • Oracle Weblogic Server
    • Oracle decode
    • Oracle Exadata
    • Oracle ZFS
    • Oracle? utilities
    • JDBC Driver for Oracle
    • Oracle? DBA Versions
    • Oracle DBA Salary
  • Interview Questions
    • Oracle Interview Questions
    • Oracle Apps Interview Questions
    • Oracle Apps Technical Interview Questions
    • Oracle Database Interview Questions
    • Oracle Forms Interview Questions
    • Oracle PL/SQL Interview Questions
    • Oracle RAC Interview Questions
    • Oracle SOA Interview Questions

Related Courses

Oracle Course Training

Oracle DBA Certification Course

MongoDB Certification Training

MINUS in Oracle

By Priya PedamkarPriya Pedamkar

MINUS in Oracle

What is Minus Operator in Oracle?

MINUS operator in Oracle is used in between multiple select statements, in turn to fetch the unique records out of all the select queries. The resulting outputs are the rows that does not repeat for more than one select statement in the MINUS query that was executed. The select queries here are executed separately, while the results are combined, and MINUS is applied on this result to get the final output. It is one of the VERTICAL JOIN operators available in Oracle’s PL/SQL.

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (85,992 ratings)

In simple words, MINUS operator returns only those rows which are unique in the first SELECT statement and does not exist in the second SELECT statement.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

  • MINUS operator can be applied on two or more than two SELECT statements.
  • MINUS operator is often called VERTICAL JOIN, as the result combines data from two or more SELECT statements based on column instead of rows.
  • The result of each SELECT statement can be treated as a set and MINUS operator applies on the sets and combines the result sets to get the one final result or a single set with the record(s) that exists only in FIRST SELECT statement and that would be UNIQUE.

Pictorial Representation:

Dataset 1 Dataset 2
1 2
2 3
3 4
1 5

MINUS in Oracle 1-1

Explanation: The MINUS query returns the record in the red outline area but does not share with the black outline area i.e. 1. This is the record that exists in Dataset 1 but not in Dataset 2.

Points of Concentration

  • The queries are all executed independently, but their output is merged
  • Only the final query ends with a semicolon ‘;’

Syntax:

SELECT col_1, col_2, ..., col_n FROM TableName WHERE condition(s)
MINUS
SELECT col_1, col_2, ..., col_n FROM TableName WHERE condition(s);

Description:

  • Col_1/2/n: The column(s) or calculation as per your requirement
  • Table Name: As per your requirement
  • WHERE: It’s optional, depends on your requirement

Query:

SELECT Empno, Ename, Job, Deptno FROM Emp WHERE Deptno=10
MINUS
SELECT Empno, Ename, Job, Deptno FROM Emp WHERE Deptno=30;

Output:

MINUS in Oracle 1-2

Examples to Implement MINUS in Oracle

In this section, we’ll see the implementation of the MINUS operator and its behavior. For that, we will use the below sample table (Emp) with 14 records to understand the MINUS operator behavior.

Query:

SELECT * from Emp;

Output:

Emp table

Example #1 – MINUS Operator without any Condition

Query:

SELECT Empno, Ename, Deptno FROM Emp
MINUS
SELECT Empno, Ename, Deptno FROM Emp;

Output:

no rows selected

In this example, MINUS operator returns ZERO record because each SELECT statement generates an individual set of the result but during the merge of the result sets, all records of the first result set exist in the second result set.

Example #2 – MINUS Operator with WHERE Clause

Query:

SELECT Empno, Ename, Deptno FROM Emp WHERE Deptno = 10
MINUS
SELECT Empno, Ename, Deptno FROM Emp WHERE Deptno = 30;

Output:

WHERE Clause

In this example, both the SELECT statements created a separate result set based on condition and MINUS operator merged both the sets and displayed a final result with three records from the first result set.

First SELECT statement fetched records as shown below

MINUS in Oracle 1-6

  • And second SELECT statement fetched records as shown below

MINUS in Oracle 1-7

And it clearly shows that the records of the first set are not matching with the second set records, that’s why the final result shows three rows output of the first result set.

Example #3 – MINUS Operator with Different Number of Columns

Query:

SELECT Empno, Ename, Deptno FROM Emp WHERE Deptno = 10
MINUS
SELECT Ename, Deptno FROM Emp WHERE Deptno = 30;

Output:

Error Query Block

Why Does ERROR Show in Output?

As MINUS operator Rules and Restrictions says that the result sets of all SELECT statements must have the same NUMBER of columns. But in this example, the first SELECT statement contains three columns but the second SELECT statement contains two columns.

Example #4 – MINUS Operator with Invalid Column Name

Query:

SELECT Dname, Ename, Deptno FROM Emp WHERE Deptno = 10
MINUS
SELECT Ename, Deptno FROM Emp WHERE Deptno = 30;

Output:

MINUS in Oracle 1-9

Why Does ERROR Show in Output?

As column name “Dname” does not exist in Emp Table, hence the output shows the invalid identifier.

NOTE: This example proves that each SELECT statement creates a separate result set.

Example #5 – MINUS Operator with Mismatch Data Type

Query:

SELECT Ename, Hiredate, Deptno FROM Emp WHERE Deptno =10
MINUS
SELECT Ename, Job, Deptno FROM Emp WHERE Deptno = 30;

Output:

MINUS in Oracle 1-10

Why Does ERROR Show in Output?

As MINUS operator Rules and Restrictions says that the data type of each column in the second result set must match the data type of its corresponding column in the first result set. But in this example, column numbers are the same in both the result sets but the data type is mismatching. The first SELECT statement result set consists of column “Hiredate” which is the DATE data type but the second result set consists of column “Job” which is a VARCHAR2 data type which is not matching, so the OUTPUT shows data type error.

Example #6 – MINUS Operator with ORDER BY Clause

Query:

SELECT Empno, Ename, Deptno FROM Emp WHERE Job =’SALESMAN’
ORDER BY Empno
MINUS
SELECT Empno, Ename, Deptno FROM Emp WHERE Deptno = 30;

Output:

MINUS in Oracle 1-11

In this example, output throws an ERROR even if the column number and data type criteria is matching with both the result sets.

Why Does Still ERROR Show in Output?

In this example, the ORDER BY clause used by the first SELECT statement to sort the result set. But we can only use the ORDER BY clause in the last SELECT statement. Following is the correct way to use the ORDER BY clause as shown below:

Query:

SELECT Empno, Ename, Job, Deptno FROM Emp WHERE Job =’SALESMAN’
MINUS
SELECT Empno, Ename, Job, Deptno FROM Emp WHERE Deptno = 10
ORDER BY   1;

Output:

MINUS in Oracle 1-12

In this example, the ORDER BY clause has been used by the last SELECT statement to sort the result without any error.

NOTE:

  • In the ORDER BY clause, column position number can be also used instead of a column name.
  • In some cases, MINUS operation is more costly than other operations (join, subquery, etc.)

Rules and Restrictions

Following are the important rules and restrictions of MINUS operator as listed below:

  • The result sets of the queries must have the same number of columns.
  • The data type of each column in the second result set must match the data type of its corresponding column in the first result set.
  • The two SELECT statements may not contain an ORDER BY clause, but the final result of the MINUS operation can be ordered.
  • The column used for ordering can be defined by the column number.
  • To sort the final result set, a SELECT statement may contain an ORDER BY clause but the last SELECT statement only may contain the ORDER BY clause.

Conclusion

MINUS Operator performs VERTICAL Join and returns only those record(s) that is/are NOT existing in second result set. To get the record(s), that is/are UNIQUE and doesn’t exist in the second SELECT statement result set, MINUS operator can be used but column(s) number and data type must be the same.

Recommended Articles

This is a guide to MINUS in Oracle. Here we discuss what is MINUS operator in oracle and points of concentration including rules and restriction with its examples. You may also look at the following articles to learn more –

  1. Examples of SQL SELECT Query
  2. How to Fetch the Data From Database?​ 
  3. Architecture of Oracle Database
  4. Steps to Install Oracle
Popular Course in this category
Oracle Training (14 Courses, 8+ Projects)
  14 Online Courses |  8 Hands-on Projects |  120+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Oracle DBA Database Management System Training (2 Courses)4.9
All in One Financial Analyst Bundle- 250+ Courses, 40+ Projects4.8
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more