This document summarizes different types of joins in Oracle Database 11g, including Cartesian products, natural joins, equijoins, non-equijoins, self joins, inner joins, and outer joins. It provides examples of SQL queries for each type of join, explaining how to select columns from multiple tables that meet certain join conditions in the WHERE clause.
2. › Cartesian Product
– A Cartesian product is formed when:
› A join condition is invalid
› All row of table A are joined with table B all rows
– To avoid Cartesian product always include a valid join condition in
where clause
4. › Cross join
– Same as Cartesian product
– SELECT employees.employee_name, departments.department_name
– FROM employees
– Corss Join departments;
5. › Natural join
– Join all columns in the two tables that have the same name.
– Selects rows from the two tables that have equal values in all matches
columns.
– If the columns having the same names but different datatypes, error
will return
– SELECT employees.employee_name, departments.department_name
– FROM employees
– Natural Join departments;
6. › Equijoin
– SELECT employees.employee_name, departments.department_name
– FROM employees,departments
– WHERE employees.department_id = departments.department_id;
7. › Using Operatores in Equijoin
– SELECT employees.employee_name, departments.department_name
– FROM employees,departments
– WHERE employees.department_id = departments.department_id
– AND employee_id = 105;
8. › More Then Two Tables
– SELECT employees.employee_name,
departments.department_name,city
– FROM employees,departments,locations
– WHERE employees.department_id = departments.department_id
– AND departments.location_id = location.location_id;
;
9. › Non-Equijoin
– SELECT e.first_name,j.grade_level
– FROM employees e, job_grades j
– WHER e.salary
– BETWEEN j.lowest_sal AND j.highest_sal;
10. › Self Join
– SELECT w.first_name ||’ works for ’|| e.first_name
– FROM employees w, employees e
– WHER e.manager_id = m.employee_id
11. › INNER JOIN
– Returns all rows when there is at least one match in BOTH tables
– SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
INNER JOIN Orders
ON Customers.CustomerID=Orders.CustomerID;
12. › OUTER JOIN
– You use outer join to see rows that do not meet the join condition.
– LEFT JOIN
› Return all rows from the left table, and the matched rows from the right table
– RIGHT JOIN
› Return all rows from the right table, and the matched rows from the left table
– FULL JOIN
– Return all rows when there is a match in ONE of the tables
14. › Left JOIN
– SELECT e.last_name, e.department_id, d.department_name
– FROM employees e
– LEFT OUTER JOIN department d
– ON (e.department_id = d.epartment_id);
– SELECT e.last_name, e.department_id, d.department_name
– FROM employees e, departments d
– WHERE e.department_id (+)= d.epartment_id;
15. › Right JOIN
– SELECT e.last_name, e.department_id, d.department_name
– FROM employees e
– Right OUTER JOIN department d
– ON (e.department_id = d.epartment_id);
– SELECT e.last_name, e.department_id, d.department_name
– FROM employees e, departments d
– WHERE e.department_id = d.epartment_id (+);
16. › Full Outer JOIN
– SELECT e.last_name, e.department_id, d.department_name
– FROM employees e
– FULL OUTER JOIN department d
– ON (e.department_id = d.epartment_id);