Back to Python
2026-05-057 min read

MySQL UNION (Python Programming)

Learn MySQL UNION (Python Programming) step by step with clear examples and exercises.

Title: MySQL UNION Operator (Python Programming)

Why This Matters

The MySQL UNION operator is a powerful tool for combining the result-set of two or more SELECT statements. It's essential for data analysts, developers, and anyone working with large datasets to understand how to use it effectively in their Python scripts. This knowledge can help you avoid common pitfalls, optimize query performance, and ensure accurate data aggregation.

In this lesson, we will explore the MySQL UNION operator, its syntax, and various use cases. We'll also discuss common mistakes, practice questions, and frequently asked questions to help you master this important concept.

Prerequisites

Before diving into the MySQL UNION operator, make sure you have a good understanding of:

  • Python programming basics (variables, functions, control structures)
  • SQL syntax and database concepts (tables, columns, rows, joins, WHERE clause)
  • Basic MySQL commands for creating, inserting, and querying data in a MySQL database

Core Concept

The UNION operator is used to combine the result-sets of two or more SELECT statements. It eliminates duplicate rows while keeping the columns in the same order as the first query. Here's the basic syntax:

(SELECT column1, column2 FROM table1)
UNION
(SELECT column1, column2 FROM table2);

In this example, table1 and table2 are MySQL tables with identical columns (column1 and column2). The UNION operator combines the rows from both tables into a single result-set.

Union All

The UNION ALL operator is similar to UNION but does not eliminate duplicate rows. This means that all rows, including duplicates, will be included in the final result-set. The syntax for UNION ALL is:

(SELECT column1, column2 FROM table1)
UNION ALL
(SELECT column1, column2 FROM table2);

Order of Operations

When using multiple UNION or UNION ALL statements, the order of operations follows these rules:

  1. Each SELECT statement is executed independently.
  2. The results are combined according to the specified UNION or UNION ALL operator.
  3. The result-sets are sorted based on the first column in the final result-set.
  4. If necessary, NULL values are moved to the end of each sorted list before combining them.

Example

Let's create two tables (students_table1 and students_table2) with identical columns (name, age, and city) and some sample data:

import mysql.connector

mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword"
)

mycursor = mydb.cursor()

Create table1

mycursor.execute("CREATE TABLE IF NOT EXISTS students_table1 (name VARCHAR(25), age INT, city VARCHAR(20))")

Insert data into table1

mycursor.execute("INSERT INTO students_table1 VALUES ('John', 23, 'New York'), ('Emma', 27, 'Los Angeles')")

mydb.commit()

Create table2

mycursor.execute("CREATE TABLE IF NOT EXISTS students_table2 (name VARCHAR(25), age INT, city VARCHAR(20))")

Insert data into table2

mycursor.execute("INSERT INTO students_table2 VALUES ('Mike', 30, 'Chicago'), ('Sarah', 24, 'New York')")

mydb.commit()


Now let's combine the data from both tables using UNION and UNION ALL:

Combine data with UNION

mycursor.execute("SELECT FROM students_table1 UNION SELECT FROM students_table2")

Fetch all rows

results = mycursor.fetchall()

for row in results:

print(row)

Combine data with UNION ALL

mycursor.execute("SELECT FROM students_table1 UNION ALL SELECT FROM students_table2")

Fetch all rows

results = mycursor.fetchall()

for row in results:

print(row)


### Example - Combining data with different columns

Let's create two tables (`employees_table1` and `employees_table2`) with the same columns (`id`, `name`, `position`, and `salary`) but different data. We want to create a single table that contains all employees from both tables:

import mysql.connector

mydb = mysql.connector.connect(

host="localhost",

user="yourusername",

password="yourpassword"

)

mycursor = mydb.cursor()

Create table1

mycursor.execute("CREATE TABLE IF NOT EXISTS employees_table1 (id INT, name VARCHAR(25), position VARCHAR(20), salary FLOAT)")

Insert data into table1

mycursor.execute("INSERT INTO employees_table1 VALUES (1, 'Alice', 'Manager', 60000), (2, 'Bob', 'Developer', 75000), (3, 'Charlie', 'Designer', 80000)")

mydb.commit()

Create table2

mycursor.execute("CREATE TABLE IF NOT EXISTS employees_table2 (employee_id INT, employee_name VARCHAR(25), employee_position VARCHAR(20), employee_salary FLOAT)")

Insert data into table2

mycursor.execute("INSERT INTO employees_table2 VALUES (4, 'David', 'HR', 65000), (5, 'Eve', 'Marketing', 70000), (6, 'Frank', 'Developer', 85000)")

mydb.commit()


Now let's combine the data from both tables using UNION and UNION ALL:

Combine data with UNION

mycursor.execute("SELECT id AS employee_id, name, position, salary FROM employees_table1 UNION SELECT employee_id, employee_name, employee_position, employee_salary FROM employees_table2")

Fetch all rows

results = mycursor.fetchall()

for row in results:

print(row)

Combine data with UNION ALL

mycursor.execute("SELECT id AS employee_id, name, position, salary FROM employees_table1 UNION ALL SELECT employee_id, employee_name, employee_position, employee_salary FROM employees_table2")

Fetch all rows

results = mycursor.fetchall()

for row in results:

print(row)

Worked Example

Let's say we have two tables (employees_table1 and employees_table2) with the same columns (id, name, position, and salary) but different data. We want to create a single table that contains all employees from both tables, sorted by name:

import mysql.connector

mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword"
)

mycursor = mydb.cursor()

Create table1

mycursor.execute("CREATE TABLE IF NOT EXISTS employees_table1 (id INT, name VARCHAR(25), position VARCHAR(20), salary FLOAT)")

Insert data into table1

mycursor.execute("INSERT INTO employees_table1 VALUES (1, 'Alice', 'Manager', 60000), (2, 'Bob', 'Developer', 75000), (3, 'Charlie', 'Designer', 80000)")

mydb.commit()

Create table2

mycursor.execute("CREATE TABLE IF NOT EXISTS employees_table2 (id INT, name VARCHAR(25), position VARCHAR(20), salary FLOAT)")

Insert data into table2

mycursor.execute("INSERT INTO employees_table2 VALUES (4, 'David', 'HR', 65000), (5, 'Eve', 'Marketing', 70000), (6, 'Frank', 'Developer', 85000)")

mydb.commit()


Now let's combine the data from both tables using UNION and UNION ALL, sort the result-set by name, and fetch only employees with a salary greater than 70000:

Combine data with UNION

mycursor.execute("SELECT * FROM (SELECT id AS employee_id, name, position, salary FROM employees_table1 UNION SELECT id, name, position, salary FROM employees_table2) tmp ORDER BY name")

mycursor.execute("WHERE salary > 70000")

Fetch all rows

results = mycursor.fetchall()

for row in results:

print(row)

Common Mistakes

1. Incorrect column order

Ensure that the columns in each SELECT statement are ordered identically for the UNION operator to work correctly. If the columns are not in the same order, you'll encounter an error or unexpected results.

2. Mismatched data types

The columns in both SELECT statements must have the same data type (integer, string, float, etc.). Otherwise, you may encounter errors or unexpected results when combining the data.

3. Forgetting to use parentheses

Always enclose each SELECT statement within parentheses when using the UNION operator. This is essential for correctly grouping the statements and avoiding syntax errors.

Practice Questions

  1. Write a Python script that combines data from two tables (students_table1 and students_table2) with different columns (name, age, city, and grade) using UNION and UNION ALL.
  1. Given the following tables (employees_table1 and employees_table2), write a Python script that combines the data using UNION and UNION ALL, sorts the result-set by name, and fetches only employees with a salary greater than 70000:
import mysql.connector

mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword"
)

mycursor = mydb.cursor()

Create table1

mycursor.execute("CREATE TABLE IF NOT EXISTS employees_table1 (id INT, name VARCHAR(25), position VARCHAR(20), salary FLOAT)")

Insert data into table1

mycursor.execute("INSERT INTO employees_table1 VALUES (1, 'Alice', 'Manager', 60000), (2, 'Bob', 'Developer', 75000), (3, 'Charlie', 'Designer', 80000)")

mydb.commit()

Create table2

mycursor.execute("CREATE TABLE IF NOT EXISTS employees_table2 (id INT, name VARCHAR(25), position VARCHAR(20), salary FLOAT)")

Insert data into table2

mycursor.execute("INSERT INTO employees_table2 VALUES (4, 'David', 'HR', 65000), (5, 'Eve', 'Marketing', 70000), (6, 'Frank', 'Developer', 85000)")

mydb.commit()

FAQ

  1. Can I use the UNION operator with multiple tables at once?

Yes! You can combine data from multiple tables using multiple UNION statements, as long as the columns have the same order and data type in each SELECT statement.

  1. What happens if there are duplicate rows when using UNION without ALL?

When using UNION without ALL, duplicate rows will be eliminated, and only one row per duplicate group will be included in the final result-set.

  1. Can I use ORDER BY with UNION or UNION ALL?

Yes! You can use ORDER BY to sort the combined result-set by one or more columns. However, keep in mind that the ORDER BY clause must be applied after all UNION and UNION ALL statements have been executed.

  1. Is there a way to concatenate columns using UNION or UNION ALL?

Yes! You can concatenate columns by using a combination of the CONCAT() function and the UNION operator. For example:

SELECT CONCAT(name, ' ', position) AS combined_info FROM employees_table1
UNION
SELECT CONCAT(name, ' ', position) AS combined_info FROM employees_table2;
MySQL UNION (Python Programming) | Python | XQA Learn