RianAcademy
Search
⌘
K
Premium

Combining queries, UNION operator

The results of executing SQL queries can be combined. There is a UNION operator for this.

The general structure of a query with the operator UNION

MySQL 8.4
SELECT table_fields FROM table_name
UNION [ALL]
SELECT table_fields FROM table_name;
  • [ ]optional part
table_fields

The columns each query selects. Both queries must select the same number of columns, and columns in the same position must have compatible types.

table_name
The table each query takes its data from.
ALL

Keeps duplicate rows in the result. Without ALL, duplicates are removed.

KeywordOptional
  • Do not confuse query join operations with table join operations. To do this, use the JOIN operator.
  • Do not confuse query merge operations with subqueries. Subqueries are executed for linked tables.

Table joining with the UNION operator is performed for tables that are not related in any way, but with a similar structure.

In order for UNION to function correctly, it is essential that the resulting tables from each of the SQL queries contain the same number of columns with compatible data types and in the same order.

There are two other operators whose behavior is very similar to UNION:

  • INTERSECT Combines two SELECT queries, but returns only the first SELECT records that have matches in the second SELECT element.
  • EXCEPT Combines two SELECT queries, but returns only the first SELECT records, which do not match in the second SELECT element.

Usage examples

For example, it is necessary to display the name of all goods and the names of all family members (very conditional task). Since the data types are the same, we can do this.

MySQL 8.4
SELECT DISTINCT Goods.good_name AS name FROM Goods
UNION
SELECT DISTINCT FamilyMembers.member_name AS name FROM FamilyMembers;
name
apartment fee
phone fee
bread
milk
red caviar
cinema
black caviar
cough tablets
potato
pineapples
television
vacuum cleaner
jacket
fur coat
music school fee
english school fee
Headley Quincey
Flavia Quincey
Andie Quincey
Lela Quincey
Annie Quincey
Ernest Forrest
Constance Forrest
Wednesday Addams
Tasks for self-testing
0 / 2
1. Uniting students and teachers

Output the first names (field first_name) of all students from the Student table and all teachers from the Teacher table as one list without duplicates.
Class
id
INT
name
VARCHAR
Schedule
id
INT
date
DATE
class
INT
number_pair
INT
teacher
INT
subject
INT
classroom
INT
Student
id
INT
first_name
VARCHAR
middle_name
VARCHAR
last_name
VARCHAR
birthday
DATE
address
VARCHAR
Student_in_class
id
INT
class
INT
student
INT
Subject
id
INT
name
VARCHAR
Teacher
id
INT
first_name
VARCHAR
middle_name
VARCHAR
last_name
VARCHAR
Timepair
id
INT
start_pair
TIME
end_pair
TIME
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Result
Class
Schedule
Student
Student_in_class
Subject
Teacher
Timepair

Run a query to see the result, or open a table above