Careeroza
INTERSECT
The SQL INTERSECT Operator
INTERSECT returns only rows that appear in both result sets.
Example
Customers who ordered in January and in March 2024:
SELECT CustomerID FROM Orders WHERE OrderDate LIKE '2024-01%'
INTERSECT
SELECT CustomerID FROM Orders WHERE OrderDate LIKE '2024-03%';Result: 1 row
| CustomerID |
|---|
| 4 |
Support
PostgreSQL, SQL Server, Oracle and SQLite support INTERSECT. MySQL supports it from version 8.0.31. In older MySQL, use IN or an inner join:
SELECT DISTINCT a.CustomerID
FROM Orders a
JOIN Orders b ON a.CustomerID = b.CustomerID
WHERE a.OrderDate LIKE '2024-01%' AND b.OrderDate LIKE '2024-03%';