INTERSECT operator in
Oracle SQL is used to return the common rows from two SELECT statements.
It returns only the rows that are present in both result sets.
This operator is commonly used to find common data between two tables or
queries. Similar to set theory in mathematics, INTERSECT returns the
intersection of two sets.
1. Basic Syntax of INTERSECT
SELECT column1, column2, ...FROM table1WHERE condition1 INTERSECT SELECT column1, column2, ...FROM table2WHERE condition2;
- Both
SELECTstatements must return the same number of columns. - The data types of the corresponding columns must be compatible.
- The result set contains only those rows that are
common to both
SELECTstatements.
2. Key Characteristics of INTERSECT
a. Eliminates Duplicates by Default
- Like
UNION,INTERSECTautomatically eliminates duplicates. If a row appears multiple times in bothSELECTstatements, only one instance of that row will be included in the result set.
b. Same Number of Columns and Compatible Data Types
- The
SELECTstatements involved in theINTERSECToperation must have the same number of columns. - The corresponding columns must have compatible data types. For
example, a
VARCHARcolumn can intersect with anotherVARCHARcolumn, but not with aNUMBERcolumn.
c. Order of Queries
- The first
query (before
INTERSECT) is considered the base query, and the results will be compared against the second query. - The result set will only include rows that are in both result sets, meaning the rows in the first result set that also appear in the second.
3. How INTERSECT Works
a. Intersection of Two Result Sets
- The result of
INTERSECTis the set of rows that exist in bothSELECTstatements. - For two rows to be considered the same (i.e., to appear in the intersection), they must have exactly the same values in each column of the result set.
Example:
SELECT column1 FROM table1INTERSECTSELECT column1 FROM table2;
This will return all values from column1
that are present in both table1 and table2.
b. Removes Duplicates
INTERSECTeliminates duplicate rows by default. Even if a row appears multiple times in bothSELECTstatements, it will only appear once in the result set.
Example:
SELECT column1 FROM table1INTERSECTSELECT column1 FROM table2;
If table1 has two
occurrences of 'A' and table2 has one occurrence of 'A', the result
will return 'A' only once.
4. Use Cases for INTERSECT
a. Finding Common Records
INTERSECTis commonly used to find common records between two tables or queries. For example, if you want to identify employees that are present in both theemployeestable and thecontractorstable:
SELECT employee_id FROM employeesINTERSECTSELECT employee_id FROM contractors;
This query will return the employee_ids
that exist in both the employees and contractors tables.
b. Comparing Two Data Sets
- You can use
INTERSECTto compare two datasets and retrieve only the rows that are shared between them.
For example, comparing two sets of product IDs:
SELECT product_id FROM supplier1_productsINTERSECTSELECT product_id FROM supplier2_products;
This will return all product IDs that are found in both suppliers' product lists.
c. Identifying Overlapping Data
INTERSECTis helpful when you need to identify overlapping or matching data between two result sets, especially when both sets are expected to contain some shared data points.
Example:
SELECT order_id FROM ordersINTERSECTSELECT order_id FROM payments;
This query will return all order IDs
that have entries in both the orders table and the payments table, meaning
the orders that have been paid.
5. Performance Considerations
- Removing
Duplicates: Since
INTERSECTremoves duplicates, it may require more processing time thanUNION ALL(which doesn’t remove duplicates). For large datasets, the database engine must compare and eliminate duplicates from both result sets. - Indexes:
Performance can be improved by having indexes on the columns involved in
the
INTERSECToperation, especially if the tables contain large amounts of data. - Optimizing
Queries: Ensure that the
SELECTstatements are optimized, asINTERSECToperates over the entire result set of both queries.
6. Examples of Using INTERSECT
Example 1: Common Employees in Two Tables
You have two tables: employees and contractors.
To find employees who are also listed as contractors:
SELECT employee_id FROM employeesINTERSECTSELECT employee_id FROM contractors;
This query will return all employee_ids
that exist in both the employees and contractors tables.
Example 2: Finding Common Products Between Two Suppliers
You want to find the common products
between two suppliers, supplier1_products and supplier2_products.
SELECT product_id FROM supplier1_productsINTERSECTSELECT product_id FROM supplier2_products;
This will return all product IDs that exist in both suppliers' product lists.
Example 3: Finding Common Orders in
orders
and payments
Tables
To find orders that have corresponding
payment records, you can use INTERSECT:
SELECT order_id FROM ordersINTERSECTSELECT order_id FROM payments;
This will return the order_ids that are
present in both the orders and payments tables.
7. Handling INTERSECT
with More Complex Queries
You can use INTERSECT in more
complex queries involving joins or subqueries.
Example: Common Records After Join
If you have two tables, employees and contractors,
and want to find the common departments where both employees and contractors
work, you could do:
SELECT department FROM employeesINTERSECTSELECT department FROM contractors;
This will return the list of departments
that are common to both employees and contractors.
Example with Subqueries
You can also use subqueries with INTERSECT.
For example, finding orders that were both completed and paid:
SELECT order_id FROM (SELECT order_id FROM orders WHERE order_status = 'completed')INTERSECTSELECT order_id FROM (SELECT order_id FROM payments WHERE payment_status = 'successful');
8. INTERSECT vs. MINUS
INTERSECT: Returns the rows that are common to bothSELECTqueries.MINUS: Returns the rows that are in the firstSELECTquery but not in the second.
For example:
SELECT column1 FROM table1INTERSECTSELECT column1 FROM table2;
This query returns common values between
table1
and table2.
SELECT column1 FROM table1MINUSSELECT column1 FROM table2;
This query returns values from table1
that are not in table2.
9. Can INTERSECT be
used with ORDER BY?
Yes, you can use ORDER BY with INTERSECT,
but the ORDER BY clause must be placed at
the end of the entire query:
SELECT column1 FROM table1INTERSECTSELECT column1 FROM table2ORDER BY column1;
This will return the intersection of column1
from both tables, sorted in ascending order.
10. Can INTERSECT be
used with GROUP BY?
Yes, you can use INTERSECT with
aggregate functions and GROUP BY. For example, if you want to find the
departments where both employees and contractors have the same average salary:
SELECT department, AVG(salary) FROM employeesGROUP BY departmentINTERSECTSELECT department, AVG(salary) FROM contractorsGROUP BY department;
This will return departments where the average salary for employees matches the average salary for contractors.
No comments:
Post a Comment