The UNION operation combines rows from different tables into a single result set, removing duplicate rows. The syntax format is: SELECT column_list FROM table1 UNION SELECT column_list FROM table2..., where table1 and table2 are the tables to be merged. The UNION operation requires that the participating tables have the same number of columns and data types, and that duplicate rows are removed.
UNION operation in SQL
What is the UNION operation?
The UNION operation combines rows from two or more tables into a single result set, thereby removing duplicate rows.
How to use UNION operation?
The syntax format of the UNION operation is:
SELECT column_list FROM table1 UNION SELECT column_list FROM table2 ...
Among them:
-
column_list
Specifies the columns to be retrieved from each table Columns -
table1
,table2
, ... are the tables to be merged
Example
For example, assume the students
table and the teachers
table have the following data:
students | |
---|---|
John | 20 |
Mary | 22 |
Bob | 25 |
teachers | |
---|---|
Alice | 30 |
David | 35 |
Susan | 40 |
Use the UNION operation to merge the two tables:
SELECT name, age FROM students UNION SELECT name, age FROM teachers;
The result will look like this:
name | age |
---|---|
John | 20 |
Mary | 22 |
Bob | 25 |
30 | |
35 | |
40 |
Tables participating in the UNION operation must have the same number of columns and data types.
- UNION operation does not retain duplicate rows. The
- UNION operation can be combined with other SQL operations, such as WHERE
- and
ORDER BY
.
The above is the detailed content of How to use union in sql. For more information, please follow other related articles on the PHP Chinese website!

The article discusses horizontal and vertical data partitioning in SQL, focusing on their impact on performance and scalability. It compares benefits and considerations for choosing between them.

The article explains how to use SQL aggregate functions (SUM, AVG, COUNT, MIN, MAX) to summarize data, detailing their uses and differences, and how to combine them in queries.Character count: 159

The article discusses SQL transaction isolation levels: READ UNCOMMITTED, READ COMMITTED, REPEATABLE READ, and SERIALIZABLE. It examines their impact on data consistency and performance, noting that higher isolation ensures greater consistency but ma

The article discusses security risks of dynamic SQL, focusing on SQL injection, and provides mitigation strategies like using parameterized queries and input validation.

Article discusses using SQL for GDPR and CCPA compliance, focusing on data anonymization, access requests, and automatic deletion of outdated data.(159 characters)

The article discusses the ACID properties (Atomicity, Consistency, Isolation, Durability) in SQL transactions, crucial for maintaining data integrity and reliability.

The article discusses securing SQL databases against vulnerabilities like SQL injection, emphasizing prepared statements, input validation, and regular updates.

Article discusses implementing data partitioning in SQL for better performance and scalability, detailing methods, best practices, and monitoring tools.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 English version
Recommended: Win version, supports code prompts!

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools