Module 5 - SQL Joins
2026-02-18 21:07
Tags: #ADV_DBMS
Author: Duke Hsu
Module 5 - SQL Joins
Topic / Concept
- Three types of joins
- Compare the difference of join and subqueries
Three types of joins
INNER JOIN
- Return records that have matching values in both tables.
Syntax:
1 2 3 4 | |
flowchart LR
subgraph A["STUDENT"]
A1[(StudentID)]
A2[StudentName]
end
subgraph B["ENROLLMENT"]
B1[(StudentID)]
B2[CourseID]
end
A1 -->|ON StudentID| J{{INNER JOIN}}
B1 -->|ON StudentID| J
J --> R["JOINED RESULT"]
Example:
1 2 3 4 | |
NATURAL JOIN
- A NATURAL JOIN is a type of inner join that automatically matches columns between the two tables that have the same name and data type. The natural join implicitly uses these columns to perform the join which can sometimes simplify queries.
Syntax
1 2 3 | |
Example:
1 2 3 | |
Note
Inner join: requires explicit columns to be specified in the ON clause for the Join condition Natural join: Automatically uses all columns with the same name and data type in both tables to join the tables.
flowchart LR
subgraph STUDENT
S1[(StudentID)]
S2[StudentName]
end
subgraph ENROLLMENT
E1[(StudentID)]
E2[CourseID]
end
S1 --> J{{NATURAL JOIN}}
E1 --> J
J --> P[π Remove Duplicate StudentID]
P --> R["Final Relation"]
LEFT JOIN
LEFT JOIN, returns all the rows from the left table, even if there are no matches in the right table , if there are no matches,NULLvalues will be returned in the columns of the right table- Right table not match -> NULL
Syntax:
1 2 3 4 | |
Example:
1 2 3 4 | |
flowchart LR
subgraph A["users (LEFT TABLE)"]
A1[(usersID)]
A2[usersName]
end
subgraph B["orders (RIGHT TABLE)"]
B1[(usersID)]
B2[orderID]
B3[amount]
end
A1 -->|ON usersID| J{{LEFT JOIN}}
B1 -->|Match if Exists| J
J --> R["ALL users + MATCHED orders (Unmatched → NULL)"]
RIGHT JOIN
RIGHT JOINreturns all rows from the right table , event if there are no matches in the left table , unmatched left-side columns will beNULL- Left table not matched -> NULL
Syntax:
1 2 3 4 | |
Example:
1 2 3 4 | |
flowchart LR
subgraph A["users (LEFT TABLE)"]
A1[(usersID)]
A2[usersName]
end
subgraph B["orders (RIGHT TABLE)"]
B1[(usersID)]
B2[ordersID]
B3[amount]
end
A1 -->|Match if Exists| J{{RIGHT JOIN}}
B1 -->|ON usersID| J
J --> R["ALL orders + MATCHED users (Unmatched → NULL)"]
CROSS JOIN
- The CROSS JOIN keyword returns the Cartesian product of two or more tables(combines every row from the first table with every row from the second table)
Note
嵌入式for loop 的概念, 例子:2個3rows的table 通過cross join 會變成1個9rows的table
Syntax :
1 2 3 | |
Example:
1 2 3 | |
FULL OUTER JOIN
- FULL OUTER JOIN , returns all records when there is a match in either left or right table
Syntax:
1 2 3 4 5 | |
Example:
1 2 3 4 5 | |
SELF JOIN
- A self join is a regular join, but the table is joined with itself .
!!note 使用場景: 同一張表,既存在員工, 也存在主管; A single table contains both employees and manager.
Syntax
1 2 3 | |
Example
1 2 3 4 5 6 7 | |
Gemini canvas
https://gemini.google.com/share/531b591e835c
References
Module 5: SQL Joins.PPTX