In SQL, set operations are used to combine two or more result sets into a single result set. One of the most commonly used set operators is the UNION operator. The UNION operator is used to combine the result sets of two or more SELECT statements into a single result set. The result set of a UNION operation contains only unique values from the input result sets.
The syntax for the UNION operator is as follows:
SELECT column1, column2, ...
FROM table1
UNION
SELECT column1, column2, ...
FROM table2;
Here, the SELECT statement retrieves the specified columns from the specified tables. The UNION operator is used to combine the result sets of the two SELECT statements.
Let’s look at an example to see how the UNION operator works in practice. Suppose we have two tables: employees
and customers
. We want to retrieve the names of all employees and customers. We can do this using the UNION operator as follows:
SELECT name, 'Employee' as source
FROM employees
UNION
SELECT name, 'Customer' as source
FROM customers;
In this example, we use the UNION operator to combine the results of two SELECT statements. The first SELECT statement retrieves the name
column from the employees
table and adds a column source
with a value of ‘Employee’ for each row. The second SELECT statement retrieves the name
column from the customers
table and adds a column source
with a value of ‘Customer’ for each row. The UNION operator combines the two result sets and removes any duplicate rows. The resulting output will be a single result set containing the names of all employees and customers with a column indicating their source.
We can also use the UNION operator to combine the result sets of more than two SELECT statements. For example:
SELECT name, 'Employee' as source
FROM employees
UNION
SELECT name, 'Customer' as source
FROM customers
UNION
SELECT name, 'Vendor' as source
FROM vendors;
In this example, we use the UNION operator to combine the results of three SELECT statements. The first SELECT statement retrieves the name
column from the employees
table and adds a column source
with a value of ‘Employee’ for each row. The second SELECT statement retrieves the name
column from the customers
table and adds a column source
with a value of ‘Customer’ for each row. The third SELECT statement retrieves the name
column from the vendors
table and adds a column source
with a value of ‘Vendor’ for each row. The UNION operator combines the three result sets and removes any duplicate rows. The resulting output will be a single result set containing the names of all employees, customers, and vendors with a column indicating their source.
In summary, the UNION operator is used to combine the result sets of two or more SELECT statements into a single result set. The result set of a UNION operation contains only unique values from the input result sets. The syntax for the UNION operator is straightforward, and it is widely used in SQL for various data retrieval scenarios.
Also check WHAT IS GIT ? It’s Easy If You Do It Smart
You can also visite the Git website (https://git-scm.com/)