loading...

June 9, 2024

Set Theory in SQL: UNION, INTERSECT, and EXCEPT

In the third chapter of DataCamp’s “Joining Data in SQL” course, I explored the fascinating world of Set Theory in SQL. Here’s a comprehensive overview of the key concepts, complete with examples and explanations.

Introduction to Venn Diagrams and Set Theory

Set theory is a fundamental concept in mathematics, often visualized using Venn diagrams. In SQL, set theory operations help manipulate and combine data from multiple tables. Here are the core concepts:

Table 1: students

student_idname
1Alice
2Bob
3Carol
4Dave

Table 2: enrolled_students

student_idcourse
1Math
2Science
4Art
5History

1. UNION

The UNION operator combines the result sets of two or more SELECT statements and removes duplicates.

Example:

SELECT student_id, name FROM students
UNION
SELECT student_id, course FROM enrolled_students;

Result:

student_idname
1Alice
2Bob
3Carol
4Dave
4Art
5History

2. UNION ALL

The UNION ALL operator combines the result sets of two or more SELECT statements without removing duplicates.

Example:

SELECT student_id, name FROM students
UNION ALL
SELECT student_id, course FROM enrolled_students;

Result:

student_idname
1Alice
2Bob
3Carol
4Dave
1Math
2Science
4Art
5History

3. INTERSECT

The INTERSECT operator returns the common records between two SELECT statements.

Example:

SELECT student_id FROM students
INTERSECT
SELECT student_id FROM enrolled_students;

Result:

student_id
1
2
4

4. INTERSECT vs INNER JOIN

While INTERSECT returns common rows from two SELECT statements, INNER JOIN combines rows from two tables based on a related column. Here’s a more detailed comparison using the same tables:

INTERSECT Example:

SELECT student_id, name FROM students
INTERSECT
SELECT student_id, course FROM enrolled_students;

Result:

student_idname
1Alice
2Bob
4Dave

INNER JOIN Example:

SELECT students.student_id, students.name, enrolled_students.course
FROM students
INNER JOIN enrolled_students
ON students.student_id = enrolled_students.student_id;

Result:

student_idnamecourse
1AliceMath
2BobScience
4DaveArt

The key difference is that INTERSECT only returns the common student_id values and their associated columns from each SELECT statement, whereas INNER JOIN returns all columns from both tables for the matched student_id values, allowing for more comprehensive data retrieval.

5. EXCEPT

The EXCEPT operator returns the rows from the first SELECT statement that are not present in the second SELECT statement.

Example:

SELECT student_id FROM students
EXCEPT
SELECT student_id FROM enrolled_students;

Result:

student_id
3

Summary Table

ConceptDescriptionExample
UNIONCombines result sets and removes duplicatesSELECT ... UNION SELECT ...
UNION ALLCombines result sets without removing duplicatesSELECT ... UNION ALL SELECT ...
INTERSECTReturns common records between two SELECT statementsSELECT ... INTERSECT SELECT ...
INTERSECT vs INNER JOININTERSECT returns common rows, INNER JOIN combines rows based on a related columnSELECT ... INTERSECT SELECT ... vs INNER JOIN
EXCEPTReturns rows from the first SELECT not in the secondSELECT ... EXCEPT SELECT ...

I’ve found learning set theory in SQL both challenging and rewarding. It’s like solving a puzzle where you fit different pieces together to see the bigger picture. These operations, UNION, INTERSECT, and EXCEPT, have made me appreciate how powerful and flexible SQL can be.

Despite the busy end-of-year schedule filled with student evaluations, state exams, teacher assessments, organizing remedial classes and summer camps, and end-of-year concerts, I remain committed to my learning journey. Balancing these responsibilities with raising my rapidly growing child (and enduring the inevitable sleep deprivation all new parents face), I persevere in studying and applying new concepts daily. My ultimate goal is to transition to a software engineer role. Although I don’t plan to delve deeply into SQL for a career as a data scientist, I am confident that I now have the essential SQL foundations needed to revisit and build upon this knowledge in more advanced software development courses.

Happy coding!

Best regards,
The Principal


Feel free to like, comment, and share your thoughts! 🚀✨

Posted in Data ScienceTaggs:
Write a comment