UNION combines the result sets of two or more SELECT statements.
- Each query must have the same number of columns, with compatible types.
UNIONremoves duplicates;UNION ALLkeeps them (and is faster).- The column names come from the first query.
Combine the results of two queries.
✍️ 3 exercises + code challenge in this lessonUNION combines the result sets of two or more SELECT statements.
UNION removes duplicates; UNION ALL keeps them (and is faster).Mastering SQL UNION helps you write clean and reliable SQL code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.
SELECT City, 'Customer' AS Type FROM Customers WHERE Country = 'Pakistan'
UNION
SELECT 'Warehouse: ' || Category, 'Stock' FROM Products WHERE Stock = 0
ORDER BY Type, City;
Check what you learned in “SQL UNION”.
Combine two result sets and keep duplicates.
SELECT City FROM Customers SELECT City FROM Customers;
What does UNION do with duplicate rows?
Select CustomerName from Customers in 'UAE' UNION CustomerName from Customers in 'UK', sorted by name.
Free sign-in required · Google login available
Stuck on something?
Ask the community in the CodesInTech forum.
Need a developer?
Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.
Explore Our Services