SQL INTERSECT

SQL INTERSECT

SQL INTERSECT

The INTERSECT operator in SQL returns the common rows (intersection) between two or more SELECT queries. The output includes only rows that appear in both result sets.


Why Use SQL INTERSECT?

  • To find common data between multiple tables or queries.
  • To identify overlapping datasets in complex queries.
  • Useful in scenarios where you need matching rows from different datasets.

Syntax

SELECT column1, column2, ... FROM table1 WHERE condition INTERSECT SELECT column1, column2, ... FROM table2 WHERE condition;

Key Points

  1. Same Number of Columns: Both SELECT statements must return the same number of columns.
  2. Compatible Data Types: The corresponding columns in the SELECT statements must have similar data types.
  3. Duplicates Removed: INTERSECT eliminates duplicates in the final result set by default.

Example Dataset

Students Table

StudentIDNameCourse
1John SmithMath
2Alice LeeScience
3Bob BrownHistory
4Emma DavisMath

Enrolled Table

StudentIDNameCourse
2Alice LeeScience
3Bob BrownHistory
4Emma DavisMath
5Harry JonesArt

Examples

1. Basic INTERSECT

Find students who are listed in both the Students and Enrolled tables.

SELECT StudentID, Name, Course FROM Students INTERSECT SELECT StudentID, Name, Course FROM Enrolled;

Result:

StudentIDNameCourse
2Alice LeeScience
3Bob BrownHistory
4Emma DavisMath

2. INTERSECT with Conditions

Find students enrolled in Math who appear in both tables.

SELECT StudentID, Name, Course FROM Students WHERE Course = 'Math' INTERSECT SELECT StudentID, Name, Course FROM Enrolled WHERE Course = 'Math';

Result:

StudentIDNameCourse
4Emma DavisMath

3. INTERSECT on Partial Columns

You can intersect specific columns (e.g., only StudentID):

SELECT StudentID FROM Students INTERSECT SELECT StudentID FROM Enrolled;

Result:

StudentID
2
3
4

Equivalent Query Without INTERSECT

You can achieve the same result using a JOIN:

SELECT s.StudentID, s.Name, s.Course FROM Students s INNER JOIN Enrolled e ON s.StudentID = e.StudentID AND s.Name = e.Name AND s.Course = e.Course;

While INTERSECT simplifies the syntax, this approach might be required in databases that don’t support INTERSECT.

Difference Between INTERSECT and Other Set Operators

FeatureINTERSECTUNIONEXCEPT (MINUS)
PurposeCommon rowsCombine resultsRows in the first query but not in the second
Removes DuplicatesYesYesYes
Order of ExecutionNot relevantNot relevantFirst query only

Database Support

  • Supported by:
    • SQL Server
    • PostgreSQL
    • Oracle
  • Not natively supported in MySQL (can use JOIN as a workaround).

Common Use Cases

  1. Identifying Overlapping Data:

    • Example: Find customers who purchased from both Store A and Store B.
  2. Consistency Checks:

    • Example: Verify that entries in two datasets are identical.
  3. Data Cleanup:

    • Example: Identify records duplicated across tables.

Performance Considerations

  1. Indexes: Ensure indexed columns are used in the queries for faster execution.
  2. Duplicates: Removing duplicates incurs additional overhead; consider using JOIN for larger datasets.
  3. Query Optimization: Use filtering conditions (WHERE) to limit the data before applying INTERSECT.

Conclusion

The SQL INTERSECT operator simplifies queries where you need to find common rows between datasets. While it requires compatible columns and data types, its simplicity and clarity make it a valuable tool for data analysis.

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close