DataBolt

SQL LEARNING PLATFORM

15

CHAPTER 15

UNION SELECT

Combining results from multiple queries into a single result set.

5

Learning Blocks

SQL

Interactive Queries

In This Chapter

Concepts you'll master

1

UNION is used to combine results of two or more SELECT queries

2

Each SELECT must have the same number of columns

3

Column data types must be compatible

4

UNION removes duplicate rows by default

5

Use UNION ALL to include duplicates

🌟 Think of it this way: You have two lists β€” one of customers and one of employees. UNION merges them into a single list. It’s like stacking results on top of each other.

17.1 - UNION

Combine customer names and employee names into one list (duplicates removed).

SQL QUERY
SELECT name FROM customers
UNION
SELECT name FROM employees;

17.2 - UNION ALL

Combine results including duplicates.

SQL QUERY
SELECT name FROM customers
UNION ALL
SELECT name FROM employees;

βœ… Pro Tip: Use UNION when you want unique results. Use UNION ALL when performance matters and duplicates are allowed.

Exercise

Practice your SQL skills

SQL EDITOR
Press semicolon (;) to auto-run query

Tasks

Complete all SQL challenges

1

Combine customer and employee names (unique values)

Current Task
2

Combine customer and employee names (include duplicates)

3

Combine cities and departments

4

Combine countries and departments

5

Combine filtered results from two tables

6

Sort combined results

7

Sort combined results including duplicates

8

Remove duplicates from same table using UNION

9

Remove duplicates from employees

10

Keep duplicates using UNION ALL

Need help solving this task?