EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • All Courses
    • All Specializations
  • Blog
  • Enterprise
  • Free Courses
  • All Courses
  • All Specializations
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials SQL Tutorial SQL Set Operators
 

SQL Set Operators

Priya Pedamkar
Article byPriya Pedamkar

Updated March 27, 2023

sql set operators

 

 

Introduction to SQL Set Operators

SQL set operators are used to combine the results obtained from two or more queries into a single result. The queries which contain two or more subqueries are known as compounded queries.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

There are four major types of SQL operators, namely:

  • Union
  • Union all
  • Intersect
  • Minus

Here is an abstract table for whatever we will be learning in this article.

SQL Set Operator

Function

Union Combines distinct results of two or more SELECT statements.
Union All Combines all results of two or more SELECT statements, including duplicates.
Intersect Returns only the common records obtained from two or more SELECT statements.
Minus Returns only those records which are exclusive to the first table.

Syntax and Parameters of SQL Set Operators

The generic syntax for working with SQL set operators is as follows:

Syntax:

SELECT column_name
FROM table_name_1
SET OPERATOR
SELECT column_name
FROM table_name_2
SET OPERATOR
SELECT column_name
FROM table_name_3
.
.
.

Parameters:

The different parameters used in the syntax are :

  • SET OPERATOR: Mention the type of set operation you want to perform from { Union, Union all, Intersect, Minus}
  • column_name: Mention the column name on which you want to perform the set operation and want in the result set
  • FROM table_name_1: Mention the first table name from which the column has to be fetched
  • FROM table_name_2: Mention the second table name from which the column has to be fetched

From the above-mentioned parameters, all the parameters are mandatory. You may use WHERE GROUP BY and HAVING clauses based on your requirements.

Working

Here is a list of few points which we should be kept in mind while working with SQL set operators:

  • The number of columns in the SELECT statement on which we have applied SQL set operators must be the same.
  • The selected columns must have the same data type.
  • The order of the columns must be in the same order as mentioned in the SELECT statement.

Going ahead we will be discussing the above-mentioned functions in great detail.

  • In order to demonstrate and explain the set operators in SQL effectively, we will be using the following tables. These sample tables are “customers_jan” and “customers_dec”. These tables contain 10 records each with the customer’s id, name, city, and the country.
  • Let’s have a look at the records in the customers_jan and customers_dec table. So that later, we can understand how set operations are helpful.

A schema for the discussed tables is as follows:

SQL Set Operators-1.1

customers_jan:

customer jan

customers_dec:

cudtomer dec

Types of SQL Set Operators with Examples

Here we discuss the SQL set operators with examples:

1. Union Set Operator

The UNION set operator is used to combine the results obtained from two or more SELECT statements. Here is an example to illustrate the use of the UNION Operator.

Example:

Find the name of all the customers

SELECT name FROM customers_dec
UNION
SELECT name FROM customers_jan;
Note: Unique selects only distinct values. It can create problems when we have customers with the same name but different countries or cities.

You will notice that there are two customers named Akshay Gupta, one is from Delhi and another one from Bangalore. But the Union operator returned only one customer name. Similarly for Akansha Singh. In order to solve the above-mentioned problem, we can use UNION ALL operator.

2. Union All Set Operator

The UNION set operator is used to combine all the results obtained from two or more SELECT statements. Unlike the Union operator, it considers duplicate values and includes them in the final result.

Here is an example to illustrate the use of UNION ALL Operator.

Example:

Find the names of all the customers who registered in December or January.

SELECT name FROM customers_dec
UNION ALL
SELECT name FROM customers_jan;

You will notice that there are two distinct customers named Akshay Gupta, one is from Delhi and another one from Bangalore. Union ALL operator too both into consideration and returned Akshay Gupta twice. The same is the case for Akansha Singh.

3. Intersect Set Operator

The intersect set operator used to combine all the results of two SELECT statements. But returns only those records that are common to both the SELECT statements.

Note: The INTERSECT operator is not supported in MYSQL databases. We can use the IN or EXIST IN clause for performing similar operations.

Here is an example to illustrate the use of the INTERSECT Operator.

Example:

Find the details of customers who shopped in December and January.

SELECT name, city FROM customers_dec
INTERSECT
SELECT name, city FROM customers_jan;

4. Minus Set Operator

The MINUS set operator used to combine all the results of two or more SELECT statements. But returns only those records that are present exclusively in the first table.

Note: The MINUS operator is supported only in Oracle databases. For other databases like SQL Server, PostgreSQL, and SQLite, we can use the EXCEPT operator to perform similar operations.

Here is an example to illustrate the use of the EXCEPT Operator.

Example:

Find the details of customers who shopped only in December but not January.

SELECT name, city FROM customers_dec
EXCEPT
SELECT name, city FROM customers_jan;

Recommended Articles

We hope that this EDUCBA information on “SQL Set Operators” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. List of SQL Clauses | Syntax and Example
  2. How to Use LIKE Query in SQL?
  3. How Does NOT Condition Work in MySQL?
  4. SQL IN Operator
Primary Sidebar
Footer
Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW