0% found this document useful (0 votes)
10 views8 pages

SQL UNION Operator Explained

The SQL UNION operator combines the result sets of two or more SELECT statements, requiring the same number of columns with similar data types and order. The UNION operator selects only distinct values by default, while UNION ALL allows duplicates. Examples demonstrate how to use UNION and UNION ALL with and without WHERE clauses to filter results.

Uploaded by

matias bahiru
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views8 pages

SQL UNION Operator Explained

The SQL UNION operator combines the result sets of two or more SELECT statements, requiring the same number of columns with similar data types and order. The UNION operator selects only distinct values by default, while UNION ALL allows duplicates. Examples demonstrate how to use UNION and UNION ALL with and without WHERE clauses to filter results.

Uploaded by

matias bahiru
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

7/15/25, 10:37 AM SQL UNION Operator

 Tutorials  Exercises  Services   Sign In

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C

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 by default. To allow duplicate values, use
UNION ALL :

[Link] 1/8
7/15/25, 10:37 AM SQL UNION Operator

SELECTTutorials  Exercises  Services 


column_name(s) FROM table1
 Sign In

 UNIONCSS
HTML ALL JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
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 Address City PostalCode

1 Alfreds Maria Anders Obere Str. Berlin 12209


Futterkiste 57

2 Ana Trujillo Ana Trujillo Avda. de la México 05021


Emparedados y Constitución D.F.
helados 2222

3 Antonio Moreno Antonio Mataderos México 05023


Taquería Moreno 2312 D.F.

 

And a selection from the "Suppliers" table:

SupplierID SupplierName ContactName Address City PostalCode Co

1 Exotic Liquid Charlotte 49 London EC1 4SD UK


Cooper Gilbert
St.

2 New Orleans Shelley Burke P.O. Box New 70117 USA


Cajun Delights 78934 Orleans

[Link] 2/8
7/15/25, 10:37 AM SQL UNION Operator

3 Tutorials Grandma
Exercises 
Kelly's
Regina Murphy
Services  707 
Oxford
Ann
Arbor
48104 USA
Sign In

HTML
 CSS Homestead
JAVASCRIPT SQL PYTHON Rd.
JAVA PHP HOW TO [Link] C

Earn $5 per answer


Make money from answering simple questions. We pay you in cash. Simple and

fun. 

MetroOpin
 

SQL UNION Example


The following SQL statement returns the cities (only distinct values) from both the
"Customers" and the "Suppliers" table:

Example Get your own SQL Server

SELECT City FROM Customers


UNION
SELECT City FROM Suppliers
ORDER BY City;

Try it Yourself »

Note: If some customers or suppliers have the same city, each city will only be listed
once, because UNION selects only distinct values. Use UNION ALL to also select
duplicate values!

[Link] 3/8
7/15/25, 10:37 AM SQL UNION Operator

 Tutorials  Exercises  Services   Sign In


SQL UNION ALL Example
HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
The following SQL statement returns the cities (duplicate values also) from both the
"Customers" and the "Suppliers" table:

Example
SELECT City FROM Customers
UNION ALL
SELECT City FROM Suppliers
ORDER BY City;

Try it Yourself »

SQL UNION With WHERE


The following SQL statement returns the German cities (only distinct values) from both
the "Customers" and the "Suppliers" table:

Example
SELECT City, Country FROM Customers
WHERE Country='Germany'
UNION
SELECT City, Country FROM Suppliers
WHERE Country='Germany'
ORDER BY City;

Try it Yourself »

SQL UNION ALL With WHERE


[Link] 4/8
7/15/25, 10:37 AM SQL UNION Operator

The following SQL statement returns the German cities (duplicate values also) from both
 Tutorials and Exercises
the "Customers"  Services
the "Suppliers" table:  Sign In

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C

Example
SELECT City, Country FROM Customers
WHERE Country='Germany'
UNION ALL
SELECT City, Country FROM Suppliers
WHERE Country='Germany'
ORDER BY City;

Try it Yourself »

Another UNION Example


The following SQL statement lists all customers and suppliers:

Example
SELECT 'Customer' AS Type, ContactName, City, Country
FROM Customers
UNION
SELECT 'Supplier', ContactName, City, Country
FROM Suppliers;

Try it Yourself »

Notice the "AS Type" above - it is an alias. SQL Aliases are used to give a table or a
column a temporary name. An alias only exists for the duration of the query. So, here we
have created a temporary column named "Type", that list whether the contact person is
a "Customer" or a "Supplier".

[Link] 5/8
7/15/25, 10:37 AM SQL UNION Operator

 Tutorials  Exercises  Services 


?
 Sign In

HTML
 CSS JAVASCRIPT SQL
Exercise
PYTHON JAVA PHP HOW TO [Link] C

What is the primary purpose of the SQL UNION operator?

To create a new table with combined columns

To delete duplicate rows in a table

To perform a self join

To combine the result-sets of two or more SELECT statements

Submit Answer »

❮ Previous Next ❯

Track your progress - it's free! Sign Up Log in

[Link] 6/8
7/15/25, 10:37 AM SQL UNION Operator

 Tutorials  Exercises  Services   Sign In

HTML
 CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C

COLOR PICKER



 PLUS SPACES

GET CERTIFIED FOR TEACHERS

FOR BUSINESS CONTACT US

Top Tutorials
[Link] 7/8
7/15/25, 10:37 AM SQL UNION Operator
HTML Tutorial

 Tutorials  CSS Tutorial


Exercises 
JavaScript Tutorial
Services   Sign In
How To Tutorial
HTML
 CSS SQL Tutorial SQL
JAVASCRIPT PYTHON JAVA PHP HOW TO [Link] C
Python Tutorial
[Link] Tutorial
Bootstrap Tutorial
PHP Tutorial
Java Tutorial
C++ Tutorial
jQuery Tutorial

Top References
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
Python Reference
[Link] Reference
Bootstrap Reference
PHP Reference
HTML Colors
Java Reference
Angular Reference
jQuery Reference

Top Examples Get Certified


HTML Examples HTML Certificate
CSS Examples CSS Certificate
JavaScript Examples JavaScript Certificate
How To Examples Front End Certificate
SQL Examples SQL Certificate
Python Examples Python Certificate
[Link] Examples PHP Certificate
Bootstrap Examples jQuery Certificate
PHP Examples Java Certificate
Java Examples C++ Certificate
XML Examples C# Certificate
jQuery Examples XML Certificate

    

FORUM ABOUT ACADEMY


W3Schools is optimized for learning and training. Examples might be simplified to improve
reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot
warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted our terms of use,
cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by [Link].

[Link] 8/8

You might also like