WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

SQL UNION Operator


The SQL UNION administrator consolidates the consequence of at least two SELECT statements.


The SQL UNION Operator

The UNION administrator is utilized to consolidate the outcome set of at least two SELECT statements.

Notice that each SELECT explanation inside the UNION must have a similar number of segments. The sections should likewise have comparative information types. Additionally, the sections in each SELECT proclamation must be in the equivalent order.

SQL UNION Syntax

SELECT column_name(s) FROM demo1
UNION
SELECT column_name(s) FROM demo2;

SQL UNION ALL Syntax

SELECT column_name(s) FROM table1
Association ALL
SELECT column_name(s) FROM table2;

Database

In this instructional exercise we will utilize the notable database.

Below is a choice from the "Clients" table:

CustomerID CustomerName UserName Address City PostalCode Country
1

Andre C. Holloway Thiletwed52 Giraffe Hill Drive Frisco TX 75034 Usa
2 Melissa Araujo Cavalcanti Wriand Pointe Lane Miami FL 33179 USA
3 Victor Goncalves Correia Antonio Moreno regret Grande Fusterie BRON 69500 France

And a choice from the "Wholesaler" table:

SupplierID DistributorName UserName Address City PostalCode Country
1 Guttormur Arnlaugsson Mithency Tværgyden Gsa 8765 Danmark
2 Haddur Sævarsson Arde1973 Windfall Grove Pinehill North Shore 0632 New zealand
3 Hildigunn Jensdóttir Regina Murphy Conaway Street Ferdinand IN 47532 USA

SQL UNION Example

The following SQL explanation chooses all the different urban communities (just unmistakable qualities) from the "Clients" and the "Merchant" tables:

Example

SELECT City FROM Customers
UNION
SELECT City FROM Distributor
ORDER BY City;

SQL UNION ALL Example

The following SQL explanation utilizes UNION ALL to choose all (copy esteems additionally) urban communities from the "Clients" and "Merchant" tables:

Example

SELECT City FROM Customers
UNION ALL
SELECT City FROM Distributor
Request BY City;

SQL UNION ALL With WHERE

The following SQL articulation utilizes UNION ALL to choose all (copy esteems likewise) German urban areas from the "Clients" and "Providers" tables:

Example

SELECT City, Country FROM Customers
WHERE Country='USA'
UNION ALL
SELECT City, Country FROM Distributor
WHERE Country='USA'
ORDER BY City;