Union and Union All in MS SQL Server
Last Updated :
15 Jul, 2025
In MS SQL Server, the UNION and UNION ALL operators are used to combine the result sets of two or more SELECT statements into a single result set. This allows you to retrieve data from multiple tables or views and combine it into a single dataset.
The primary difference between UNION
and UNION ALL
is that UNION
removes duplicate rows from the result set, while UNION ALL
includes all rows, including duplicates.
UNION Operator in MS SQL Server
Union means joining two or more data sets into a single set. In SQL Server, the UNION operator is used to combine two queries into a single result set using the SELECT statements. Union extracts all the rows that are described in the query.
Syntax:
The syntax of the UNION operator in MS SQL Server is:
SELECT column1, column2, ... FROM table1
UNION
SELECT column1, column2, ... FROM table2;
Union holds a few conditions before being used in a query. One such condition is that the rows to be extracted must come from the same columns from the tables.
Example
Let's look at some examples of the UNION operator in MS SQL Server
SELECT name, rollnumber
FROM student
UNION
SELECT name, rollnumber
FROM marks;
Table Student
Name | Rollnumber | Course |
---|
Maya | 111 | CSE |
Riya | 112 | Mech |
Table Marks
Name | Rollnumber | Marks |
---|
Maya | 111 | 8.9 |
Riya | 112 | 7.8 |
Output:
Name | Rollnumber | Name | Rollnumber |
---|
Maya | 111 | Maya | 111 |
Riya | 112 | Riya | 112 |
Two different tables are being used here for extraction of rows yet the column specified for extraction is the same for both. An error occurs if different columns are being used. The data type specified also must be the same for both the queries.
UNION ALL in MS SQL Server
A union is used for extracting rows using the conditions specified in the query while UNION All is used for extracting all the rows from a set of two tables.
Syntax
The syntax to use UNION ALL in MS SQL Server is:
SELECT column1, column2, ...
FROM table1
UNION ALL
SELECT column1, column2, ...
FROM table2;
The same conditions are applicable to Union All. The only difference between Union and Union All is that Union extracts the rows that are being specified in the query while Union All extracts all the rows including the duplicates (repeated values) from both the queries.
Example
Let's look at some examples of the UNION ALL operator in MS SQL Server
SELECT Name, Rollnumber, 'Student' AS Type
FROM Student
UNION ALL
SELECT Name, Rollnumber, 'Marks' AS Type
FROM Marks;
Output:
Name | Rollnumber | Type |
---|
Maya | 111 | Student |
Riya | 112 | Student |
Maya | 111 | Marks |
Riya | 112 | Marks |
As you can see, UNION ALL operator also keeps duplicate values in it's result set.
Similar Reads
UNION vs UNION ALL in SQL SQL UNION and UNION ALL operators are used to concatenate results of multiple SELECT statements. However, they are different from each other. One key difference between UNION and UNION ALL in SQL is that the UNION command removes duplicates from the final results set, whereas the UNION ALL command a
6 min read
SQL Full Outer Join Using Union Clause In this article, we will discuss the overview of SQL, and our main focus will be on how to perform Full Outer Join Using Union Clause in SQL. Let's discuss it one by one. Overview :To manage a relational database, SQL is a Structured Query Language to perform operations like creating, maintaining da
3 min read
Joins in MS SQL Server A database comprises tables and each table in case of RDBMS is called a relation. Let us consider a sample database named University and it has two tables named Student and Marks. If a user wants to transfer a certain set of rows, insert into select statement is used along with the query. But if a u
2 min read
Joins in MS SQL Server A database comprises tables and each table in case of RDBMS is called a relation. Let us consider a sample database named University and it has two tables named Student and Marks. If a user wants to transfer a certain set of rows, insert into select statement is used along with the query. But if a u
2 min read
Combine Rows into String in SQL Server To combine rows into a string in SQL Server, use the SQL COALESCE() function or the SQL CONCAT() function. COALESCE() function in SQL is used to handle null values. It returns non-null values from a row, which can be concatenated into string. CONCAT() function in SQL is used to concatenate two or mo
2 min read
SQL | Union Clause The UNION clause in SQL is a powerful tool used to combine the results of two or more SELECT statements into a single result set. It ensures that duplicate rows are removed by default, providing only distinct values. To include duplicate values as well, the UNION ALL clause is used. The UNION clause
4 min read