📅  最后修改于: 2023-12-03 15:18:08.528000             🧑  作者: Mango
In Oracle, INTERSECT
is a set operator that returns the common rows or records between two or more SELECT statements. The returned rows must be of the same data type or data structure. The INTERSECT
operator removes any duplicate records in the result set.
The basic syntax of the INTERSECT
operator is as follows:
SELECT column1, column2, ... FROM table1
INTERSECT
SELECT column1, column2, ... FROM table2;
Here, table1
and table2
are the tables we are querying data from. column1
, column2
, etc... are the columns we want to retrieve data from.
The result of the INTERSECT
operation is a new table that contains only the common records returned by both of the SELECT
statements.
Let's take an example of how we can use the INTERSECT
operator to find common records from two tables.
Suppose we have two tables named employees
and managers
. Both tables have a common column named emp_id
.
CREATE TABLE employees (
emp_id NUMBER(5) PRIMARY KEY,
emp_name VARCHAR2(50) NOT NULL,
age NUMBER(3) NOT NULL,
salary NUMBER(8,2) NOT NULL
);
CREATE TABLE managers (
emp_id NUMBER(5) PRIMARY KEY,
emp_name VARCHAR2(50) NOT NULL,
age NUMBER(3) NOT NULL,
salary NUMBER(8,2) NOT NULL
);
We can use the INTERSECT
operator to find common records between these tables by querying the emp_id
column in both tables.
SELECT emp_id FROM employees
INTERSECT
SELECT emp_id FROM managers;
This will return a new table that contains only the common emp_id
values from both tables.
The INTERSECT
operator in Oracle is a useful tool for finding common records between two or more tables. It allows us to retrieve only the data that is common to all of the queried tables, which can be very useful when dealing with large data sets.