Skip to content

INTERSECTION

-- Assuming we have 2 tables:
-- TABLE_1
-- +-----+
-- | ID |
-- +-----+
-- | 1 |
-- | 2 |
-- | 3 |
-- +-----+
-- TABLE_2
-- +-----+
-- | ID |
-- +-----+
-- | 2 |
-- | 3 |
-- | 4 |
-- +-----+
SELECT ID FROM TABLE_1
INTERSECT
SELECT ID FROM TABLE_2;
-- Output
-- +-----+
-- | ID |
-- +-----+
-- | 2 |
-- | 3 |
-- +-----+

The INTERSECT operation in SQL is used to combine two SELECT statements and return only the records common to both. In this example, the INTERSECT operation returns the common ‘ID’ values from TABLE_1 and TABLE_2, resulting in outputs ‘2’ and ‘3’.