UNION operator in SQL Server
The UNION operator is used to combine 2 result sets from 2 or more SELECT statements. It will delete the same rows in these SELECT statements.
Each SELECT in the UNION operator must have the same number of columns in the result set with the corresponding data type.
The UNION operator syntax
SELECT bieu_thuc1, bieu_thuc2, … bieu_thucn
FROM bang
[WHERE dieu_kien]
UNION
SELECT bieu_thuc1, bieu_thuc2, … bieu_thucn
FROM bang
[WHERE
dieu_kien];
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 clears duplicate rows.
See more UNION ALL operators.
For example - return an information field
For example, the UNION operator returns a field from multiple SELECT statements (and fields with the same data type).
SELECT sanpham_id
FROM sanpham
UNION
SELECT sanpham_id
FROM hangtonkho;
In the above example, if any sanpham_id appears in both the sanpham and hangtonkho tables, only 1 sanpham_id will appear in the result set. If you do not want to remove the duplicate record, use the UNION ALL operator.
For example - use ORDER BY
The UNION operator can use the ORDER BY clause to sort query results.
SELECT danhba_id, danhba_ten
FROM danhba
WHERE ten_trang = 'QuanTriMang.com'
UNION
SELECT congty_id, congty_ten
FROM congty
WHERE ten_trang = 'TrangCuaBan.com'
ORDER B
Y 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: DELETE TOP command in SQL Server
The following article: UNION ALL operator in SQL Server
You should read it
- Conditions NOT in SQL Server
- The difference between web server and app server
- WHERE clause in SQL Server
- INTERSECT operator in SQL Server
- Network basics: Part 3 - DNS Server
- Combine AND and OR conditions in SQL Server
- New points in SQL Server 2017
- SQL Server 2019 - Microsoft Relational Database Management System
May be interested
- Load operator ++ and - in C ++the incremental operators (++) and the reduced operator (-) are the two important unary operators available in c ++.
- Mobile Union servers give generals for freesome mobile coalition servers often have free generals, up to 30 generals. players can refer to play.
- NULL value in SQLnull in sql is the term used to represent a missing value.
- Overload subscript operator [] in C ++the subscript operator [] in c ++ is often used to access array elements. this operator can be overloaded to enhance existing functionality in arrays in c ++.
- AND conditions in SQL Serverin sql server, the and condition (or and operator) is used to test two or more conditions.
- 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.
- Types of money in Mobile Unionin the mobile coalition there will be different types of money to buy items, especially to buy generals and skins.
- Operator overload and Load overlap in C ++c ++ allows you to define more than one definition for a function name or an operator in the scope, respectively called load overloading (function overloading) and load operator overloading in c ++. .
- Load the Input / Output operator stack in C ++c ++ is able to input and output existing data types by using thread extraction operators >> and thread insertion operators
- Operator overloading in C #overloading operator is operator overload. you can redefine or overload most existing operators in c #.