Member-only story
UNION vs. UNION ALL: The Guards of your combined SQL statements
You’re not a Medium member? Enjoy the full article for free HERE 👈😊
Summer is coming, and with it, the parties that roll on until the morning. When it comes to guests, the list might be as long as the ancient Egyptian scrolls that held their laws (or something equally endless — I’m sure you get the point 😉)
We all know that the more the merrier when it comes to parties — but what happens when the guest list exceeds the available space?
The same dilemma arises when you need to retrieve data from two different tables. How do you combine them? Do you take the UNION ALL
approach and let everyone in, including duplicates? Or do you manage the crowd by filtering out duplicate entries, just like UNION
?
Before choosing a strategy, let’s explore both options and determine which best fits your data needs. So, let’s dive in! 🤿
UNION…that guard that keeps the space safe
To avoid overcrowding and use database space wisely, UNION steps in like a big, tough security guard at the entrance, filtering out duplicate guests. Similarly, in our database, UNION does its job by combining results from two queries while removing duplicates.

With a guard like this, no one dares to sneak into your query result! 😂
How to Hire This Security Guard in SQL
It’s simpler than you think! Just use the keyword UNION in your SELECT statement:
SELECT column_a FROM table_1 UNION SELECT column_a FROM table_2;
As soon as this statement runs, our big guy gets to work — sorting and removing duplicates. (We’ll talk about his performance — or salary — a bit later! 😉)
UNION ALL…the more friendly guard
Unlike UNION
, which carefully filters out duplicates, UNION ALL
is the chill guard who lets everyone in — no questions asked! 😎