UNION
UNION is an SQL operation that combines rows from two or more SELECT statements into a single result. It removes duplicate records and requires both SELECT statement queries to have the same number of columns with similar data types. The columns in each SELECT statement must also be in the same order.
Example
Output
Explanation
The UNION operator is used to combine the result-set of two or more SELECT statements. Each SELECT statement within the UNION must have the same number of columns, the columns must also have similar data types, and the columns in each SELECT statement must also be in the same order. In this example, we’re selecting country
from two different tables: Suppliers
and Customers
. The resulting output is a list of countries from both tables, without any duplicate entries.
Example
Output
Explanation
The UNION operator is used to combine rows from two or more SELECT statements. In the given example, two SELECT statements are combined to create one single set of results. The first SELECT statement yields ‘John Doe’ as the Employee Name and ‘Development’ as the Department. The second statement yields ‘Jane Smith’ as the Employee Name and ‘Marketing’ as the Department. The UNION between them outputs two rows, one for each SELECT statement.
Example
Output
Explanation
The UNION operator is used to combine the result-set of two or more SELECT statements. This SQL code is connecting data from two tables: “Customers” and “Suppliers”. It is returning a list of cities and countries from both tables, but only the distinct (meaning no duplicates) values. The result is ordered by city name.
Example
Output
Explanation
UNION is used to combine the result-set of two or more SELECT statements. Each SELECT statement within the UNION must have the same number of columns. The columns must also have similar data types. Also, the columns in each SELECT statement must be ordered in the same manner. The UNION operator selects only distinct values by default.
Example
Output
Explanation
UNION
is used to combine the result set of two or more SELECT
statements. It removes duplicate rows from the results. In the example, cities from Customers and Suppliers tables are combined and duplicates are removed. The result is a list of unique cities from both tables, sorted in alphabetical order.