Using SQL UNION: Combining Data from Different Tables
What Will You Learn in This Guide?
This guide teaches you the correct use of UNION and UNION ALL operators in SQL.
You aggregate similar data from different tables into a single result set.
You clearly understand the differences in filtering, sorting and performance.
🧠 Technical Summary
Main topic: SQL UNION and UNION ALL operators.
Solved problem: Presenting similar data in separate tables as a single list.
Steps: UNION logic, filtering with WHERE, sorting with ORDER BY.
1. Basic Logic of UNION Operator
UNION combines the results of two SELECT queries.
Duplicate lines are automatically removed.
SELECT musteri_adi FROM kitap_satis
UNION
SELECT musteri_adi FROM kitap_kiralama;
- This query lists all unique customers.
2. UNION Terms of Use
- UNION requires some conditions to work correctly.
-
Column numbers must be equal
-
Column orders must be the same
-
Data types must be compatible
-
If these rules are violated, an error or incorrect result will occur.
3. Filtering with WHERE
- Each SELECT query can use its own WHERE condition.
SELECT kitap_adi FROM kitap_satis
WHERE musteri_adi = 'Ahmet'
UNION
SELECT kitap_adi FROM kitap_kiralama
WHERE musteri_adi = 'Ahmet';
- This query returns all the books Ahmet has read.
4. Sorting Results with ORDER BY
- ORDER BY always appears at the end of the query. Sorting is applied to the combined result set.
SELECT kitap_adi FROM kitap_satis
WHERE musteri_adi = 'Ahmet'
UNION
SELECT kitap_adi FROM kitap_kiralama
WHERE musteri_adi = 'Ahmet'
ORDER BY kitap_adi;
- This command sorts the books alphabetically.
5. Preserving Duplications with UNION ALL
- UNION deletes repetitions. UNION ALL preserves repeats and runs faster.
SELECT kitap_adi FROM kitap_satis
UNION ALL
SELECT kitap_adi FROM kitap_kiralama;
- This query joins all records as they are.
- Performance note: UNION ALL is more efficient on large data sets.
❓ Frequently Asked Questions (FAQ)
1. What is the difference between UNION and JOIN? JOIN joins columns, UNION joins rows.
2. Does UNION work if the column names are different? Yes, it works if the data types are compatible.
3. Can I use more than one UNION? Yes, multiple SELECT queries can be chained.
4. Why does UNION cause some records to be lost? Automatically clears duplicate rows.
🎯 Result
UNION is a powerful tool for reporting and analysis. UNION ALL is ideal for performance-oriented scenarios. Proper use produces clean and consistent results.
You can try the GenixNode platform now to run your SQL projects on a secure and high-performance infrastructure.

