EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Oracle Tutorial MINUS in Oracle
 

MINUS in Oracle

Priya Pedamkar
Article byPriya Pedamkar

Updated March 24, 2023

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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.

  • 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

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA

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

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

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

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

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

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW