UNION ALL operator in SQL Server
The UNION ALL operator is used to combine the result set from 2 or more SELECT statements in SQL Server.
Unlike the UNION operator, the UNION ALL operator returns all rows from the query and does not delete duplicate rows.
Each SELECT in the UNION operator must have the same number of columns in the result set with the corresponding data type.
UNION ALL operator syntax
SELECTbieu_thuc1, bieu_thuc2, … bieu_thucn
FROM bang
[WHERE dieu_kien]
UNION ALL
SELECT bieu_thuc1, bieu_thuc2, … bieu_thucn
FROM bang
[WHERE dieu_k
ien];
Variable name or variable value
bieu_thuc1, bieu_thuc2, . bieu_thucn
The column or calculation value you want to retrieve.
state
Table wants to get the record. Must have at least 1 table in the FROM clause.
WHERE dieu_kien
Option. Conditions must satisfy for the selected record.
Note:
- 2 SELECT statements must have the same number of expressions
- The corresponding number of columns in each SELECT statement must have the same data type
- UNION operator does not delete duplicate rows.
- See more UNION operators
For example - return an information field
SELECT sanpham_id
FROM sanpham
UNION ALL
SELECT sanpham_id
FROM
hangtonkho;
This example returns many sanpham_id in the result set if they appear in both the sanpham and hangtonkho tables. If you want to delete duplicates, use the UNION whole element.
For example - use ORDER BY
The UNION ALL operator can use the ORDER BY clause to sort query results.
SELECT danhba_id, danhba_ten
FROM danhba
WHERE ten_trang = 'QuanTriMang.com'
UNION ALL
SELECT congty_id, congty_ten
FROM congty
WHERE ten_trang = 'TrangCuaBan.com'
ORDER BY 2;
In this example, because the column name in the two SELECT statements is different, it is easier to refer to the column in the ORDER BY statement by position in the result set. In the above example, we filter the results by namba_ten / congty_ten in ascending order, as stated by the phrase ORDER BY 2.
danhba_ten / congty_ten is in the second position in the result set.
Previous article: UNION operator in SQL Server
The following article: INTERSECT operator in SQL Server
You should read it
- The difference between web server and app server
- Combine AND and OR conditions in SQL Server
- AND conditions in SQL Server
- OR conditions in SQL Server
- Network basics: Part 3 - DNS Server
- SQL Server 2019 - Microsoft Relational Database Management System
- INTERSECT operator in SQL Server
- WHERE clause in SQL Server
May be interested
- How to decentralize users in MS SQL Serveradministrators can create, delete or deny permissions in ms sql server. the permissions here refer to database access (csdl). you can create, delete or deny permissions in ms sql server.
- OR conditions in SQL Serverthe or condition in sql server (transact-sql) is used to check multiple conditions.
- Combine AND and OR conditions in SQL Serverthe article explains how to use and conditions and or conditions in sql server (transact-sql).
- DISTINCT clause in SQL Serverthe distinct clause is used to remove duplicates in the result set.
- Introduction to SQL Server Reporting Servicessql server 2005, 2008 and 2008 r2 software packages are pre-packaged with sql server reporting services (ssrs) - a specialized report creation solution for businesses. with ssrs, you can completely create, publish, and manage a huge number of reports from various data sources ...
- Instructions for creating and editing reports in SQL Server Reporting Servicessql server 2005, 2008 and 2008 r2 product suite, all packaged with sql server reporting services (ssrs) - a dedicated solution for creating reports for businesses. with ssrs, users can create, schedule, post and manage various reports from various sources of original data.