1. Home
  2. Articles
  3. Courses
    1. Articles
  4. Community
  5. Definitions
  6. Files
    1. Terms Of Condition
  • Login
  • Register
  • Search
Course Articles
  • Everywhere
  • Articles
  • Pages
  • Forum
  • Definitions
  • Course Articles
  • Filebase Entry
  • More Options
  1. Scientific Tools
  2. Courses
  3. MySQL Guides

Practical guide for UNION and UNION ALL in MySQL with examples

  • SciTools
  • November 24, 2023 at 2:21 PM
  • 924 times read
Contents [hideshow]
  1. Understanding UNION and UNION ALL
  2. Basic Syntax
  3. Using UNION
    1. Example: Combining Different Tables
  4. Using UNION ALL
    1. Example: Including Duplicate Rows
  5. Sorting Results in UNION Queries
    1. Example: Sorted Combined Results
  6. Combining Results from Grouped Data
    1. Example: Aggregate Data from Different Tables
  7. Using UNION with Joins
    1. Example: Combining Results from Joined Tables
  8. Column Number and Type Matching

Subqueries in MySQL allow you to nest one SQL query within another. They can be used in a variety of contexts, including the SELECT, FROM, and WHERE clauses, as well as in conjunction with joins. In this guide, we will explain the basics of subqueries in MySQL with several practical examples.

Understanding UNION and UNION ALL

Both UNION and UNION ALL are used to combine the results of two or more SELECT statements. However, there's a key difference:

  • UNION: Combines results from multiple SELECT statements and removes duplicate rows.
  • UNION ALL: Combines results from multiple SELECT statements and includes all rows, including duplicates.

Basic Syntax

The basic structure for using UNION or UNION ALL is:

SQL
SELECT column_name(s) FROM table1
UNION [ALL]
SELECT column_name(s) FROM table2;

Using UNION

Example: Combining Different Tables

Imagine you have two tables, ProductsA and ProductsB, both with similar structures. To combine data from both:

SQL
SELECT Name, Price FROM ProductsA
UNION
SELECT Name, Price FROM ProductsB;

This query combines products from both tables into a single list, removing any duplicate products.

Using UNION ALL

Example: Including Duplicate Rows

If you want to include all products, even if they are duplicates:

SQL
SELECT Name, Price FROM ProductsA
UNION ALL
SELECT Name, Price FROM ProductsB;

This query lists all products from both tables, including duplicates.

Sorting Results in UNION Queries

You can sort the combined result of a UNION or UNION ALL query using an ORDER BY clause.

Example: Sorted Combined Results

To sort the combined list of products by price:

SQL
SELECT Name, Price FROM ProductsA
UNION
SELECT Name, Price FROM ProductsB
ORDER BY Price;

Combining Results from Grouped Data

UNION and UNION ALL can also combine results from queries that include grouping and aggregation.

Example: Aggregate Data from Different Tables

To get the count of products in each category from two tables:

SQL
SELECT Category, COUNT(*) AS Count FROM ProductsA GROUP BY Category
UNION
SELECT Category, COUNT(*) AS Count FROM ProductsB GROUP BY Category;

Using UNION with Joins

UNION can be used to combine results from queries that involve joins.

Example: Combining Results from Joined Tables

If you have a Users table and an Orders table, and you want to list names of users who have placed orders and those who have not:

SQL
SELECT Users.Name FROM Users
JOIN Orders ON Users.UserID = Orders.UserID
UNION
SELECT Users.Name FROM Users
LEFT JOIN Orders ON Users.UserID = Orders.UserID
WHERE Orders.OrderID IS NULL;

Column Number and Type Matching

When using UNION or UNION ALL, ensure that each SELECT statement has the same number of columns, in the same order, and with similar data types.

  • Previous Article Joining Tables in MySQL guide with examples
  • Next Article Advanced Filtering in MySQL guide and examples

Categories

  • MySQL

Archive

  1. 2023 (33)
    1. November (27)
      • Guide to MySQL IN and NOT IN Operators
      • Guide to MySQL OR Operator
      • Guide to MySQL AND Operator
      • Guide to MySQL DISTINCT Clause
      • MySQL SELECT FROM Statement Guide
      • What is EXPLAIN statement in MySQL and what it does ?
      • Guide to Query Caching in MySQL
      • How to Optimize MySQL Queries?
      • Guide to Entity-Relationship (ER) Diagrams in MySQL with Examples
      • What is Normal Forms and Normalization in MySQL
      • How to design a MySQL database with high performance
      • Principals of Secure Database Design in MySQL
      • How to secure and harden MySQL
      • User Management MySQL: Guide to Create, Manage and Permissions
      • Guide to Views in MySQL: Creating Updating Deleting
      • Guide to Stored Procedures and Functions in MySQL
      • Guide to Indexes in MySQL: Creating and Managing Indexes
      • Advanced Filtering in MySQL guide and examples
      • Practical guide for UNION and UNION ALL in MySQL with examples
      • Joining Tables in MySQL guide with examples
      • Guide to GROUP BY and HAVING clauses in MySQL with examples
      • MySQL ORDER BY Clause guide with practical examples
      • Complete MySQL WHERE Clause guide with examples
      • What is a MySQL subquery and how are subqueries executed in MySQL?
      • How to do queries in MySQL
      • How to create, modify, rename and delete tables using MySQL
      • MySQL CRUD Operations (Create, Read, Update, Delete)
    2. October (2)
    3. August (2)
    4. July (2)

Tags

  • union
  • union all
  1. Privacy Policy
  2. Legal Notice
Copyright© ScientificTools.org 2025. All rights reserved.
All the content posted on this website are licenses by MySecure Space GmbH under Creative Commons CC BY-NC-ND 4.0
Creative Commons CC BY-NC-ND 4.0
Developed & Hosted by: MySecure.Space | Powered by: WoltLab Suite™