database joins

16
Muhammad Umair Joi n Oracle Database 11g Developer Track

Upload: umair-shakir

Post on 14-Apr-2017

126 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Database Joins

Muhammad Umair

Join

Oracle Database 11g Developer Track

Page 2: Database Joins

› 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

Join

Page 3: Database Joins

› Types Of Joins› Cross Join› Natural Join› Equijoin› Non-Equijoin› Self Join› Inner Join› Outer Join

Join

Page 4: Database Joins

› Cross join– Same as Cartesian product

– SELECT employees.employee_name, departments.department_name

– FROM employees– Corss Join departments;

Join

Page 5: Database Joins

› 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;

Join

Page 6: Database Joins

› Equijoin– SELECT employees.employee_name,

departments.department_name– FROM employees,departments– WHERE employees.department_id =

departments.department_id;

Join

Page 7: Database Joins

› 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;

Join

Page 8: Database Joins

› 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;;

Join

Page 9: Database Joins

› 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;

Join

Page 10: Database Joins

› Self Join– SELECT w.first_name ||’ works for ’|| e.first_name– FROM employees w, employees e– WHER e.manager_id = m.employee_id

Join

Page 11: Database Joins

› INNER JOIN– Returns all rows when there is at least one match in BOTH tables

– SELECT Customers.CustomerName, Orders.OrderIDFROM CustomersINNER JOIN OrdersON Customers.CustomerID=Orders.CustomerID;

Join

Page 12: Database Joins

› 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

Join

Page 13: Database Joins

Join

Left JOINRight JOIN

Full JOIN

Page 14: Database Joins

› 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;

Join

Page 15: Database Joins

› 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 (+);

Join

Page 16: Database Joins

› 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);

Join