Tuesday, August 25, 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
Sagar is multidisciplinary technologist, educator, and entrepreneur based in Nepal. As the founder of Illionso Technologies and Kashi Garden Resort, he operates at the intersection of web architecture, data intelligence, innovation, and digital transformation. From building digital solutions to scaling real-world concepts, his mission is to share knowledge alongside merge emerging as well as disruptive technologies with transformative offline experiences.
RELATED ARTICLES

SynthID and C2PA Explained

Welcome to another AI-focused article here on datasagar.com! As I scroll through my social media feeds lately, I find myself playing a...

Why Linux Won the Infrastructure War? Lessons from Linus Torvalds

In 1991, 21-year-old Linus Torvalds announced a "hobby" operating system. Today, Linux powers supercomputers, cloud servers, and Android. Explore his journey, the open-source economy, and the lessons for modern developers.

What Is Vibe Coding? A Complete Beginner’s Guide to Building Software and Web Pages With AI

A few years ago, building a website meant sitting down with a programming book, picking a language, and slowly working through syntax...

1 COMMENT

Most Popular

SynthID and C2PA Explained

Welcome to another AI-focused article here on datasagar.com! As I scroll through my social media feeds lately, I find myself playing a...

Why Linux Won the Infrastructure War? Lessons from Linus Torvalds

In 1991, 21-year-old Linus Torvalds announced a "hobby" operating system. Today, Linux powers supercomputers, cloud servers, and Android. Explore his journey, the open-source economy, and the lessons for modern developers.

What Is Vibe Coding? A Complete Beginner’s Guide to Building Software and Web Pages With AI

A few years ago, building a website meant sitting down with a programming book, picking a language, and slowly working through syntax...

What is Answer Engine Optimization (AEO)? Getting Ready for AI Powered Search and Agentic Era Digital Marketing

Search is changing faster than it has in the last two decades. For years, businesses focused on ranking their websites on search...