Tools
Pricing
Log in
Try for free

SQL - UNION ALL

Convert your text instructions into SQL queries - powered by AI.

Generate
Explain
SQL, MySQL, MSSQL, PostgreSQL, BigQuery SQL, Redshift SQL, Oracle SQL, SQLite, MariaDB SQL, MongoDB SQL, GraphQL SQL
query
based on
Generate with AI

What is UNION ALL?

UNION ALL is a SQL operation that combines the results of two or more SELECT queries into a single result set. Unlike the UNION operator, which removes duplicate records from the result, UNION ALL includes all records from the combined queries, even if they are duplicates. This makes UNION ALL a useful tool when you want to retain all data, including repeated entries.

In simpler terms, UNION ALL gives you everything from the combined queries without filtering out any duplicates.

A Practical Example

Imagine you are managing a library database with two tables: one for books currently available and another for books that are checked out.

Available Books Table:

BookID Title Author
1 The Great Gatsby F. Scott Fitzgerald
2 1984 George Orwell
3 To Kill a Mockingbird Harper Lee

Checked Out Books Table:

BookID Title Author
1 The Great Gatsby F. Scott Fitzgerald
4 Brave New World Aldous Huxley

You want to create a comprehensive list of all books in the library, including both available and checked out books, even if some titles appear in both tables.

SQL Query Using UNION ALL

To achieve this, you would use UNION ALL:

SQL icon SQL
SELECT BookID, Title, Author
FROM AvailableBooks
UNION ALL
SELECT BookID, Title, Author
FROM CheckedOutBooks;

Result of the Query:

This query retrieves all books from both tables, including duplicates. The output would look like this:

BookID Title Author
1 The Great Gatsby F. Scott Fitzgerald
2 1984 George Orwell
3 To Kill a Mockingbird Harper Lee
1 The Great Gatsby F. Scott Fitzgerald
4 Brave New World Aldous Huxley

Here, "The Great Gatsby" appears twice because it is present in both the available and checked out books tables.

Why Use UNION ALL?

UNION ALL is particularly useful when you want to ensure that no data is lost during the combination of results. In our example, you may want to see how many times a book appears in the library, regardless of its status. If you used UNION instead, the duplicate entry for "The Great Gatsby" would be eliminated, and you would lose valuable information about its availability.

Key Takeaways:

  • UNION ALL: Combines results from multiple SELECT queries, including duplicates.
  • Useful for Data Completeness: Retains all records, providing a full picture of the data across multiple tables.
  • Common Use Cases: Ideal for aggregating data from different sources, such as sales records from different regions, customer lists from various campaigns, or any scenario where you want to see all entries without filtering out duplicates.

Understanding how to use UNION ALL effectively can enhance your data analysis capabilities, allowing you to create more comprehensive reports and insights.

Happy querying!

Try our other free AI data tools
//e1b5c549a6dd5273e224cd87b24dd3fb.cdn.bubble.io/f1712248048835x171910885545995420/data%20analyser.svg_icon
AI Data Analysis Chat
Chat with your data to generate visualizations, spreadsheets, insights, advanced analysis & more.
//e1b5c549a6dd5273e224cd87b24dd3fb.cdn.bubble.io/f1711640762344x820626742105414500/excel-formula-generator_icon.svg_icon
AI Excel Formula Generator
Convert your text instructions into formulas or input a formula to have it explained with our Excel AI Formula Generator.
//e1b5c549a6dd5273e224cd87b24dd3fb.cdn.bubble.io/f1724422435477x384387332830927740/sentiment-analysis-tool.svg_icon
Sentiment Analysis Tool
Upload a file or list of text to generate the sentiment - positive, negative or neutral.