EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials Oracle Tutorial INTERSECT 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

INTERSECT in Oracle

By Priya PedamkarPriya Pedamkar

INTERSECT in Oracle

What is INTERSECT in Oracle?

INTERSECT is also called as ‘Vertical Join’, as the name says it is a type of SET function used to associate the contents of a number of tables and display it as a single result set, provided there are matching records from the SELECT query. The SELECT query for each table will be executed individually, but the result after INTERSECT will be displayed as a single set of records. It is important that all the columns involved in the INTERSECT query should have same data types in order to execute and fetch the results successfully.

  • INTERSECT operator returns only those rows that are returned by each of the SELECT statement.
  • INTERSECT 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 the INTERSECT operator applies on the sets and combines the result set to get the one final result or a single set with the matching record(s).

INTERSECT in Oracle 1-1

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Explanation: The INTERSECT query returns the records in the Intersect area. These are the records that exist in both Dataset 1 and 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)
INTERSECT
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 requirements.

Query:

SELECT empno, ename, job, deptno FROM Emp WHERE job=’SALESMAN’
INTERSECT
SELECT empno, ename, job, deptno FROM Emp WHERE deptno=30;

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 (86,112 ratings)

Output:

INTERSECT in Oracle 1-2

Rules and Restrictions

Before implementing of INTERSECT operator, we must know some important rules and restrictions of the INTERSECT operator. 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 INTERSECT operation can be ordered.
  • The column used for ordering can be defined through the column number.
  • To sort the final result set, the SELECT statement can contain an ORDER BY clause but the last SELECT statement only can contain the ORDER BY clause.

Implementations of INTERSECT in Oracle

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

Query:

SELECT * from Emp;

Output:

Emp Table

1. INTERSECT Operator without any Condition

Query:

SELECT empno, ename, deptno FROM Emp
INTERSECT
SELECT empno, ename, deptno FROM Emp;

Output:

SQL SELECT

In this SQL SELECT statement with INTERSECT operator, we got the same 14 records in which the EMP table consists because each SELECT statement generated an individual set of results but during the merge of the result sets, all records of one set matching with the other set.

2. INTERSECT Operator with WHERE Clause

Query:

SELECT ename, deptno FROM Emp WHERE deptno = 10
INTERSECT
SELECT ename, deptno FROM Emp WHERE deptno = 30;

Output:

no rows selected

In this example, both the SELECT statements create a separate result set based on condition respectively and INTERSECT operator merged both the sets and displayed a final result with ZERO records because

  • First SELECT statement fetched below records

INTERSECT in Oracle 1-6

  • And second SELECT statement fetched below records

INTERSECT in Oracle 1-7

And it clearly shows that the record(s) of the first set is not matching with the second set record(s), that’s why the final result shows ZERO rows selected output.

3. INTERSECT Operator with Different Number of Columns

Query:

SELECT empno, ename, deptno FROM Emp WHERE deptno = 10
INTERSECT
SELECT ename, deptno FROM Emp WHERE deptno = 30;

Output:

INTERSECT in Oracle 1-8

ERROR output! WHY?

As INTERSECT operator Rules and Restrictions says that the result sets of all SELECT statements must have the same NUMBER of columns. But in this example first SELECT statement having three columns but the second SELECT statement having two columns. That’s why OUTPUT showing an incorrect number of result columns error.

4. INTERSECT Operator with Invalid Column Name

Query:

SELECT dname, ename, deptno FROM Emp WHERE deptno = 10
INTERSECT
SELECT ename, deptno FROM Emp WHERE deptno = 30;

Output:

invalid identifier

ERROR output! WHY?

Because the column name “Dname” does not exist in the Emp Table. That’s why I got an invalid identifier.

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

5. INTERSECT Operator with Mismatch Data type

Query:

SELECT ename, hiredate, deptno FROM Emp WHERE job =’SALESMAN’
INTERSECT
SELECT ename, job, deptno FROM Emp WHERE deptno = 30;

Output:

INTERSECT in Oracle 1-10

ERROR Output! WHY?

As INTERSECT 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 with the result sets. First SELECT statement result set consisting column “Hiredate” which is DATE data type but the second result set consisting column “Job” which is VARCHAR2 data type it’s not matching. That’s why OUTPUT showing data type error.

6. INTERSECT Operator with ORDER BY Clause

Query:

SELECT empno, ename, deptno FROM Emp WHERE job =’SALESMAN’
ORDER BY empno
INTERSECT
SELECT empno, ename, deptno FROM Emp WHERE deptno = 30 ;

Output:

INTERSECT in Oracle 1-11

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

WHY THE ERROR?

In this example 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.

Query:

SELECT empno, ename, hiredate, deptno FROM Emp WHERE Job =’SALESMAN’
INTERSECT
SELECT empno, ename, hiredate, deptno FROM Emp WHERE deptno = 30
ORDER BY   1;

Output:

INTERSECT in Oracle 1-12

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

NOTE:

  • In the order by clause, you can also use the column position number instead of the column name.
  • In some cases, INTERSECT operation is costlier than other operations (join, sub query, etc).

Conclusion

INTERSECT Operator performs VERTICAL Join and returns only those record(s) that are matching with each SELECT statement result set. If you are looking for records, that exist in each SELECT statement result set, use the INTERSECT operator but column(s) number and data type must be the same.

Recommended Articles

This is a guide to the INTERSECT operator in Oracle. Here we discuss what is INTERSECT in oracle and the implementation of INTERSECT operator along with rules and restrictions. You may also look at the following articles to learn more –

  1. Architecture of RMAN Oracle?
  2. Education Required for Career in Oracle
  3. Oracle PL/SQL Interview Questions
  4. PostgreSQL INTERSECT | How to Works?
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