DATABASE SYSTEM DESIGN AND MANAGEMENT
What three join types are included in the outer join classification, what is a cross join, and what is a recursive join?
Provide 2 examples on each Syntax
Sample Solution
- LEFT JOIN: Returns all rows from the left table, even if there are no matching rows in the right table. The rows in the right table that do not have a match in the left table will have NULL values in the columns from the left table.
- RIGHT JOIN: Returns all rows from the right table, even if there are no matching rows in the left table. The rows in the left table that do not have a match in the right table will have NULL values in the columns from the right table.
- FULL OUTER JOIN: Returns all rows from both tables, even if there are no matching rows in the other table. The rows in each table that do not have a match in the other table will have NULL values in the columns from the other table.
Full Answer Section
This query will return all possible combinations of rows from the table1
and table2
tables.
Recursive join:
A recursive join is a type of join that is used to traverse a tree-like structure. It works by joining a table to itself, with a recursive WHERE clause that specifies the parent-child relationship between the rows.
For example, the following query will return all employees and their managers, recursively:
SQL
WITH RECURSIVE employees (
id,
name,
manager_id
) AS (
SELECT
id,
name,
NULL AS manager_id
FROM employees
WHERE manager_id IS NULL
UNION ALL
SELECT
e.id,
e.name,
e.manager_id
FROM employees e
INNER JOIN employees m
ON e.manager_id = m.id
)
SELECT *
FROM employees;