- SQL Basics
- SQL Introduction
- SQL Syntax
- SQL Sample Database
- SQL SELECT
- SQL WHERE
- SQL ORDER BY
- SQL DISTINCT
- SQL LIMIT
- SQL FETCH
- SQL AND
- SQL OR
- SQL BETWEEN
- SQL IN
- SQL LIKE
- SQL IS NULL
- SQL Comparison Operators
- SQL Logical Operators
- SQL Alias
- SQL CASE
- Joins and Subqueries
- SQL INNER JOIN
- SQL LEFT JOIN
- SQL RIGHT JOIN
- SQL FULL OUTER JOIN
- SQL SELF JOIN
- SQL CROSS JOIN
- SQL Subquery
- SQL Correlated Subquery
- SQL UNION
- SQL INTERSECT
- SQL EXCEPT
- Aggregate Functions
- SQL AVG
- SQL COUNT
- SQL MAX
- SQL MIN
- SQL SUM
- SQL GROUP BY
- SQL HAVING
- SQL ROLLUP
- SQL CUBE
- SQL GROUPING SETS
- Database Management
- SQL CREATE DATABASE
- SQL ALTER DATABASE
- SQL DROP DATABASE
- SQL BACKUP DATABASE
- SQL SHOW DATABASES
- SQL SELECT DATABASE
- Table Management
- SQL CREATE TABLE
- SQL ALTER TABLE
- SQL ADD COLUMN
- SQL DROP COLUMN
- SQL DROP TABLE
- SQL TRUNCATE TABLE
- SQL SHOW TABLES
- SQL RENAME TABLE
- SQL Constraints
- SQL Primary Key
- SQL Foreign Key
- SQL UNIQUE Constraint
- SQL CHECK Constraint
- SQL NOT NULL Constraint
SQL INTERSECT
The INTERSECT operator in SQL is used to return the common rows between two SELECT
queries. It retrieves the rows that are present in both result sets. Similar to the UNION
operator, the INTERSECT
operator combines results from multiple SELECT
statements, but only the rows that appear in both queries will be returned.
Syntax of SQL INTERSECT
- SELECT statements: The individual queries whose results you want to intersect.
- INTERSECT: The operator used to find the common rows between the two result sets.
- columns: Both
SELECT
statements must have the same number of columns, and the corresponding columns must have compatible data types.
Key Points
- The
INTERSECT
operator returns only the common rows between two result sets. INTERSECT
removes duplicates by default. Each row returned will be unique.- The number of columns and their data types must be the same in both queries.
- It can be used with conditions on both queries to refine the result set.
Example of SQL INTERSECT
Consider the following two tables:
Table 1: Employees (Department A)
Table 2: Employees (Department B)
Using the INTERSECT
operator, we can find employees who appear in both tables:
Output:
Explanation:
- The result set includes only the rows that are common to both
employees_1
andemployees_2
. Alice and Bob are the only employees present in both tables.
SQL INTERSECT with Conditions
You can also apply conditions to both SELECT
queries when using INTERSECT
. For example, you might want to find employees who are in both tables and belong to a specific department:
Output:
Summary
The INTERSECT operator in SQL returns the rows that are common to both SELECT
queries. It ensures that only the matching rows from both queries are returned. The queries must have the same number of columns, and the data types of the columns should be compatible. INTERSECT
eliminates duplicates from the result, so only unique common rows are displayed.