The clause combines UNION data in SQL

In SQL, you can combine the same structured data from multiple tables into one query using the UNION and UNION ALL operators. These tables are sometimes in the same database, sometimes in different databases.

  1. UNION combines but removes the same.
  2. UNION ALL combines but does not remove the same.

In this article, Quantum will guide in detail how to use the UNION clause in SQL with syntax and specific examples to make it easier to visualize and capture statements.

UNION in SQL

The UNION clause / SQL operator is used to combine the results of two or more SELECT statements without returning any duplicate records.

To use UNION, each SELECT statement must have the same number of columns, the same number of expressions of columns, the same data type, and the corresponding column must be in the correct order but not the same length.

UNION syntax in SQL

The basic syntax of the UNION clause is as follows:

 SELECT cot1 [, cot2 ] 
FROM bang1 [, bang2 ]
[WHERE dieu_kien]

UNION

SELECT cot1 [, cot2 ]
FROM bang1 [, bang2 ]
[WHERE dieu_kien]

Here, given dieu_kien can be any expression based on your requirements.

Example of UNION in SQL

Suppose the two tables are NHANVIEN and TIENTHUONG with the following records:

Table 1: NHANVIEN

 +----+----------+-----+-----------+----------+ | ID | TEN |TUOI | DIACHI | LUONG | +----+----------+-----+-----------+----------+ | 1 | Thanh | 32 | Haiphong | 2000.00 | | 2 | Loan | 25 | Hanoi | 1500.00 | | 3 | Nga | 23 | Hanam | 2000.00 | | 4 | Manh | 25 | Hue | 6500.00 | | 5 | Huy | 27 | Hatinh | 8500.00 | | 6 | Cao | 22 | HCM | 4500.00 | | 7 | Lam | 24 | Hanoi | 10000.00 | +----+----------+-----+-----------+----------+

Table 2: TIENTHUONG

 +-----+---------------------+-------------+--------+ |TT_ID| NGAY | NHANVIEN_ID | SOTIEN | +-----+---------------------+-------------+--------+ | 102 | 2019-01-08 00:00:00 | 3 | 3000 | | 100 | 2019-01-08 00:00:00 | 3 | 1500 | | 101 | 2019-02-20 00:00:00 | 2 | 1560 | | 103 | 2018-12-20 00:00:00 | 4 | 2060 | +-----+---------------------+-------------+--------+ 

Now we combine these two tables in the SELECT statement as follows:

 SQL> SELECT ID, TEN, SOTIEN, NGAY 
FROM NHANVIEN
LEFT JOIN TIENTHUONG
ON NHANVIEN.ID = TIENTHUONG.NHANVIEN_ID
UNION
SELECT ID, TEN, SOTIEN, NGAY
FROM NHANVIEN
RIGHT JOIN TIENTHUONG
ON NHANVIEN.ID = TIENTHUONG.NHANVIEN_ID;

The result is:

 +------+----------+--------+---------------------+ | ID | TEN | SOTIEN | NGAY | +------+----------+--------+---------------------+ | 1 | Thanh | NULL | NULL | | 2 | Loan | 1560 | 2019-02-20 00:00:00 | | 3 | Nga | 3000 | 2019-01-08 00:00:00 | | 3 | Nga | 1500 | 2019-01-08 00:00:00 | | 4 | Manh | 2060 | 2018-12-20 00:00:00 | | 5 | Huy | NULL | NULL | | 6 | Cao | NULL | NULL | | 7 | Lam | NULL | NULL | +------+----------+--------+---------------------+ 

UNION ALL in SQL

The clause / UNION ALL operator in SQL is used to combine the results of two or more SELECT statements including duplicate records.

The rules applicable to UNION also apply to the UNION ALL operator.

The UNION ALL syntax in SQL

The basic syntax of UNION ALL clause is as follows:

 SELECT cot1 [, cot2 ] 
FROM bang1 [, bang2 ]
[WHERE dieu_kien]

UNION ALL

SELECT cot1 [, cot2 ]
FROM bang1 [, bang2 ]
[WHERE dieu_kien]

Here, given dieu_kien can be any expression based on your requirements.

Example of UNION ALL in SQL

Suppose the two tables are NHANVIEN and TIENTHUONG with the following records:

Table 1: NHANVIEN

 +----+----------+-----+-----------+----------+ | ID | TEN |TUOI | DIACHI | LUONG | +----+----------+-----+-----------+----------+ | 1 | Thanh | 32 | Haiphong | 2000.00 | | 2 | Loan | 25 | Hanoi | 1500.00 | | 3 | Nga | 23 | Hanam | 2000.00 | | 4 | Manh | 25 | Hue | 6500.00 | | 5 | Huy | 27 | Hatinh | 8500.00 | | 6 | Cao | 22 | HCM | 4500.00 | | 7 | Lam | 24 | Hanoi | 10000.00 | +----+----------+-----+-----------+----------+

Table 2: TIENTHUONG

The clause combines UNION data in SQL Picture 1

Now we combine these two tables in the SELECT statement as follows:

 SQL> SELECT ID, TEN, SOTIEN, NOW 
FROM NHANVIEN
LEFT JOIN TIENTHUONG
ON NHANVIEN.ID = TIENTHUONG.NHANVIEN_ID
UNION ALL
SELECT ID, TEN, SOTIEN, NOW
FROM NHANVIEN
RIGHT JOIN TIENTHUONG
ON NHANVIEN.ID = TIENTHUONG.NHANVIEN_ID;

The result is:

 +------+----------+--------+---------------------+ | ID | TEN | SOTIEN | NGAY | +------+----------+--------+---------------------+ | 1 | Thanh | NULL | NULL | | 2 | Loan | 1560 | 2019-02-20 00:00:00 | | 3 | Nga | 3000 | 2019-01-08 00:00:00 | | 3 | Nga | 1500 | 2019-01-08 00:00:00 | | 4 | Manh | 2060 | 2018-12-20 00:00:00 | | 5 | Huy | NULL | NULL | | 6 | Cao | NULL | NULL | | 7 | Lam | NULL | NULL | | 3 | Nga | 3000 | 2019-01-08 00:00:00 | | 3 | Nga | 1500 | 2019-01-08 00:00:00 | | 2 | Loan | 1560 | 2019-02-20 00:00:00 | | 4 | Manh | 2060 | 2018-12-20 00:00:00 | +------+----------+--------+---------------------+ 

In addition, there are two other clauses / operators similar to the UNION clause:

  1. The INTERSECT clause in SQL : is used to combine two SELECT statements, but returns rows only from the first SELECT statement that is the same as the one in the second SELECT statement.
  2. The EXCEPT clause in SQL : is used to combine two SELECT statements and return rows from the first SELECT statement that are not returned by the second SELECT statement.

In the next articles, Quantrimang will discuss with you the NULL value in SQL. Have you remembered to it!

Previous lesson: The JOIN data clause in SQL

Next lesson: NULL value in SQL

4 ★ | 1 Vote

May be interested

  • HAVING clause in SQLHAVING clause in SQL
    the having clause in sql is used to filter records and retrieve only those records that match the requirements or are actually needed.
  • UNION operator in SQL ServerUNION operator in SQL Server
    this tutorial explains how to use the union operator in sql server with specific syntax and examples.
  • What is Western Union?What is Western Union?
    in the following article we will learn about western union international money transfer service, because this is a prestigious service, quickly used by many people to send money to family. let's find out through this service.
  • DISTINCT clause in SQL ServerDISTINCT clause in SQL Server
    the distinct clause is used to remove duplicates in the result set.
  • Types of money in Mobile UnionTypes of money in Mobile Union
    in the mobile coalition there will be different types of money to buy items, especially to buy generals and skins.
  • HAVING clause in SQL ServerHAVING clause in SQL Server
    the having clause is used in conjunction with the group by clause in sql server (transact-sql) to limit the group of returned rows, only when the condition is met is true.
  • Interesting Facts About The Santa ClauseInteresting Facts About The Santa Clause
    the santa clause is one of the best christmas movies of all time. however, behind this christmas movie are surprising, little-known facts about the making process.
  • PIVOT clause in SQL ServerPIVOT clause in SQL Server
    in sql server (transact-sql), the pivot clause allows cross tabulation to pass data from one table to another.
  • How to Watch the State of the Union AddressHow to Watch the State of the Union Address
    the state of the union address is a speech given every january by the president of the united states, delivered to congress and--thanks to technology--the entire world. first given by george washington in 1790, the state of the union...
  • Top 10 most powerful generals in Mobile UnionTop 10 most powerful generals in Mobile Union
    top 10 most powerful generals in mobile union. lien quan mobile got such attraction thanks to the game with quite a variety of generals, each type of hero has a very unique and beautiful skill set. in this article, tipsmake.com will introduce to you the top 10