Set Operators in MySQL
Combining Results from Multiple
Queries
Introduction to Set Operators
• • Definition: Used to combine results from
two or more SELECT queries.
• • Key Operators:
• - UNION
• - UNION ALL
• - INTERSECT (if supported)
• - EXCEPT / MINUS
UNION and UNION ALL
• • UNION: Combines results and removes
duplicates.
• • UNION ALL: Combines results without
removing duplicates.
• Example:
• SELECT name FROM students;
• UNION
• SELECT name FROM teachers;
INTERSECT (If Supported)
• • Returns rows that are common in both
queries.
• Example:
• SELECT id FROM orders;
• INTERSECT
• SELECT id FROM delivered_orders;
EXCEPT / MINUS
• • EXCEPT: Returns rows from the first query
not in the second.
• Example:
• SELECT id FROM orders;
• EXCEPT
• SELECT id FROM delivered_orders;
Rules for Set Operators
• • Same number of columns in queries.
• • Columns must have compatible data types.
• • ORDER BY is applied to the final result.
Conclusion
• • Effective for merging datasets.
• • Keep syntax rules in mind.
• • Enhances query flexibility and power.