0% found this document useful (0 votes)
12 views

SQL UNION Operator

Uploaded by

Anum Masood
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

SQL UNION Operator

Uploaded by

Anum Masood
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

SQL
1. SQL COMMANDS
COMMANDS FOR
FOR BEGINNERS
BEGINNERS

2. FULL
2. FULL JOIN
JOIN IN
IN SQL
SQL

SQL UNION
Operator
‹ Previous Next ›

The SQL UNION Operator


The UNION operator is used to combine the
result-set of two or more SELECT statements.

Every SELECT statement within UNION


must have the same number of columns
The columns must also have similar data
types
The columns in every SELECT statement
must also be in the same order

UNION Syntax

SELECT column_name(s) FROM table1


UNION
SELECT column_name(s) FROM table2;

UNION ALL Syntax


The UNION operator selects only distinct values
The UNION operator selects only distinct values
by default. To allow duplicate values, use UNION
ALL :

SELECT column_name(s) FROM table1


UNION ALL
SELECT column_name(s) FROM table2;

Note: The column names in the result-set are


usually equal to the column names in the first
SELECT statement.

Demo Database
In this tutorial we will use the well-known
Northwind sample database.

Below is a selection from the "Customers" table:

CustomerID CustomerName ContactName

1 Alfreds Maria Anders


Futterkiste

2 Ana Trujillo Ana Trujillo


Emparedados y
helados

3 Antonio Moreno Antonio


Taquería Moreno

And a selection from the "Suppliers" table:

SupplierID SupplierName ContactName Addres

1 Exotic Liquid Charlotte 49


1 Exotic Liquid Charlotte 49
Cooper Gilbert
St.

2 New Orleans Shelley Burke P.O. Box


Cajun Delights 78934

3 Grandma Regina Murphy 707


Kelly's Oxford
Homestead Rd.

Search for

1. Full Join in SQL ›› 1.

2. SQL Commands List ›› 2.

3. SQL for Beginners ›› 3.

SQL UNION Example


The following SQL statement returns the cities
 Tutorials  Exercises  Certificates  Services  Search...
(only distinct values) from both the "Customers"
and the "Suppliers" table:
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C

SQL Tutorial
Example Get your own SQL Server
SQL HOME
SQL Intro SELECT City FROM Customers
SQL Syntax UNION
SQL Select SELECT City FROM Suppliers
SQL Select Distinct ORDER BY City;
SQL Where
SQL Order By Try it Yourself »
SQL And
SQL Or
SQL Not Note: If some customers or suppliers have the
SQL Insert Into same city, each city will only be listed once,
SQL Null Values because UNION selects only distinct values. Use
SQL Update UNION ALL to also select duplicate values!
SQL Delete
SQL Select Top
SQL Aggregate Functions SQL UNION ALL Example
SQL Min and Max
SQL Count The following SQL statement returns the cities
SQL Sum (duplicate values also) from both the "Customers"
SQL Avg and the "Suppliers" table:
SQL Like
SQL Wildcards
SQL In Example
SQL Between SELECT City FROM Customers
SQL Aliases UNION ALL
SQL Joins SELECT City FROM Suppliers
SQL Inner Join ORDER BY City;
SQL Left Join
SQL Right Join Try it Yourself »
SQL Full Join
SQL Self Join
SQL Union
SQL Group By
SQL Having SQL UNION With WHERE
SQL Exists
The following SQL statement returns the German
SQL Any, All
cities (only distinct values) from both the
SQL Select Into
"Customers" and the "Suppliers" table:
SQL Insert Into Select
SQL Case
SQL Null Functions
Example
SQL Stored Procedures
SQL Comments
SELECT City, Country FROM Customers
SQL Operators WHERE Country='Germany'
UNION
SQL Database SELECT City, Country FROM Suppliers
SQL Create DB WHERE Country='Germany'
ORDER BY City;
SQL Drop DB
SQL Drop DB
SQL Backup DB
SQL Create Table Try it Yourself »
SQL Drop Table
SQL Alter Table
SQL Constraints
SQL Not Null
SQL Unique
SQL UNION ALL With
SQL Primary Key
SQL Foreign Key
WHERE
SQL Check The following SQL statement returns the German
SQL Default cities (duplicate values also) from both the
SQL Index "Customers" and the "Suppliers" table:
SQL Auto Increment
SQL Dates
SQL Views Example
SQL Injection
SQL Hosting SELECT City, Country FROM Customers
SQL Data Types WHERE Country='Germany'
UNION ALL
SELECT City, Country FROM Suppliers
SQL References
SQL Keywords  WHERE Country='Germany'
MySQL Functions ORDER BY City;

SQL Server Functions 
MS Access Functions  Try it Yourself »
SQL Quick Ref

SQL Examples
SQL Examples
SQL Editor
Another UNION Example
SQL Quiz
The following SQL statement lists all customers
SQL Exercises
and suppliers:
SQL Server
SQL Bootcamp
SQL Certificate
Example

SELECT 'Customer' AS Type, ContactName,


City, Country
FROM Customers
UNION

You might also like