Code Like A Girl

Welcome to Code Like A Girl, a space that celebrates redefining society's perceptions of women in technology. Share your story with us!

Follow publication

Member-only story

UNION vs. UNION ALL: The Guards of your combined SQL statements

Luchiana Dumitrescu
Code Like A Girl
Published in
6 min read4 days ago

--

Photo by Yasir Eryilmaz on Unsplash

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.

The guy above is our UNION — the perfect guard to keep everything under control. (Source: https://www.clipartmax.com/so/security-guard-clip-art/)

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! 😎

--

--

Published in Code Like A Girl

Welcome to Code Like A Girl, a space that celebrates redefining society's perceptions of women in technology. Share your story with us!

Written by Luchiana Dumitrescu

I'm a BI Developer, bookworm, writer, and pet lover with a huge passion for coffee and data. Let's have fun exploring the world of data together! 🔎📈😊

No responses yet

Write a response