displaying data from multiple tables

22
Displaying Data Displaying Data from Multiple from Multiple Tables Tables

Upload: holleb

Post on 19-Jan-2016

65 views

Category:

Documents


0 download

DESCRIPTION

Displaying Data from Multiple Tables. Objectives. After completing this lesson, you should Be able to do the following: Write SELECT statements to access data from more than one table using quality and nonequality joins View data that generally does not - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Displaying Data from Multiple Tables

Displaying DataDisplaying Datafrom Multiple from Multiple

TablesTables

Page 2: Displaying Data from Multiple Tables

ObjectivesObjectives

After completing this lesson, you After completing this lesson, you should should

Be able to do the following:Be able to do the following:• Write SELECT statements to accessWrite SELECT statements to access

data from more than one table usingdata from more than one table usingquality and nonequality joinsquality and nonequality joins

• View data that generally does notView data that generally does notjoin condition by using outer joinsjoin condition by using outer joins

• Join a table to itselfJoin a table to itself

Page 3: Displaying Data from Multiple Tables

Obtaining Data from Obtaining Data from Multiple tablesMultiple tables

EMPNO ENAME …… DEPTNO------------ -------------- ------ ------------ 7893 KING …… 10 7698 BLAKE …… 30 ……. 7934 MILLER …… 10

EMP DEPTNO DNAME LOC------------ -------------- -------------- 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON

DEPT

EMPNO DEPTNO LOC------------ -------------- -------------- 7893 10 NEW YORK 7698 30 CHICAGO 7782 10 NEW YORK 7566 20 DALLES 7654 30 CHICAGO 7499 30 CHICAGO …….14 rows selected.

Page 4: Displaying Data from Multiple Tables

What Is a join?What Is a join?

Use a join to query data from more than Use a join to query data from more than

one table.one table.

• Write the join condition in the WHERE Write the join condition in the WHERE clause.clause.

• Prefix the column name with the table name Prefix the column name with the table name when the same column name appears in when the same column name appears in more than one table.more than one table.

SELECT table1 . column , table2 . columnFROM table1 , table2WHERE table1 . column1 = table2 . Column2;

Page 5: Displaying Data from Multiple Tables

Cartesian ProductCartesian Product

• A Cartesian product is formed when:A Cartesian product is formed when:- A join condition is omitted- A join condition is omitted- A join condition is invalid- A join condition is invalid- All rows in the first table are joined - All rows in the first table are joined to to all rows in the second tableall rows in the second table

• To avoid a Cartesian product , alwaysTo avoid a Cartesian product , alwaysinclude a valid join condition in a include a valid join condition in a WHERE clause.WHERE clause.

Page 6: Displaying Data from Multiple Tables

Generating a Cartesian Generating a Cartesian ProductProduct

EMPNO ENAME …… DEPTNO------------ -------------- ------ ------------ 7893 KING …… 10 7698 BLAKE …… 30 ……. 7934 MILLER …… 10

EMP (14 rows) DEPTNO DNAME LOC------------ -------------- -------------- 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON

DEPT (4 rows)

ENAME DNAME ------------ -----------------KING ACCOUNTINGBLAKE ACCOUNTING…….KING RESEARCHBLAKE RESEARCH…….56 rows selected.

“Cartesian

Product:

14 * 4 = 56 rows”

Page 7: Displaying Data from Multiple Tables

Types of joinsTypes of joins

Equijoin Non-equijoin Outer join Self join

Page 8: Displaying Data from Multiple Tables

What Is an Equijoin?What Is an Equijoin?

EMPNO ENAME …… DEPTNO------------ -------------- ------ ------------ 7893 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.

EMP DEPTNO DNAMEE LOC------------ -------------- ----------------- 10 ACCOUNTING NEW YORK 30 SALES CHICAGO 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 30 SALES CHICAGO 30 SALES CHICAGO 30 SALES CHICAGO 30 SALES CHICAGO 20 RESEARCH DALLAS 20 RESEARCH DALLAS….14 rows selected.

DEPT

Foreign Key Primary Key

Page 9: Displaying Data from Multiple Tables

Retrieving Records Retrieving Records with Equijoinswith Equijoins

SQL> SELECT emp.empno , emp.ename , emp.deptno , dept.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.

Page 10: Displaying Data from Multiple Tables

Qualifying AmbiguousQualifying AmbiguousColumn NamesColumn Names

• Use table prefixes to qualify column Use table prefixes to qualify column names that are in multiple tables.names that are in multiple tables.

• Improve performance by using table Improve performance by using table prefixes.prefixes.

• Distinguish columns that have Distinguish columns that have identical names but reside in identical names but reside in different tables by using column different tables by using column aliases.aliases.

Page 11: Displaying Data from Multiple Tables

Additional Search Additional Search ConditionsConditions

Using the AND OperatorUsing the AND Operator EMPNO ENAME …… DEPTNO------------ -------------- ------ ------------ 7893 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.

EMP DEPTNO DNAMEE LOC------------ -------------- ----------------- 10 ACCOUNTING NEW YORK 30 SALES CHICAGO 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 30 SALES CHICAGO 30 SALES CHICAGO 30 SALES CHICAGO 30 SALES CHICAGO 20 RESEARCH DALLAS 20 RESEARCH DALLAS….14 rows selected.

DEPT

Page 12: Displaying Data from Multiple Tables

Using Table AliasesUsing Table AliasesSimplify queries by using table Simplify queries by using table

aliases.aliases.

SQL> SELECT emp.empno , emp.ename , emp.deptno , dept.deptno , dept.loc 3 FROM emp, dept 4 WHERE emp.deptno=dept.deptno;

SQL> SELECT e.empno , e.ename , e.deptno , d.deptno , d.loc

3 FROM emp e, dept d 4 WHERE e.deptno = d.deptno;

Page 13: Displaying Data from Multiple Tables

Joining More Than Two Joining More Than Two TablesTables

NAME CUSTID------------------------- -------------- JOCKSPORTS 100 TKB SPORT SHOP 101VOLLYRITE 102JUST TENNIS 103K+T SPORTS 105SHAPE UP 106WOMENS SPORTS 107….9 rows selected.

CUSTID ORDID ----------------- --------------

101 610102 611104 612106 601102 602106106

….21 rows selected.

CUSTOMER ORD

ORDID ITEMID ---------- -------------- 610 3 611 1 612 1 601 1 602 1….64 rows selected.

Page 14: Displaying Data from Multiple Tables

Non-EquijoinsNon-Equijoins

EMPNO ENAME SAL---------- ---------- ----------7839 KING 50007698 BLAKE 28507782 CLARK 24507566 JONES 29757654 MARTIN 12507499 ALLEN 16007844 TURNER 1500 7900 JAMES 950….14 rows selected.

EMP

GRADE LOSAL HISAL---------- ---------- ----------1 700 12002 1201 14003 1401 20004 2001 30005 3001 9999

“salary in the EMP table is between low salary and high salary in the SALGRADE table”

SALGRADE

Page 15: Displaying Data from Multiple Tables

Retrieving RecordsRetrieving Recordswith Non-Equijoinswith Non-Equijoins

SQL> SELECT e.ename , e.sal , s.grade 2 FROM emp e , salgrade s 4 WHERE e.sal 5 BETWEEN s.losal AND s.hisal ;

ENAME SAL GRADE------------------ ---------- ---------------- JAMES 950 1SMITH 800 1ADAMS 1100 1….14 rows selected.

Page 16: Displaying Data from Multiple Tables

Outer JoinsOuter Joins

ENAME DEPTNO------------- -------------- KING 10 BLAKE 30CLARK 10JONES 20 ….

EMP

DEPTNO DNAME------------- -------------- 10 ACCOUNTING30 SALES10 ACCOUNTING20 RESEARCH….40 OPERATIONS

DEPT

No employee in the

OPERATIONS department

Page 17: Displaying Data from Multiple Tables

Outer JoinsOuter Joins• You use an outer join to also see rows that You use an outer join to also see rows that

do not usually meet the join condition.do not usually meet the join condition.• Outer join operation is the plus sign (+).Outer join operation is the plus sign (+).

SELECT table1.column , table2.columnFROM table1 , table2WHERE table1 . column(+) = table2.column;

SELECT table1.column , table2.columnFROM table1 , table2WHERE table1 . column = table2.column(+);

Page 18: Displaying Data from Multiple Tables

Using Outer joinsUsing Outer joins

SQL> SELECT e.ename , d.deptno , d.dname 2 FROM emp e , dept d 3 WHERE e.deptno(+) = d.deptno 4 ORDER BY e.deptno;

ENAME DEPTNO DNAME------------------ --------------- -------------------- KING 10 ACCOUNTINGCLARK 10 ACCOUNTING….

40 OPERATIONS15 rows selected.

Page 19: Displaying Data from Multiple Tables

Self JoinsSelf Joins

EMPNO ENAME MGR---------- ---------- ----------7839 KING 7698 BLAKE 78397782 CLARK 78397566 JONES 78397654 MARTIN 76987499 ALLEN 7698

EMP ( WORKER )

EMPNO ENAME

---------- ----------

7839 KING7839 KING7839 KING7698 BLAKE7698 BLAKE

EMP ( MANAGER )

“MGR in the WORKER table is equal to EMPNO in the

MANAGER table”

Page 20: Displaying Data from Multiple Tables

Joining a Table to ltselfJoining a Table to ltself

SQL> SELECT worker.ename | | ‘ works for ‘ | | manager.ename 2 FROM emp worker , emp manager 3 WHERE worker.mgr = manager.empno;

WORKER.ENAME | | ‘WORKSFOR’ | | MANAG---------------------------------------------------------------------- BLAKE works for KINGCLARK works for KINGJONES works for KINGMARTIN works for BLAKE….13 rows selected.

Page 21: Displaying Data from Multiple Tables

SummarySummary

SELECT table1.column , table2.columnFROM table1 , table2WHERE table1 , column1 = table2.column2;

Equijoin Non-equijoin Outer join Self join

Page 22: Displaying Data from Multiple Tables

Practice OverviewPractice Overview

• Joinning tables using an equijoinJoinning tables using an equijoin• Performing outer and self joinsPerforming outer and self joins• Adding conditionsAdding conditions