The SQL set operations are :
- Union Operation:
- Think of it as stacking one query result on top of another.
- If you have two queries, say Query A and Query B, the UNION operation combines the results of both.
- It removes duplicate rows, so you only get distinct rows in the final result.
- So, the result of a UNION operation contains all unique rows from both queries.
- Intersect Operation:
- Imagine you have two sets of data (results of two queries), and you want to find where they overlap.
- The INTERSECT operation gives you the common rows that appear in both sets.
- It only returns rows that are present in both queries’ results.
- So, the result of an INTERSECT operation contains only the rows that are common in both queries.
- Except Operation:
- This operation is like subtracting one set of data from another.
- If you have Query A and Query B, the EXCEPT operation gives you the rows that are in Query A but not in Query B.
- It essentially removes the rows that are common between the two queries, leaving you with the rows unique to Query A.
- So, the result of an EXCEPT operation contains only the rows that are exclusive to the first query.
In essence:
- UNION combines and removes duplicates.
- INTERSECT gives you common rows.
- EXCEPT gives you rows that are in the first query but not in the second query.