All the course material is stored in the SQL Crash Course repository.
Hi everyone! Once again, Cornellius and Josep Ferrer from DataBites here👋.
Let’s continue our SQL Crash Course journey!📚
Whether this is your first time or you need a refresher, we're excited to discuss seven key SQL topics together!
Each topic will be broken down into engaging posts published on Non-Brand Data and DataBites.
📚 Previously in SQL Basics…
Remember last Thursday we already saw
📌 #5 Filtering and Sorting by
in DataBites📌 #6 Aggregate Functions by me in Non-Brand Data.
Today, two fresh issues just dropped:
📌 #7 – JOINS (INNER, LEFT, RIGHT, FULL) – Where Josep will explain how to connect multiple tables effectively.
📌 #8 – UNION & UNION ALL – the current article where we learn how to stack query results efficiently.
As we begin our four-part Intermediate SQL journey, let’s take a look at what you will learn:
JOINS (INNER, LEFT, RIGHT, FULL)
UNION & UNION ALL
CASE Expressions
Functions (String, Date, Numeric)
Let’s keep the SQL momentum going together- don't miss out!
SQL UNION🤝- Merging Data Streams Seamlessly
In relational databases, data is often split into different structured tables for efficiency. However, this means that related data may be located in different tables.
UNION
and UNION ALL
enables you to combine results from multiple tables vertically into a single dataset.
Let’s say you have the following table: POSTS
, INTERACTIONS
, and NEWSLETTERS
NEWSLETTERS
holds metadata about newsletter information.POSTS
stores individual articles.
You then need a comprehensive list of all names (newsletters and posts), but how can you obtain that information?
That’s where UNION
and UNION ALL
shine.🌟
By using the UNION
and UNION ALL
w
e can stack the information from different tables into one single table. Though, it means UNION
and UNION ALL
need to have the same number of columns for the output.
Just imagine that it literally stacks the columns vertically; you can have different information with different column names stacked together, but you can’t have a different number of columns.
But let’s be real — understanding when to use UNION
vs. UNION ALL
is sometimes could trip people up.
The question becomes: Do you need unique results? Should duplicates stay or go?🤔
Let’s break it down together. 👇
✅ UNION
🔹 What it does:
Stacks results from two SELECT
queries with the same column numbers vertically and removes duplicates.
🔹 Use case:
Create a unique list of names from both NEWSLETTERS
and POSTS
, ensuring no repeats.
Structure:
SELECT column FROM TableA
UNION
SELECT column FROM TableB
Real Example:
SELECT name FROM newsletters
UNION
SELECT name FROM posts;
🧠 If "DataBites" exists in both tables, it appears once in the result.
✅ UNION ALL
🔹 What it does:
Stacks results from two SELECT
queries with the same column numbers vertically and keep all duplicates.
🔹 Use case:
Aggregate all posts from 2023 and 2024 into a single list (duplicates allowed for performance)
Structure:
SELECT column FROM TableA
UNION ALL
SELECT column FROM TableB
Real Example:
SELECT name FROM newsletters
UNION ALL
SELECT name FROM posts
🧠 If "DataBites" exists in both tables, it appears multiple times in the result.
💡 BONUS: Stack 3 Tables With Mixed Data Types
🔹 What it does:
Aggregates data vertically from NEWSLETTERS
, POSTS
, and INTERACTIONS
into a single list — not by linking relationships but by stacking results.
🔹 Use case:
Create a master list of all IDs, names, and numeric values (like points
) across unrelated datasets.
SELECT id, name, NULL AS points
FROM newsletters
UNION ALL
SELECT id, name, NULL AS points
FROM posts
UNION ALL
SELECT id, type_of_interaction AS name, points
FROM interactions;
🧠 This is how you compile data into a single report, even if the tables aren’t directly related.
🚨Summary
Here is the summary of what we learn from this course.
And now? It is your turn to play in our SQL playground with UNION and UNION ALL.
👉🏻 SQL playground with Union and Union All
How to Get Started 🚀
Over the coming weeks, we’ll guide you through:
✅ SQL Fundamentals
✅ Intermediate SQL
✅ Advanced SQL
✅ Database Operations
✅ Writing Efficient Queries
Once you grasp the basics, practice is key! Start working on real-world projects and solving hands-on data problems.
What’s Next? ➡️
This is the first of many posts about the upcoming SQL Courses. It will only explain what SQL is in its crude form.
To get the full experience and fully immersed in the learning:
👉 Subscribe to Databites.tech (By Josep)
👉 Subscribe to Non-Brand Data (By Cornellius)
👉 Check out the SQL Crash Course GitHub repo
👉 Share with your friend and whoever needs it!
🗓️ Every Thursday, you will have two new issues in your inbox!
Let’s dive in and make SQL less scary, more fun, and way more useful! 🚀
Josep & Cornellius