? MySQL UNION and UNION ALL
? Quick Overview
The UNION and UNION ALL operators in MySQL combine results from multiple SELECT queries into a single result set.
? Key Concepts
- UNION removes duplicate rows.
- UNION ALL keeps all rows including duplicates.
? Syntax / Theory
? View Code Example
SELECT column1, column2
FROM table1
UNION ALL
SELECT column1, column2
FROM table2;
? Code Example
? View Code Example
SELECT customer_name
FROM customers
UNION
SELECT supplier_name
FROM suppliers;
? Live Output / Explanation
The result combines rows from both tables into a single list.
? Interactive Idea
Try switching between UNION and UNION ALL to observe duplicate handling.
? Use Cases
- Merging data from similar tables
- Generating unified reports
- Cross-table searches
✅ Tips & Best Practices
- Use UNION ALL for better performance when duplicates are not a concern.
- Ensure column count and data types match.
? Try It Yourself
- Create a UNION query combining three tables.
- Compare performance of UNION vs UNION ALL.