Displaying Data from Multiple Tables<br />
Objectives<br />After completing this lesson, you should be able to do the following:<br />Write SELECT statements to acce...
Obtaining Data from Multiple Tables<br />EMP <br />DEPT <br /> EMPNOENAME	...	DEPTNO------	-----	...	------7839KING	...	  ...
What Is a Join?<br />Use a join to query data from more than one table.<br />Write the join condition in the WHERE clause....
Cartesian Product<br />A Cartesian product is formed when:<br />A join condition is omitted<br />A join condition is inval...
Generating a Cartesian Product<br />“Cartesianproduct: 14*4=56 rows”<br />EMP (14 rows) <br />DEPT (4 rows) <br /> EMPNOEN...
Types of Joins<br />Equijoin<br />Non-equijoin<br />Outer join<br />Self join<br />
What Is an Equijoin?<br />Foreign key<br />Primary key<br />EMP <br />DEPT <br /> EMPNO ENAME    DEPTNO<br />------ ------...
Retrieving Records with Equijoins<br />SQL&gt; SELECT emp.empno, emp.ename, emp.deptno,2dept.deptno, dept.loc<br />3  FROM...
Qualifying Ambiguous Column Names<br />Use table prefixes to qualify column names that are in multiple tables.<br />Improv...
Additional Search ConditionsUsing the AND Operator <br />EMP <br />DEPT <br /> EMPNO ENAME    DEPTNO<br />------ ------- -...
Using Table Aliases<br />Simplify queries by using table aliases.<br />SQL&gt; SELECT emp.empno, emp.ename, emp.deptno,  <...
Joining More Than Two Tables<br />ORD <br /> CUSTID   ORDID<br />------- -------<br />101610<br />102611<br />104612<br />...
Non-Equijoins<br />“salary in the EMP <br />table is between <br />low salary and high <br />salary in the SALGRADE<br />t...
Retrieving Records with Non-Equijoins<br />SQL&gt; SELECT e.ename, e.sal, s.grade<br />2FROMemp e, salgrade s<br />3WHERE ...
Outer Joins<br />No employee in theOPERATIONS department<br />EMP <br />DEPT <br />ENAMEDEPTNO-----	------KING10<br />BLAK...
Outer Joins<br />You use an outer join to also see rows that do not usually meet the join condition.<br />Outer join opera...
Using Outer Joins<br />SQL&gt; SELECTe.ename, d.deptno, d.dname<br />2  FROMemp e, dept d<br />3  WHEREe.deptno(+) = d.dep...
Self Joins<br />“MGR in the WORKER table is equal to EMPNO in the MANAGER table”<br />EMP (WORKER)<br />EMP (MANAGER)<br /...
Joining a Table to Itself<br />SQL&gt; SELECT worker.ename||&apos; works for &apos;||manager.ename<br />2  FROM emp worker...
Summary<br />SELECTtable1.column, table2.column<br />FROMtable1, table2<br />WHEREtable1.column1= table2.column2;<br />Equ...
Practice Overview<br />Joining tables using an equijoin<br />Performing outer and self joins<br />Adding conditions<br />
Upcoming SlideShare
Loading in …5
×

Les04 Displaying Data From Multiple Table

3,947 views
3,789 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,947
On SlideShare
0
From Embeds
0
Number of Embeds
17
Actions
Shares
0
Downloads
362
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Les04 Displaying Data From Multiple Table

  1. 1. Displaying Data from Multiple Tables<br />
  2. 2. Objectives<br />After completing this lesson, you should be able to do the following:<br />Write SELECT statements to access data from more than one table using equality and nonequality joins<br />View data that generally does not meet a join condition by using outer joins<br />Join a table to itself<br />
  3. 3. Obtaining Data from Multiple Tables<br />EMP <br />DEPT <br /> EMPNOENAME ... DEPTNO------ ----- ... ------7839KING ... 10<br />7698BLAKE ... 30<br /> ... <br />7934MILLER ... 10<br />DEPTNO DNAME LOC <br />------ ---------- --------<br />10ACCOUNTINGNEW YORK<br />20RESEARCHDALLAS<br />30SALESCHICAGO<br />40OPERATIONSBOSTON<br />EMPNO DEPTNO LOC<br />----- ------- -------- <br />783910 NEW YORK<br />769830 CHICAGO<br />778210 NEW YORK<br />756620 DALLAS<br />765430 CHICAGO<br />749930 CHICAGO<br />...<br />14 rows selected.<br />
  4. 4. What Is a Join?<br />Use a join to query data from more than one table.<br />Write the join condition in the WHERE clause.<br />Prefix the column name with the table name when the same column name appears in more than one table.<br />SELECTtable1.column, table2.column<br />FROMtable1, table2<br />WHEREtable1.column1= table2.column2;<br />
  5. 5. Cartesian Product<br />A Cartesian product is formed when:<br />A join condition is omitted<br />A join condition is invalid<br />All rows in the first table are joined to all rows in the second table<br />To avoid a Cartesian product, always include a valid join condition in a WHERE clause.<br />
  6. 6. Generating a Cartesian Product<br />“Cartesianproduct: 14*4=56 rows”<br />EMP (14 rows) <br />DEPT (4 rows) <br /> EMPNOENAME ... DEPTNO------ ----- ... ------7839KING ... 10<br />7698BLAKE ... 30<br /> ... <br />7934MILLER ... 10<br />DEPTNO DNAME LOC <br />------ ---------- --------<br />10ACCOUNTINGNEW YORK<br />20RESEARCHDALLAS<br />30SALESCHICAGO<br />40OPERATIONSBOSTON<br />ENAME DNAME<br />------ ----------<br />KINGACCOUNTING<br />BLAKEACCOUNTING <br />...<br />KINGRESEARCH<br />BLAKERESEARCH<br />...<br />56 rows selected.<br />
  7. 7. Types of Joins<br />Equijoin<br />Non-equijoin<br />Outer join<br />Self join<br />
  8. 8. What Is an Equijoin?<br />Foreign key<br />Primary key<br />EMP <br />DEPT <br /> EMPNO ENAME DEPTNO<br />------ ------- -------<br />7839 KING 10<br />7698 BLAKE 30<br />7782 CLARK 10<br />7566 JONES 20<br />7654 MARTIN 30<br />7499 ALLEN 30<br />7844 TURNER 30<br />7900 JAMES 30<br />7521 WARD 30<br />7902 FORD 20<br />7369 SMITH 20<br />...<br />14 rows selected.<br /> DEPTNO DNAME LOC <br />------- ---------- --------<br />10 ACCOUNTING NEW YORK<br />30 SALESCHICAGO<br />10 ACCOUNTINGNEW YORK <br />20 RESEARCHDALLAS<br />30 SALESCHICAGO<br />30 SALESCHICAGO<br />30 SALESCHICAGO<br />30 SALESCHICAGO<br />30 SALESCHICAGO<br />20 RESEARCHDALLAS<br />20 RESEARCHDALLAS<br />...<br />14 rows selected.<br />
  9. 9. Retrieving Records with Equijoins<br />SQL&gt; SELECT emp.empno, emp.ename, emp.deptno,2dept.deptno, dept.loc<br />3 FROM emp, dept<br />4 WHERE emp.deptno=dept.deptno;<br />EMPNO ENAME DEPTNO DEPTNO LOC<br />----- ------ ------ ------ ---------<br />7839 KING1010 NEW YORK<br />7698 BLAKE 3030 CHICAGO<br />7782 CLARK1010 NEW YORK<br />7566 JONES 2020 DALLAS<br />...<br />14 rows selected.<br />
  10. 10. Qualifying Ambiguous Column Names<br />Use table prefixes to qualify column names that are in multiple tables.<br />Improve performance by using table prefixes.<br />Distinguish columns that have identical names but reside in different tables by using column aliases.<br />
  11. 11. Additional Search ConditionsUsing the AND Operator <br />EMP <br />DEPT <br /> EMPNO ENAME DEPTNO<br />------ ------- -------<br />7839 KING 10<br />7698 BLAKE 30<br />7782 CLARK 10<br />7566 JONES 20<br />7654 MARTIN 30<br />7499 ALLEN 30<br />7844 TURNER 30<br />7900 JAMES 30<br />7521 WARD 30<br />7902 FORD 20<br />7369 SMITH 20<br />...<br />14 rows selected.<br />DEPTNO DNAME LOC <br />------ --------- --------<br />10 ACCOUNTINGNEW YORK<br />30SALES CHICAGO<br />10 ACCOUNTINGNEW YORK <br />20 RESEARCHDALLAS<br />30 SALES CHICAGO<br />30 SALES CHICAGO<br />30 SALES CHICAGO<br />30 SALES CHICAGO<br />30 SALES CHICAGO<br />20 RESEARCHDALLAS<br />20 RESEARCHDALLAS<br />...<br />14 rows selected.<br />
  12. 12. Using Table Aliases<br />Simplify queries by using table aliases.<br />SQL&gt; SELECT emp.empno, emp.ename, emp.deptno, <br />2 dept.deptno, dept.loc<br />3 FROM emp, dept<br />4 WHERE emp.deptno=dept.deptno;<br />SQL&gt; SELECT e.empno, e.ename, e.deptno, <br />2 d.deptno, d.loc<br />3 FROM emp e, dept d<br />4 WHERE e.deptno=d.deptno;<br />
  13. 13. Joining More Than Two Tables<br />ORD <br /> CUSTID ORDID<br />------- -------<br />101610<br />102611<br />104612<br />106601<br />102602<br />106604<br />106605<br />... <br />21 rows selected.<br />ITEM <br /> ORDID ITEMID<br />------ -------<br />6103<br />6111<br />6121<br />6011<br />6021<br />...<br />64 rows selected. <br />CUSTOMER <br />NAMECUSTID<br />----------- ------<br />JOCKSPORTS100<br />TKB SPORT SHOP101<br />VOLLYRITE102<br />JUST TENNIS103<br />K+T SPORTS105<br />SHAPE UP106<br />WOMENS SPORTS 107<br />... ...<br />9 rows selected.<br />
  14. 14. Non-Equijoins<br />“salary in the EMP <br />table is between <br />low salary and high <br />salary in the SALGRADE<br />table”<br />EMP<br />SALGRADE<br /> EMPNO ENAME SAL<br />------ ------- ------<br />7839 KING 5000<br />7698 BLAKE 2850<br />7782 CLARK 2450<br />7566 JONES 2975<br />7654 MARTIN 1250<br />7499 ALLEN 1600<br />7844 TURNER 1500<br />7900 JAMES 950<br />...<br />14 rows selected.<br />GRADE LOSAL HISAL<br />----- ----- ------<br />17001200<br />212011400<br />314012000<br />420013000<br />530019999<br />
  15. 15. Retrieving Records with Non-Equijoins<br />SQL&gt; SELECT e.ename, e.sal, s.grade<br />2FROMemp e, salgrade s<br />3WHERE e.sal<br />4BETWEEN s.losal AND s.hisal;<br />ENAME SAL GRADE<br />---------- --------- ---------<br />JAMES 9501<br />SMITH 8001<br />ADAMS 11001<br />...<br />14 rows selected.<br />
  16. 16. Outer Joins<br />No employee in theOPERATIONS department<br />EMP <br />DEPT <br />ENAMEDEPTNO----- ------KING10<br />BLAKE30<br />CLARK10<br />JONES20<br />... <br />DEPTNO DNAME<br />------ ----------<br />10ACCOUNTING<br />30SALES<br />10ACCOUNTING<br />20RESEARCH<br />... <br />40OPERATIONS<br />
  17. 17. Outer Joins<br />You use an outer join to also see rows that do not usually meet the join condition.<br />Outer join operator is the plus sign (+).<br />SELECTtable1.column, table2.column<br />FROMtable1, table2<br />WHEREtable1.column(+)= table2.column;<br />SELECTtable1.column, table2.column<br />FROMtable1, table2<br />WHEREtable1.column = table2.column(+);<br />
  18. 18. Using Outer Joins<br />SQL&gt; SELECTe.ename, d.deptno, d.dname<br />2 FROMemp e, dept d<br />3 WHEREe.deptno(+) = d.deptno<br />4 ORDER BYe.deptno;<br />ENAME DEPTNO DNAME<br />---------- --------- -------------<br />KING 10 ACCOUNTING<br />CLARK 10 ACCOUNTING<br />...<br />40 OPERATIONS<br />15 rows selected.<br />
  19. 19. Self Joins<br />“MGR in the WORKER table is equal to EMPNO in the MANAGER table”<br />EMP (WORKER)<br />EMP (MANAGER)<br />EMPNOENAME MGR----- ------ ----7839KING<br />7698BLAKE7839<br />7782CLARK7839<br />7566JONES7839<br />7654MARTIN7698<br />7499ALLEN7698<br />EMPNOENAME----- --------<br />7839KING<br />7839KING<br />7839KING<br />7698BLAKE<br />7698BLAKE<br />
  20. 20. Joining a Table to Itself<br />SQL&gt; SELECT worker.ename||&apos; works for &apos;||manager.ename<br />2 FROM emp worker, emp manager<br />3 WHERE worker.mgr = manager.empno;<br />WORKER.ENAME||&apos;WORKSFOR&apos;||MANAG<br />-------------------------------<br />BLAKE works for KING<br />CLARK works for KING<br />JONES works for KING<br />MARTIN works for BLAKE<br />...<br />13 rows selected.<br />
  21. 21. Summary<br />SELECTtable1.column, table2.column<br />FROMtable1, table2<br />WHEREtable1.column1= table2.column2;<br />Equijoin<br />Non-equijoin<br />Outer join<br />Self join<br />
  22. 22. Practice Overview<br />Joining tables using an equijoin<br />Performing outer and self joins<br />Adding conditions<br />

×