Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
In mathematical set theory, the Intersection of two sets represents the elements that exist in both Set A and Set B simultaneously. In standard ANSI SQL, this is expressed using the INTERSECT operator.
While modern MySQL 8.0.31+ natively added INTERSECT support, millions of production applications run on earlier MySQL versions or need alternative, highly optimized relational equivalents. In this tutorial, you will master both native INTERSECT and how to emulate it using INNER JOIN and IN.
INTERSECT (MySQL 8.0.31+)In modern MySQL (version 8.0.31 and newer), you can write INTERSECT directly:
INTERSECT:INTERSECT ALL.INNER JOIN (High-Performance)In any version of MySQL, the most performant and idiomatic way to calculate an intersection is using an INNER JOIN with DISTINCT:
IN OperatorYou can also express set intersection cleanly using an IN subquery:
EXISTSFor large tables with indexes, the EXISTS semi-join pattern is often the fastest execution strategy:
INTERSECT, two NULL values are considered equal to each other! In contrast, when emulating with INNER JOIN on a.id = b.id, NULL = NULL evaluates to UNKNOWN, meaning rows with NULL keys will not match. Use <=> (spaceship operator) if your key columns are nullable.INTERSECT automatically performs deduplication. If your datasets are already distinct, an INNER JOIN is often faster.INTERSECT operator return in relational database SQL?INTERSECT operator?INTERSECT between two result sets?INTERSECT treat two NULL values during comparison?Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Combining Results with UNION & UNION ALL | Emulating EXCEPT / MINUS in MySQL |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.