| Lesson 4 | Different Types of joins |
| Objective | Define same-table, outer, and Cartesian product joins. |
A same-table join[1] is an inner join in which a table is joined to itself.
This technique is used when the rows of a single table contain a hierarchical or recursive relationship. A classic example is an Employee table that stores both employees and their supervisors in the same structure.
Consider the following EMPLOYEE table:
| EmpID | Name | SupervisorID |
| ----- | ----------- | ------------ |
| 101 | J. Reynolds | 103 |
| 102 | P. Short | 103 |
| 103 | A. Kell | 104 |
| 104 | C. Frye | 104 |
1) Employee table consisting of columns EmpID, Name, SupervisorID
Because supervisors are also employees, the query must use two instances of the same table -one representing the employee, one representing the supervisor. The resulting same-table join is shown below:
| EmpID | Name (Employee) | SupervisorID | Name (Supervisor) |
| ----- | ---------------- | ------------ | ----------------- |
| 101 | J. Reynolds | 103 | A. Kell |
| 102 | P. Short | 103 | A. Kell |
| 103 | A. Kell | 104 | C. Frye |
| 104 | C. Frye | 104 | C. Frye |
2) A join consisting of a) Employee instance and b) Supervisor instance
An outer join[2] returns all rows from one table, even when no matching row exists in the other table. Inner joins return only matching pairs; outer joins return unmatched rows as well, filling missing values with NULL.
In the example below, the Customer table contains customers who may or may not have placed orders.
| CustID | CustName |
| ------ | ------------- |
| 101 | R. MacDicken |
| 102 | K. McCarty |
| 103 | J. Montgomery |
| OrderID | CustName | CustID | Title |
| ------- | ------------ | ------ | -------------- |
| 901 | R. MacDicken | 101 | Southern Tales |
| 902 | R. MacDicken | 101 | Northern Tales |
| 903 | K. McCarty | 102 | Southern Tales |
1) Consider the above records from these truncated versions of the Customer and Order tables.
| CustID | CustName | OrderID | Title |
| ------ | ------------- | ------- | -------------- |
| 101 | R. MacDicken | 901 | Southern Tales |
| 101 | R. MacDicken | 902 | Northern Tales |
| 102 | K. McCarty | 903 | SouthernTales |
| 103 | J. Montgomery | (null) | (null) |
2) When two tables are combined by an outer join, any records from the base table with no matching records from the other table are included in the results and any columns where no values are available are filled with nulls.
You use an outer join when the business rule requires showing all rows from one table, regardless of whether the related table contains matching rows. Common examples include:
A Cartesian product[3] (also called a cross join) occurs when no join condition is supplied. Every row from the first table is combined with every row from the second table. The result is usually unintended and massive, especially for large tables.
The Cartesian product of the Customer and Order tables looks like this:
| CustID | CustName | OrderID | Title |
| ------ | ------------- | ------- | -------------- |
| 101 | R. MacDicken | 901 | Southern Tales |
| 101 | R. MacDicken | 902 | Northern Tales |
| 101 | R. MacDicken | 903 | SouthernTales |
| 102 | K. McCarty | 901 | Southern Tales |
| 102 | K. McCarty | 902 | Northern Tales |
| 102 | K. McCarty | 903 | SouthernTales |
| 103 | J. Montgomery | 901 | Southern Tales |
| 103 | J. Montgomery | 902 | Northern Tales |
| 103 | J. Montgomery | 903 | SouthernTales |
Cartesian product of the Customer and Order tables
Cartesian products are almost never useful for real-world reporting. In fact, they are one of the most common SQL mistakes made by beginners - typically caused by forgetting a join condition. Because the number of resulting rows is rows(tableA) × rows(tableB), the size of the result set grows explosively.
FROM clause.
When two of these tables share column names, references to those columns must be fully qualified to avoid ambiguity.
FROM clause or the WHERE clause.
Oracle evaluates the join by pairing rows from each table where the join condition is TRUE.
For multi-table joins, Oracle combines tables step-by-step, using the optimizer to determine the most efficient join order.
NULL.