Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4 JOINING TABLES & FUNCTION Lecture by Ty Rasmey

Similar presentations


Presentation on theme: "Chapter 4 JOINING TABLES & FUNCTION Lecture by Ty Rasmey"— Presentation transcript:

1 Chapter 4 JOINING TABLES & FUNCTION Lecture by Ty Rasmey

2  JOINING TABLES  NUMERIC, CHARACTER AND DATE FUNCTIONS  CONVERSION AND MISCELLANEOUS FUNCTIONS

3  A join is a query that combines rows from two or more tables, views, or materialized views.  Oracle Database performs a join whenever multiple tables appear in the FROM clause of the query.  The select list of the query can select any columns from any of these tables.  If any two of these tables have a column name in common, then you must qualify all references to these columns throughout the query with table names to avoid ambiguity.

4 EMPNO DEPTNO LOC NEW YORK CHICAGO NEW YORK DALLAS CHICAGO CHICAGO rows selected. EMPNO DEPTNO LOC NEW YORK CHICAGO NEW YORK DALLAS CHICAGO CHICAGO rows selected. EMPDEPT EMPNOENAME...DEPTNO KING BLAKE MILLER DEPTNO DNAME LOC ACCOUNTINGNEW YORK 20RESEARCHDALLAS 30SALESCHICAGO 40OPERATIONSBOSTON

5  Use a join to query data from more than one table.  Write the join condition in the WHERE clause.  Prefix the column name with the table name when the same column name appears in more than one table. SELECTtable1.column, table2.column FROMtable1, table2 WHEREtable1.column1 = table2.column2; SELECTtable1.column, table2.column FROMtable1, table2 WHEREtable1.column1 = table2.column2;

6 1. Equijoins 2. Self Joins 3. Cartesian Products 4. Inner Joins 5. Outer Joins 6. Antijoins 7. Semijoins

7 1. Equijoin

8  An equijoin is a join with a join condition containing an equality operator.  An equijoin combines rows that have equivalent values for the specified columns.

9 EMP DEPT EMPNO ENAME DEPTNO KING BLAKE CLARK JONES MARTIN ALLEN TURNER JAMES WARD FORD SMITH rows selected. DEPTNO DNAME LOC ACCOUNTING NEW YORK 30 SALES CHICAGO 10 ACCOUNTINGNEW YORK 20 RESEARCHDALLAS 30 SALES CHICAGO 20 RESEARCHDALLAS rows selected. Foreign key Primary key

10 SQL> SELECT emp.empno, emp.ename, emp.deptno, 2dept.deptno, dept.loc 3 FROM emp, dept 4 WHERE emp.deptno=dept.deptno; EMPNO ENAME DEPTNO DEPTNO LOC KING NEW YORK 7698 BLAKE CHICAGO 7782 CLARK NEW YORK 7566 JONES DALLAS rows selected.

11 SQL> SELECT last_name, job_id, departments.department_id, department_name FROM employees, departments WHERE employees.department_id = departments.department_id ORDER BY last_name, job_id; LAST_NAME JOB_ID DEPARTMENT_ID DEPARTMENT_NAME Abel SA_REP 80 Sales Ande SA_REP 80 Sales Atkinson ST_CLERK 50 Shipping Austin IT_PROG 60 IT...

12  Write equijoin to returns the name, job, department number, and department name of all sales managers that have job_id = ‘SA_MAN’ and sort by mane;

13  Use table prefixes to qualify column names that are in multiple tables.  Improve performance by using table prefixes.  Distinguish columns that have identical names but reside in different tables by using column aliases.

14  Simplify queries by using table aliases. SQL> SELECT emp.empno, emp.ename, emp.deptno, 2 dept.deptno, dept.loc 3 FROM emp, dept 4 WHERE emp.deptno=dept.deptno; SQL> SELECT e.empno, e.ename, e.deptno, 2 d.deptno, d.loc 3 FROM emp e, dept d 4 WHERE e.deptno= d.deptno;

15 NAMECUSTID JOCKSPORTS 100 TKB SPORT SHOP 101 VOLLYRITE 102 JUST TENNIS 103 K+T SPORTS 105 SHAPE UP 106 WOMENS SPORTS rows selected. NAMECUSTID JOCKSPORTS 100 TKB SPORT SHOP 101 VOLLYRITE 102 JUST TENNIS 103 K+T SPORTS 105 SHAPE UP 106 WOMENS SPORTS rows selected. CUSTOMER CUSTID ORDID rows selected. CUSTID ORDID rows selected.ORD ORDID ITEMID rows selected. ORDID ITEMID rows selected.ITEM

16 2. Self Join

17  A self join is a join of a table to itself. This table appears twice in the FROM clause and is followed by table aliases that qualify column names in the join condition.  To perform a self join, Oracle Database combines and returns rows of the table that satisfy the join condition.

18 EMP (WORKER)EMP (MANAGER) “ “MGR in the WORKER table is equal to EMPNO in the MANAGER table” EMPNOENAME MGR KING 7698BLAKE CLARK JONES MARTIN ALLEN7698 EMPNOENAME KING 7698BLAKE

19 WORKER.ENAME||'WORKSFOR'||MANAG BLAKE works for KING CLARK works for KING JONES works for KING MARTIN works for BLAKE rows selected. WORKER.ENAME||'WORKSFOR'||MANAG BLAKE works for KING CLARK works for KING JONES works for KING MARTIN works for BLAKE rows selected. SQL> SELECT worker.ename||' works for '||manager.ename 2 FROM emp worker, emp manager 3 WHERE worker.mgr = manager.empno;

20 3.Cartesian Products

21  If two tables in a join query have no join condition, then Oracle Database returns their Cartesian product.  Oracle combines each row of one table with each row of the other.  A Cartesian product always generates many rows and is rarely useful

22  For example, the Cartesian product of two tables, each with 100 rows, has 10,000 rows.  Always include a join condition unless you specifically need a Cartesian product.  If a query joins three or more tables and you do not specify a join condition for a specific pair, then the optimizer may choose a join order that avoids producing an intermediate Cartesian product.

23 ENAME DNAME KINGACCOUNTING BLAKE ACCOUNTING... KINGRESEARCH BLAKE RESEARCH rows selected. ENAME DNAME KINGACCOUNTING BLAKE ACCOUNTING... KINGRESEARCH BLAKE RESEARCH rows selected. EMP (14 rows) DEPT (4 rows) EMPNOENAME...DEPTNO KING BLAKE MILLER EMPNOENAME...DEPTNO KING BLAKE MILLER DEPTNO DNAME LOC ACCOUNTINGNEW YORK 20RESEARCHDALLAS 30SALESCHICAGO 40OPERATIONSBOSTON DEPTNO DNAME LOC ACCOUNTINGNEW YORK 20RESEARCHDALLAS 30SALESCHICAGO 40OPERATIONSBOSTON “ “Cartesian product: 14*4=56 rows”

24 4. Inner Join

25  An inner join (sometimes called a simple join) is a join of two or more tables that returns only those rows that satisfy the join condition

26 5. Outer Join

27  An outer join returns all rows that satisfy the join condition and also returns some or all of those rows from one table for which no rows from the other satisfy the join condition.  To write a query that performs an outer join of tables A and B and returns all rows from A (a left outer join), use the LEFT [OUTER] JOIN syntax in the FROM clause, or apply the outer join operator (+) to all columns of B in the join condition in the WHERE clause. For all rows in A that have no matching rows in B, Oracle Database returns null for any select list expressions containing columns of B.

28  To write a query that performs an outer join of tables A and B and returns all rows from B (a right outer join), use the RIGHT [OUTER] JOIN syntax in the FROM clause, or apply the outer join operator (+) to all columns of A in the join condition in the WHERE clause. For all rows in B that have no matching rows in A, Oracle returns null for any select list expressions containing columns of A.  To write a query that performs an outer join and returns all rows from A and B, extended with nulls if they do not satisfy the join condition (a full outer join), use the FULL [OUTER] JOIN syntax in the FROM clause.

29  You cannot compare a column with a subquery in the WHERE clause of any outer join, regardless which form you specify.  You can use outer joins to fill gaps in sparse data. Such a join is called a partitioned outer join and is formed using the query_partition_clause of the join_clause syntax.  Sparse data is data that does not have rows for all possible values of a dimension such as time or department. For example, tables of sales data typically do not have rows for products that had no sales on a given date. Filling data gaps is useful in situations where data sparsity complicates analytic computation or where some data might be missed if the sparse data is queried directly.

30  Oracle recommends that you use the FROM clause OUTER JOIN syntax rather than the Oracle join operator. Outer join queries that use the Oracle join operator (+) are subject to the following rules and restrictions, which do not apply to the FROM clause OUTER JOIN syntax:  You cannot specify the (+) operator in a query block that also contains FROM clause join syntax.

31  The (+) operator can appear only in the WHERE clause or, in the context of left-correlation (when specifying the TABLE clause) in the FROM clause, and can be applied only to a column of a table or view.  If A and B are joined by multiple join conditions, then you must use the (+) operator in all of these conditions. If you do not, then Oracle Database will return only the rows resulting from a simple join, but without a warning or error to advise you that you do not have the results of an outer join.  The (+) operator does not produce an outer join if you specify one table in the outer query and the other table in an inner query.  You cannot use the (+) operator to outer-join a table to itself, although self joins are valid.

32  For example, the following statement is not valid:  However, the following self join is valid: SELECT employee_id, manager_id FROM employees WHERE employees.manager_id(+) = employees.employee_id; SELECT employee_id, manager_id FROM employees WHERE employees.manager_id(+) = employees.employee_id; SELECT e1.employee_id, e1.manager_id, e2.employee_id FROM employees e1, employees e2 WHERE e1.manager_id(+) = e2.employee_id ORDER BY e1.employee_id, e1.manager_id, e2.employee_id; SELECT e1.employee_id, e1.manager_id, e2.employee_id FROM employees e1, employees e2 WHERE e1.manager_id(+) = e2.employee_id ORDER BY e1.employee_id, e1.manager_id, e2.employee_id;

33  The (+) operator can be applied only to a column, not to an arbitrary expression. However, an arbitrary expression can contain one or more columns marked with the (+) operator.  A WHERE condition containing the (+) operator cannot be combined with another condition using the OR logical operator.  A WHERE condition cannot use the IN comparison condition to compare a column marked with the (+) operator with an expression.

34  In previous releases of Oracle Database, in a query that performed outer joins of more than two pairs of tables, a single table could be the null-generated table for only one other table. Beginning with Oracle Database 12c, a single table can be the null-generated table for multiple tables. For example, the following statement is allowed in Oracle Database 12c:  In this example, B, the null-generated table, is outer- joined to two tables, A and D. Refer to SELECT on page 19-4 for the syntax for an outer join. SELECT * FROM A, B, D WHERE A.c1 = B.c2(+) and D.c3 = B.c4(+); SELECT * FROM A, B, D WHERE A.c1 = B.c2(+) and D.c3 = B.c4(+);

35 SELECTtable1.column, table2.column FROMtable1, table2 WHEREtable1.column(+) = table2.column; SELECTtable1.column, table2.column FROMtable1, table2 WHEREtable1.column(+) = table2.column; SELECTtable1.column, table2.column FROMtable1, table2 WHEREtable1.column = table2.column(+); SELECTtable1.column, table2.column FROMtable1, table2 WHEREtable1.column = table2.column(+);

36 SELECTe.ename, d.deptno, d.dname FROMemp e, dept d WHEREe.deptno(+) = d.deptno ORDER BYe.deptno; ENAME DEPTNO DNAME KING 10 ACCOUNTING CLARK 10 ACCOUNTING OPERATIONS 15 rows selected.

37 6. Antijoins

38  An antijoin returns rows from the left side of the predicate for which there are no corresponding rows on the right side of the predicate.  It returns rows that fail to match (NOT IN) the subquery on the right side

39  The following example selects a list of employees who are not in a particular set of departments: SELECT * FROM emp WHERE deptno NOT IN (SELECT deptno FROM dept WHERE loc = ‘CHICAGO’) ORDER BY ename; SELECT * FROM emp WHERE deptno NOT IN (SELECT deptno FROM dept WHERE loc = ‘CHICAGO’) ORDER BY ename;

40 7. Semijoins

41  A semijoin returns rows that match an EXISTS subquery without duplicating rows from the left side of the predicate when multiple rows on the right side satisfy the criteria of the subquery.  Semijoin and antijoin transformation cannot be done if the subquery is on an OR branch of the WHERE clause.

42  In the following example, only one row needs to be returned from the departments table, even though many rows in the employees table might match the subquery. If no index has been defined on the salary column in employees, then a semijoin can be used to improve query performance.

43 SELECT * FROM dept WHERE EXISTS (SELECT * FROM emp WHERE dept.deptno = emp.deptno AND emp.sal > 2500) ORDER BY department_name; SELECT * FROM dept WHERE EXISTS (SELECT * FROM emp WHERE dept.deptno = emp.deptno AND emp.sal > 2500) ORDER BY department_name;


Download ppt "Chapter 4 JOINING TABLES & FUNCTION Lecture by Ty Rasmey"

Similar presentations


Ads by Google