Union And Union All In Sql Server With Examples

Union And Union All In Sql Server With Examples

In today’s age of lesser resources and an increased emphasis on efficiency and scaling up all operations, more and more companies are looking at implementing data warehousing (DW) tools to marry the best of OLTP with the best of OLAP.  After a few years of dealing with the ever-increasing strain of resource-crunched systems and applications, now is a good time to go back to the drawing board.  But planning for a successful implementation requires proper understanding of how each design element interacts with the others — and where they can be employed most effectively.  Hence, this paper will cover some important points that need to be kept in mind while going about designing a DW system.

UNION vs. UNION ALL in SQL Server

Union And Union All In Sql Server With Examples

In this article, I am going to discuss SQL Union and UNION ALL with examples.

Union is used to combine the result set of two or more queries into one single result set. It is also known as UNION DISTINCT operator in SQL Server. Union works on the basis of combining the rows with common columns from different tables (it does not have any concept of duplicate rows).

Let’s take an example, where we have two tables namely Employee_Table & Department_Table. These two tables have some common columns like Name, Designation etc. Here we are going to use union operator to combine the result set of these two tables into one single result table.

SQL UNION, UNION ALL – SQL JOINS

In this tutorial, you’ll learn all about SQL union and union all, including syntax and examples.

SQL UNION Syntax:

The syntax for the SQL union clause is:

SELECT column-name(s) FROM table-name1 UNION SELECT column-name(s) FROM table-name2

SQL UNION is a SQL statement which is used to combine rows from two or more tables. The UNION operator is used to combine rows from two or more tables and it returns only distinct values. But if you want to return all the rows, then use UNION ALL instead of UNION.

UNION SELECT statement can be used to select the duplicate values from the two different columns or multiple columns. The UNION SELECT query will return all the duplicate values from both the columns.

SQL UNION is a SQL command that can be used to combine the result of two or more SELECT statements. In this SQL tutorial, we will learn how to use the UNION and UNION ALL keywords with examples.

Before we start learning about UNION, let us first understand what is a JOIN clause?

Join Clause in SQL

A join clause combines records from two or more tables based on a common column. It is also referred as Cartesian product.

A join clause can be written in two ways:

* INNER JOIN (non-equi)

* LEFT OUTER JOIN (non-equi)

The following example shows how to use SQL UNION operator with different types of data source like Table, View, Stored Procedure, etc.

UNION Operator Syntax:

UNION [ALL] ([DISTINCT | ALL] select_statement) [WHERE condition];

SQL Server UNION ALL example

In this example, we will use the UNION ALL keyword to combine multiple resultsets into one resultset.

UNION ALL will return all rows from both tables, even if they have duplicate data.

The syntax for a UNION statement is as follows:

SELECT column_name1,column_name2 FROM table_name UNION SELECT column_name3,column_name4 FROM table_name;

SQL UNION is used to combine the result of two or more SELECT statements into a single result set. The UNION operator combines the results of two SELECT statements with the same number and type of columns. It returns the distinct values from both queries.

The UNION operator does not have any effect on the data returned by a query. It simply combines two SELECT statements into one result set.

The syntax for UNION is:

SELECT column_name1, column_name2, … FROM table1 [CROSS JOIN table2] [WHERE condition];

Get Ready to Learn SQL Server: 15. Learn to use Union, Intersect, and Except Clauses - CodeProject

UNION ALL has all rows returned by each query in its own distinct set, whereas UNION has all rows with duplicates removed in its own distinct set.

SQL Union Example

Let’s take an example to understand the concept of SQL Union. In this example, we have two tables Employee and Department. The Employee table has the following columns:

In the Department table, we have the following columns:

EmployeeID, FirstName, LastName and DepartmentID.

Now let’s say we want to get all rows from both the tables where EmployeeID is equal to some value (say 1). In this case, we can use the following query:

SELECT * FROM Employee UNION SELECT * FROM Department WHERE EmployeeID = 1.

SQL UNION is used to combine the results of two or more SELECT statements. SQL UNION all will return a result set that is the combination of two or more SELECT statements with identical column names and compatible datatypes. If your SQL Server database has multiple tables, then you have to use the SQL JOIN keyword to combine their records into one result set.

SQL UNION ALL can be used in combination with an ON clause, which means that only rows that satisfy both queries will be included in the final result set. Also, if your SQL query contains multiple UNION ALL keywords, then they must be separated by commas.

SQL Union is a SQL operator that combines the rows of two or more result sets.

Union works on the same column in all tables. If there are no common columns, union returns an error. You can use UNION ALL to include all columns from the select statement in the results, even if they have duplicate values.

In this article, I will explain how to use Union and Union All operators in SQL Server with examples.

SQL UNION Example – Union Multiple Tables

The following example explains how to use UNION operator in SQL Server to combine two tables.

SELECT A.CustomerID, B.City FROM dbo.Customers AS A INNER JOIN dbo.BillingDetails AS B ON A.CustomerID = B.CustomerID UNION ALL SELECT A2.* FROM dbo.Customers AS A2 INNER JOIN dbo.BillingDetails AS B2 ON A2.CustomerID = B2.CustomerID;

The below SQL UNION example returns all the rows from the Employee table where the salary is greater than 2000:

In this SQL UNION example, we have used the EXISTS clause to check for the existence of a value in one of the columns. The result would be True if any one of them evaluates to true.

SELECT * FROM Employee WHERE Name LIKE ‘%John’ OR Salary > 2000;

Leave a Reply

Your email address will not be published. Required fields are marked *