Wednesday, July 29, 2026
Home Academic SET operations in SQL

SET operations in SQL

Set operations combines the results of two independent SQL queries. There are three set operations in SQL 

1.Union

2. Intersect

3. Except

These set operations operate on relations and are based on the general principles of mathematical set theory that you’ve probably covered in school level math. 

One thing to not forgot is, each of these operations automatically eliminates duplicates in the result. In order to retain all the duplicates, one can use the corresponding multiset versions of basic set operations union all, intersect all and except all.

Suppose a tuple/row occurs m times in a relation named r and n times in relation named s then it occurs

m  + n times in r union all s

min(m,n) times in r intersect all s

max(0, m – n) times in r except all s.

Some example queries are shown in image below:

The relation schemes for all the examples used above are:

Branch(bname, bcity, assets)

Customer(cname, street, ccity)

Depositor(cname, account#)

Account(bname, account#, balance)

Loan(bname, loan#, amount)

Borrower(cname, loan#)

datasagarhttp://www.DataSagar.com
The author of this blog post is a technology fellow, an IT entrepreneur, and Educator in Kathmandu Nepal. With his keen interest in Data Science and Business Intelligence, he writes on random topics occasionally in the DataSagar blog.
RELATED ARTICLES

Meta to Deploy Nepal’s First Direct Point of Presence (PoP) Data Center in Kathmandu

Matt Jensen, a representative from Meta, announced that Meta will deploy its own dedicated Point of Presence (PoP) infrastructure in Nepal at Data World’s Tier-3 Data Center located in Matatirthha, Kathmandu, targeted for completion by late 2026.

How to setup DNS Records in Cloudflare – Guide for Website Owners

Whether you're launching your first website, connecting a custom email address, or moving your domain to a new hosting provider, you've probably...

DeepSeek AI: The Open-Source AI Disruptor Shaking Up the Tech World

DeepSeek AI is revolutionizing the artificial intelligence landscape with its open-source, cost-effective, and highly customizable model. Built on cutting-edge transformer architecture and fine-tuned using reinforcement learning with human feedback (RLHF), DeepSeek delivers human-like text generation and accurate responses. Its efficient training techniques reduce computational costs by up to 40%, while its foundation on open-source frameworks like PyTorch and TensorFlow ensures seamless integration for developers. Whether you're a startup, a developer, or a large enterprise, DeepSeek offers unparalleled transparency, affordability, and performance, making it a game-changer in the world of AI. Discover why DeepSeek is outshining competitors like ChatGPT and Google Gemini in this comprehensive guide.

1 COMMENT

Most Popular

Meta to Deploy Nepal’s First Direct Point of Presence (PoP) Data Center in Kathmandu

Matt Jensen, a representative from Meta, announced that Meta will deploy its own dedicated Point of Presence (PoP) infrastructure in Nepal at Data World’s Tier-3 Data Center located in Matatirthha, Kathmandu, targeted for completion by late 2026.

How to setup DNS Records in Cloudflare – Guide for Website Owners

Whether you're launching your first website, connecting a custom email address, or moving your domain to a new hosting provider, you've probably...

DeepSeek AI: The Open-Source AI Disruptor Shaking Up the Tech World

DeepSeek AI is revolutionizing the artificial intelligence landscape with its open-source, cost-effective, and highly customizable model. Built on cutting-edge transformer architecture and fine-tuned using reinforcement learning with human feedback (RLHF), DeepSeek delivers human-like text generation and accurate responses. Its efficient training techniques reduce computational costs by up to 40%, while its foundation on open-source frameworks like PyTorch and TensorFlow ensures seamless integration for developers. Whether you're a startup, a developer, or a large enterprise, DeepSeek offers unparalleled transparency, affordability, and performance, making it a game-changer in the world of AI. Discover why DeepSeek is outshining competitors like ChatGPT and Google Gemini in this comprehensive guide.

Useful Python Libraries for Data Science & ML Enthusiasts

Hi there, DataSagar here! If you're as passionate about data science as I am, then you know how overwhelming it can be...