Concept Flow - INTERSECT equivalent
Start
Query Set A
Find common rows
Output result
End
This flow shows how to find common rows between two queries by selecting rows that appear in both sets.
SELECT column1 FROM table1 WHERE condition1 AND column1 IN ( SELECT column1 FROM table2 WHERE condition2 );
| Step | Action | Query Part | Intermediate Result | Explanation |
|---|---|---|---|---|
| 1 | Execute subquery | SELECT column1 FROM table2 WHERE condition2 | [B1, B2, B3] | Get values from table2 matching condition2 |
| 2 | Execute main query with IN | SELECT column1 FROM table1 WHERE condition1 AND column1 IN (subquery) | [A2, A3] | Select rows from table1 where column1 is in subquery result |
| 3 | Output final result | Result set | [A2, A3] | Rows common to both queries output |
| 4 | End | - | - | No more steps, query complete |
| Variable | Start | After Step 1 | After Step 2 | Final |
|---|---|---|---|---|
| subquery_result | empty | [B1, B2, B3] | [B1, B2, B3] | [B1, B2, B3] |
| main_query_result | empty | empty | [A2, A3] | [A2, A3] |
INTERSECT finds common rows between two queries. MySQL lacks INTERSECT, so use IN with a subquery. Syntax: SELECT col FROM table1 WHERE col IN (SELECT col FROM table2); This returns rows in table1 also in table2. Duplicates in subquery don't affect results. Use this to simulate INTERSECT behavior.