Skip to content

Commit 8f27469

Browse files
committed
feat(sql-set-ops): add explanation of SQL Set Operations
What - Added documentation of SQL set operations that combine results of multiple queries: - UNION → merges results of two queries, removing duplicates. - UNION ALL → merges results, keeping duplicates. - INTERSECT → returns rows common to both queries. - EXCEPT / MINUS (DB-specific) → returns rows from first query not present in second. - Explained syntax with simple two-table examples (e.g., students vs alumni). - Highlighted rules: same number of columns, compatible data types. Why - Set operations are fundamental for advanced SQL querying when multiple datasets must be compared or merged. - Developers often face scenarios like combining customers and leads, or finding overlapping records. - Distinguishes set-based operations from joins (row vs set comparison). How to use - UNION: Merge active and inactive employee lists into one result set. - UNION ALL: Combine product sales logs while keeping duplicate entries. - INTERSECT: Find students who are also alumni. - EXCEPT/MINUS: Identify customers who have not yet placed an order. Real-life Applications - Data warehousing: Merging historical and live datasets. - Business intelligence: Finding overlaps or differences between customer segments. - Compliance: Extracting records missing in one system compared to another. Notes - UNION removes duplicates automatically; use UNION ALL for performance if duplicates are acceptable. - Column names in result set follow the first query. - DBMS differences: MINUS in Oracle vs EXCEPT in SQL Server/PostgreSQL. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 7239773 commit 8f27469

File tree

4 files changed

+17
-21
lines changed

4 files changed

+17
-21
lines changed

Section28JDBCusingSQLite/SQL (Aggregated Functions & Set Operations)/Set Operations/SQL Set Operations Detail.txt renamed to Section28JDBCusingSQLite/SQL (Aggregated Functions & Set Operations)/Set Operations SQL/SQL set operations in theory, all common set operation types.txt

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@ SQL set operations in theory, all common set operation types.
22

33
---
44

5-
# SQL Set Operations: Theory and Concepts
6-
5+
# SQL Set Operations: Theory and Concepts:
76
SQL set operations are used to combine the results of two or more SELECT queries into a single result set.
87
They work similarly to mathematical set operations. Each operation has its own characteristics, and
98
understanding them is important when designing queries that require the merging of data from multiple queries.
109

11-
## Key Set Operations in SQL
12-
13-
### 1. UNION
10+
Key Set Operations in SQL:
1411

12+
1. UNION
1513
- Purpose:
1614
Combines the results of two SELECT queries and returns only distinct rows.
1715

@@ -25,8 +23,7 @@ understanding them is important when designing queries that require the merging
2523
Use UNION when you want to merge similar datasets and remove duplicates, such as combining student names
2624
from different cities while ensuring each name appears only once.
2725

28-
### 2. UNION ALL
29-
26+
2. UNION ALL
3027
- Purpose:
3128
Similar to UNION, but includes all duplicate rows in the result set.
3229

@@ -39,8 +36,7 @@ understanding them is important when designing queries that require the merging
3936
Use UNION ALL when duplicate rows are meaningful or when performance is a concern and you know
4037
duplicates do not affect the outcome.
4138

42-
### 3. INTERSECT
43-
39+
3. INTERSECT
4440
- Purpose:
4541
Returns only the rows that are common to both SELECT query results.
4642

@@ -53,8 +49,8 @@ understanding them is important when designing queries that require the merging
5349
Use INTERSECT when you need to find common records between two datasets, such as the list of students enrolled in
5450
both two different courses.
5551

56-
### 4. EXCEPT (or MINUS)
5752

53+
4. EXCEPT (or MINUS)
5854
- Purpose:
5955
Returns rows from the first SELECT query that are not present in the second SELECT query.
6056

@@ -68,8 +64,7 @@ understanding them is important when designing queries that require the merging
6864
Use EXCEPT when you need to identify differences between two datasets, such as finding students
6965
who are enrolled in one course but not in another.
7066

71-
## Important Considerations for All Set Operations
72-
67+
Important Considerations for All Set Operations:
7368
- Column Consistency:
7469
Every SELECT statement combined by a set operator must return the same number of columns.
7570
The data types of corresponding columns must be compatible to ensure the operations can be performed without errors.
@@ -90,4 +85,4 @@ understanding them is important when designing queries that require the merging
9085
- SQL Dialects Variations:
9186
While the core principles remain the same across different SQL dialects, certain keywords and behaviors
9287
(like the use of MINUS instead of EXCEPT) might differ slightly. Always refer to your specific SQL database
93-
documentation for exact details.
88+
documentation for exact details.
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
Set operations allow you to combine the results of multiple SELECT queries into a single result set.
22

33
The most common set operations are:
4-
UNION: Combines the results of two queries and removes duplicate rows.
5-
UNION ALL: Combines the results of two queries and includes all duplicates.
6-
INTERSECT: Returns only the rows that appear in both queries.
7-
EXCEPT (or MINUS in some SQL dialects): Returns rows from the first query that are not in the second query.
4+
UNION: Combines the results of two queries and removes duplicate rows.
5+
UNION ALL: Combines the results of two queries and includes all duplicates.
6+
INTERSECT: Returns only the rows that appear in both queries.
7+
EXCEPT (or MINUS in some SQL dialects): Returns rows from the first query that are not in the second query.
88

99
Important points:
1010
1. Each SELECT statement must have the same number of columns.
@@ -13,10 +13,11 @@ Important points:
1313

1414
Example usage from your commands:
1515
Combining student names from 'Delhi' and 'Mumbai' cities:
16-
SELECT name FROM students WHERE city = 'Delhi'
17-
UNION
18-
SELECT name FROM students WHERE city = 'Mumbai';
16+
SELECT name FROM students WHERE city = 'Delhi'
17+
UNION
18+
SELECT name FROM students WHERE city = 'Mumbai';
19+
1920
This returns a list of unique names from both cities.
2021

2122
When selecting different columns (like name from one query and roll, name from another),
22-
SQLite returns an error because the SELECT statements don't have the same number of columns.
23+
SQLite returns an error because the SELECT statements don't have the same number of columns.

0 commit comments

Comments
 (0)