NEW
Operations like union, intersection, and difference applied to sets or relations.
DESCRIPTION
Set operations derive result sets by combining or filtering tuples across relations based on mathematical set semantics. In SQL, UNION, INTERSECT, and EXCEPT implement these operations, typically producing distinct rows. They complement joins, projection, and selection from relational algebra. Proper use simplifies queries, removes duplicates, and expresses intent more clearly than complex joins. Multiset versions like UNION ALL preserve duplicates for performance and auditing use cases.
ALTERNATIVE DEFINITIONS
PostgreSQL Documentation
The UNION operator computes the union of the results of two SELECT statements.
Explicitly frames UNION as a set-theoretic union of SELECT results.
Oracle SQL Language Reference
The INTERSECT operator returns all distinct rows selected by both queries.
Highlights distinctness and two-query intersection semantics.
Microsoft Learn
Set operators combine results from two or more SELECT statements into a single result set.
Emphasizes combination of result sets via set semantics in T-SQL.
RELATED TERMS
Relational Algebra Union INTERSECT EXCEPT DISTINCT Join Projection Filtering Multiset SQL
COMPETING TERMS
Multiset Operations
RELATIONS
- Broader: Operations
KNOWLEDGE AREAS
Programming Languages Data Modeling Data Storage & Operations Data Engineering Data Integration & Interoperability
TARGET AUDIENCE
Data Engineer Data Analyst Data Scientist Software Developer Data Modeler BI Specialist
EXTERNAL SOURCES
- Wikipedia - Relational algebra
- PostgreSQL - UNION/INTERSECT/EXCEPT docs
- MySQL - UNION operator reference
- Apache Spark - Spark SQL set operators
TOP QUESTIONS
When should Set Operations be used instead of joins?
Use set operations when combining whole result sets or removing duplicates between queries. Joins are better for aligning related rows across tables by keys.
Category: HowTo
What is the difference between UNION and UNION ALL in Set Operations?
UNION removes duplicate rows, enforcing set semantics. UNION ALL preserves duplicates and is typically faster, reflecting multiset semantics.
Category: Concepts
How does INTERSECT work in SQL Set Operations?
INTERSECT returns distinct rows present in both result sets. It effectively computes the set-theoretic intersection.
Category: Definition
What performance considerations apply to Set Operations?
Set operations may involve sorting or hashing for deduplication. Using UNION ALL and proper indexing can significantly improve performance.
Category: Performance
Are Set Operations part of relational algebra?
Yes, union, intersection, and difference are core relational algebra operations derived from set theory.
Category: Theory
How do Set Operations affect NULL handling?
NULL comparisons vary by engine; equality in set operations follows SQL semantics, often treating NULLs distinctly and requiring careful testing.
Category: Mistakes
Can Set Operations be used across different schemas or databases?
Yes, if queries produce compatible schemas and types. Cross-database operations may require federated engines or data virtualization.
Category: Requirements
What is a common mistake with Set Operations?
Mismatched columns and types across SELECTs cause errors or implicit casts. Always align column order and compatible datatypes.
Category: Mistakes
How do Set Operations relate to DISTINCT?
UNION inherently applies DISTINCT across two result sets; DISTINCT applies within a single query result.
Category: Concepts