Joins in DBMS: Combining Data from Multiple Tables
Learn about the concept of joins in database management systems (DBMS), which allow you to combine data from different tables based on specified conditions. Explore the various types of joins, including Theta join, natural join, inner join, outer join (left, right, full), and self-join, and understand their applications in data retrieval and analysis.
DBMS - Joins
Joins allow us to combine tuples from different relations based on a specified condition, providing an alternative to the Cartesian product, which can be computationally expensive for large relations. A join operation pairs two tuples from different relations only if a given join condition is satisfied.
Theta (θ) Join
A Theta join combines tuples from different relations if they meet a specified condition, denoted by θ, which can involve any comparison operator.
Notation: R1 ⋈θ R2
Here, R1 and R2 represent relations with attributes (A1, A2, ..., An) and (B1, B2, ..., Bn), and they must not have any common attributes (R1 ∩ R2 = Φ).
Example:
SID | Name | Std |
---|---|---|
101 | Alex | 10 |
102 | Maria | 11 |
Class | Subject |
---|---|
10 | Math |
10 | English |
11 | Music |
11 | Sports |
Join operation: STUDENT ⋈Student.Std = Subject.Class SUBJECT
SID | Name | Std | Class | Subject |
---|---|---|---|---|
101 | Alex | 10 | 10 | Math |
101 | Alex | 10 | 10 | English |
102 | Maria | 11 | 11 | Music |
102 | Maria | 11 | 11 | Sports |
Equijoin
An Equijoin is a Theta join that uses only the equality comparison operator. The example above also represents an Equijoin, as the join condition involves equality.
Natural Join (⋈)
A Natural Join does not use any comparison operators but relies on matching attributes between two relations, which must have the same name and domain. The Natural Join combines tuples with equal values in the common attribute(s) and eliminates duplicate attributes.
Example:
CID | Course | Dept |
---|---|---|
CS01 | Database | CS |
ME01 | Mechanics | ME |
EE01 | Electronics | EE |
Dept | Head |
---|---|
CS | Alex |
ME | Maya |
EE | Mira |
Natural Join: Courses ⋈ HoD
Dept | CID | Course | Head |
---|---|---|---|
CS | CS01 | Database | Alex |
ME | ME01 | Mechanics | Maya |
EE | EE01 | Electronics | Mira |
Outer Joins
Inner joins include only tuples with matching attributes. Outer joins, however, also include non-matching tuples, making them useful for preserving data from one or both relations. There are three types of outer joins:
Left Outer Join
All tuples from the left relation are included in the result. Non-matching tuples from the right relation are filled with NULL values.
A | B | C | D |
---|---|---|---|
100 | Database | 100 | Alex |
101 | Mechanics | --- | --- |
102 | Electronics | 102 | Maya |
Right Outer Join
All tuples from the right relation are included, with non-matching tuples from the left relation filled with NULL values.
A | B | C | D |
---|---|---|---|
100 | Database | 100 | Alex |
102 | Electronics | 102 | Maya |
--- | --- | 104 | Mira |
Full Outer Join
All tuples from both relations are included, with unmatched attributes filled with NULL values for each relation.
A | B | C | D |
---|---|---|---|
100 | Database | 100 | Alex |
101 | Mechanics | --- | --- |
102 | Electronics | 102 | Maya |
--- | --- | 104 | Mira |