displaying data from multiple tables (join) displaying data from multiple tables (join) displaying...

Download Displaying Data from Multiple Tables (Join) Displaying Data from Multiple Tables (Join) Displaying Data from Multiple Tables (Join Displaying Data from

Post on 13-Jan-2016

221 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

  • Displaying Data from Multiple Tables (Join)Sub queriesCreating SequencesCreating ViewsDynamic SQL QueriesControlling User AccessCH3 Part2

  • Displaying Data from Multiple Tables (Join)

  • ObjectivesAfter completing this lesson, you should be able to do the following:Write SELECT statements to access data from more than one table using equality and nonequality joinsView data that generally does not meet a join condition by using outer joinsJoin a table to itself

  • EMPNO DEPTNO LOC----- ------- -------- 7839 10 NEW YORK 7698 30 CHICAGO 7782 10 NEW YORK 7566 20 DALLAS 7654 30 CHICAGO 7499 30 CHICAGO...14 rows selected.Obtaining Data from Multiple TablesEMP DEPT EMPNOENAME...DEPTNO -----------...------ 7839KING... 10 7698BLAKE... 30 ... 7934MILLER... 10DEPTNO DNAME LOC ------ ------------------ 10ACCOUNTINGNEW YORK 20RESEARCHDALLAS 30SALESCHICAGO 40OPERATIONSBOSTON

  • What Is a Join?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.columnFROMtable1, table2WHEREtable1.column1 = table2.column2;

  • Cartesian ProductA Cartesian product is formed when:A join condition is omittedA join condition is invalidAll rows in the first table are joined to all rows in the second tableTo avoid a Cartesian product, always include a valid join condition in a WHERE clause.

  • Generating a Cartesian ProductENAME DNAME------ ----------KINGACCOUNTINGBLAKE ACCOUNTING ...KINGRESEARCHBLAKE RESEARCH...56 rows selected.EMP (14 rows) DEPT (4 rows) EMPNOENAME...DEPTNO -----------...------ 7839KING... 10 7698BLAKE... 30 ... 7934MILLER... 10DEPTNO DNAME LOC ------ ------------------ 10ACCOUNTINGNEW YORK 20RESEARCHDALLAS 30SALESCHICAGO 40OPERATIONSBOSTON

  • Types of JoinsEquijoinNon-equijoinOuter joinSelf join

  • What Is an Equijoin?EMP DEPT EMPNO ENAME DEPTNO------ ------- ------- 7839 KING 10 7698 BLAKE 30 7782 CLARK 10 7566 JONES 20 7654 MARTIN 30 7499 ALLEN 30 7844 TURNER 30 7900 JAMES 30 7521 WARD 30 7902 FORD 20 7369 SMITH 20...14 rows selected. DEPTNO DNAME LOC ------- ---------- -------- 10 ACCOUNTING NEW YORK 30 SALES CHICAGO 10 ACCOUNTINGNEW YORK 20 RESEARCHDALLAS 30 SALES CHICAGO 30 SALES CHICAGO 30 SALES CHICAGO 30 SALES CHICAGO 30 SALES CHICAGO 20 RESEARCHDALLAS 20 RESEARCHDALLAS...14 rows selected.

  • Retrieving Records with EquijoinsSQL> 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----- ------ ------ ------ --------- 7839 KING 10 10 NEW YORK 7698 BLAKE 30 30 CHICAGO 7782 CLARK 10 10 NEW YORK 7566 JONES 20 20 DALLAS...14 rows selected.

  • Qualifying Ambiguous Column NamesUse 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.

  • Additional Search ConditionsUsing the AND Operator EMP DEPT EMPNO ENAME DEPTNO------ ------- ------- 7839 KING 10 7698 BLAKE 30 7782 CLARK 10 7566 JONES 20 7654 MARTIN 30 7499 ALLEN 30 7844 TURNER 30 7900 JAMES 30 7521 WARD 30 7902 FORD 20 7369 SMITH 20...14 rows selected.DEPTNO DNAME LOC ------ ----------------- 10 ACCOUNTINGNEW YORK 30SALES CHICAGO 10 ACCOUNTINGNEW YORK 20 RESEARCHDALLAS 30 SALES CHICAGO 30 SALES CHICAGO 30 SALES CHICAGO 30 SALES CHICAGO 30 SALES CHICAGO 20 RESEARCHDALLAS 20 RESEARCHDALLAS...14 rows selected.

  • Using Table AliasesSimplify 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;

  • Joining More Than Two TablesNAMECUSTID-----------------JOCKSPORTS 100TKB SPORT SHOP 101VOLLYRITE 102JUST TENNIS 103K+T SPORTS 105SHAPE UP 106WOMENS SPORTS 107......9 rows selected.CUSTOMER

  • Non-EquijoinsEMPSALGRADE EMPNO ENAME SAL------ ------- ------ 7839 KING 5000 7698 BLAKE 2850 7782 CLARK 2450 7566 JONES 2975 7654 MARTIN 1250 7499 ALLEN 1600 7844 TURNER 1500 7900 JAMES 950...14 rows selected.GRADE LOSAL HISAL----- ----- ------1 70012002 120114003 140120004200130005 30019999

  • Retrieving Records with Non-EquijoinsENAME SAL GRADE---------- --------- ---------JAMES 950 1SMITH 800 1ADAMS 1100 1...14 rows selected.SQL> SELECT e.ename, e.sal, s.grade 2FROMemp e, salgrade s 3WHERE e.sal 4BETWEEN s.losal AND s.hisal;

  • Outer JoinsEMP DEPT ENAMEDEPTNO ----------- KING10BLAKE30CLARK10JONES20...

    DEPTNO DNAME------ ----------10 ACCOUNTING30 SALES10 ACCOUNTING20RESEARCH...40OPERATIONS

  • Outer JoinsYou use an outer join to also see rows that do not usually meet the join condition.Outer join operator is the plus sign (+).

    SELECTtable1.column, table2.columnFROMtable1, table2WHEREtable1.column(+) = table2.column;SELECTtable1.column, table2.columnFROMtable1, table2WHEREtable1.column = table2.column(+);

  • Using Outer JoinsSQL> SELECTe.ename, d.deptno, d.dname 2 FROMemp e, dept d 3 WHEREe.deptno(+) = d.deptno 4 ORDER BYe.deptno;ENAME DEPTNO DNAME---------- --------- -------------KING 10 ACCOUNTINGCLARK 10 ACCOUNTING... 40 OPERATIONS15 rows selected.

  • Self JoinsEMP (WORKER)EMP (MANAGER)EMPNOENAME MGR --------------- 7839KING 7698BLAKE7839 7782CLARK7839 7566JONES7839 7654MARTIN7698 7499ALLEN7698EMPNOENAME ------------- 7839KING 7839KING 7839KING 7698BLAKE 7698BLAKE

  • Joining a Table to ItselfWORKER.ENAME||'WORKSFOR'||MANAG-------------------------------BLAKE works for KINGCLARK works for KINGJONES works for KINGMARTIN works for BLAKE...13 rows selected.SQL> SELECT worker.ename||' works for '||manager.ename 2 FROM emp worker, emp manager 3 WHERE worker.mgr = manager.empno;

  • SummaryEquijoinNon-equijoinOuter joinSelf joinSELECTtable1.column, table2.columnFROMtable1, table2WHEREtable1.column1 = table2.column2;

  • Sub queries

  • ObjectivesAfter completing this lesson, you should be able to do the following:Describe the types of problems that subqueries can solveDefine subqueriesList the types of subqueriesWrite single-row and multiple-row subqueries

  • Using a Subquery to Solve a ProblemWho has a salary greater than Jones?Which employees have a salary greater than Jones salary?Main Query?What is Jones salary??Subquery

  • SubqueriesThe subquery (inner query) executes once before the main query.The result of the subquery is used by the main query (outer query).SELECTselect_listFROMtableWHEREexpr operator (SELECTselect_list FROMtable);

  • SQL> SELECT ename 2 FROM emp 3 WHERE sal > 4 (SELECT sal 5 FROM emp 6 WHERE empno=7566);

    Using a SubqueryENAME----------KINGFORDSCOTT

  • Guidelines for Using SubqueriesEnclose subqueries in parentheses. Place subqueries on the right side of the comparison operator.Do not add an ORDER BY clause to a subquery.Use single-row operators with single-row subqueries.Use multiple-row operators with multiple-row subqueries.

  • Types of SubqueriesSingle-row subqueryCLERK

  • Single-Row SubqueriesReturn only one rowUse single-row comparison operatorsOperator=> >=<
  • Executing Single-Row SubqueriesENAME JOB---------- ---------MILLER CLERKSQL> SELECT ename, job 2 FROM emp 3 WHERE job = 4(SELECT job 5 FROM emp 6 WHERE empno = 7369) 7 AND sal > 8(SELECT sal 9FROMemp 10WHEREempno = 7876);

  • Using Group Functions in a SubqueryENAME JOB SAL---------- --------- ---------SMITH CLERK 800SQL> SELECTename, job, sal 2 FROMemp 3 WHEREsal = 4(SELECTMIN(sal) 5FROMemp);

  • HAVING Clause with SubqueriesThe Oracle Server executes subqueries first.The Oracle Server returns results into the HAVING clause of the main query.SQL> SELECTdeptno, MIN(sal) 2 FROMemp 3 GROUP BYdeptno 4 HAVINGMIN(sal) > 5(SELECTMIN(sal) 6FROMemp 7WHEREdeptno = 20);

  • What Is Wrong with This Statement?ERROR:ORA-01427: single-row subquery returns more than one row

    no rows selectedSQL> SELECT empno, ename 2 FROM emp 3 WHERE sal = 4(SELECT MIN(sal) 5FROM emp 6GROUP BY deptno);Single-row operator with multiple-row subquery

  • Will This Statement Work?no rows selectedSubquery returns no valuesSQL> SELECT ename, job 2 FROM emp 3 WHERE job = 4(SELECTjob 5FROMemp 6WHEREename='SMYTHE');

  • Multiple-Row SubqueriesReturn more than one rowUse multiple-row comparison operatorsOperator IN ANY ALLMeaningEqual to any member in the listCompare value to each value returned by the subquery Compare value to every value returned by the subquery

  • Using ANY Operator in Multiple-Row Subqueries EMPNO ENAM

Recommended

View more >