displaying data from multiple tables

48
Displaying Data from Multiple Tables

Upload: damita

Post on 01-Feb-2016

36 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 equality and nonequality joins - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Displaying Data  from Multiple Tables

Displaying Data from Multiple Tables

Displaying Data from Multiple Tables

Page 2: Displaying Data  from Multiple Tables

ObjectivesObjectives

After 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 joins

View data that generally does not meet a join condition by using outer joins

Join a table to itself

After 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 joins

View data that generally does not meet a join condition by using outer joins

Join a table to itself

Page 3: Displaying Data  from Multiple Tables

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.

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 TablesObtaining Data from Multiple TablesEMP EMP DEPT DEPT EMPNO ENAME ... DEPTNO------ ----- ... ------ 7839 KING ... 10 7698 BLAKE ... 30 ... 7934 MILLER ... 10

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

Page 4: Displaying Data  from Multiple Tables

What Is a Join?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.

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.

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

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 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.

A 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.

Page 6: Displaying Data  from Multiple Tables
Page 7: Displaying Data  from Multiple Tables
Page 8: Displaying Data  from Multiple Tables

Generating a Cartesian ProductGenerating a Cartesian Product

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

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

EMP (14 rows) EMP (14 rows) DEPT (4 rows) DEPT (4 rows)

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

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

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

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

““CartesianCartesianproduct: product: 14*4=56 rows”14*4=56 rows”

Page 9: Displaying Data  from Multiple Tables

SQL> SELECT ename, dname

2 FROM emp, dept;

ENAME DNAME

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

KING ACCOUNTING

BLAKE ACCOUNTING

...

KING RESEARCH

BLAKE RESEARCH

...

56 rows selected.

Page 10: Displaying Data  from Multiple Tables
Page 11: Displaying Data  from Multiple Tables
Page 12: Displaying Data  from Multiple Tables
Page 13: Displaying Data  from Multiple Tables

Types of JoinsTypes of JoinsEquijoinEquijoin Non-equijoinNon-equijoin Outer joinOuter join Self joinSelf join

Types of Joins

There are two main types of join conditions:

Equijoins, Non-equijoins

Additional join methods include the following:

Outer joins, Self joins, Set operators

Page 14: Displaying Data  from Multiple Tables

What Is an EquijoinWhat Is an EquijoinEMP EMP DEPT 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 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.

Foreign keyForeign key Primary keyPrimary key

Page 15: Displaying Data  from Multiple Tables
Page 16: Displaying Data  from Multiple Tables
Page 17: Displaying Data  from Multiple Tables

Retrieving Records Retrieving Records

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

Page 18: Displaying Data  from Multiple Tables
Page 19: Displaying Data  from Multiple Tables

Qualifying Ambiguous Column NamesQualifying Ambiguous Column Names

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.

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.

Page 20: Displaying Data  from Multiple Tables

Additional Search ConditionsUsing the AND Operator Additional Search ConditionsUsing the AND Operator

EMP EMP DEPT 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 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.

Page 21: Displaying Data  from Multiple Tables

SQL> SELEC empno, ename, emp.deptno, loc

2 FROM emp, dept

3 WHERE emp.deptno = dept.deptno

4 AND INITCAP(ename) = 'King';

EMPNO ENAME DEPTNO LOC--------- ---------- --------- ------------- 7839 KING 10 NEW YORK

Page 22: Displaying Data  from Multiple Tables
Page 23: Displaying Data  from Multiple Tables
Page 24: Displaying Data  from Multiple Tables
Page 25: Displaying Data  from Multiple Tables

Using Table AliasesUsing Table AliasesSimplify queries by using table aliases.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;

Page 26: Displaying Data  from Multiple Tables

Joining More Than Two TablesJoining More Than Two Tables

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

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

CUSTOMER CUSTOMER

CUSTID ORDID------- ------- 101 610 102 611 104 612 106 601 102 602 106 604 106 605... 21 rows selected.

CUSTID ORDID------- ------- 101 610 102 611 104 612 106 601 102 602 106 604 106 605... 21 rows selected.

ORD ORD

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

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

ITEM ITEM

Page 27: Displaying Data  from Multiple Tables

SQL> SELECT c.name, o.ordid, i.itemid, i.itemtot, o.total

2 FROM customer c, ord o, item i

3 WHER c.custid = o.custid

4 AND o.ordid = i.ordid

5 AND c.name = 'TKB SPORT SHOP';

NAME ORDID ITEMID ITEMTOT TOTAL------------ --------- --------- --------- ---------TKB SPORT SHOP 610 3 58 101.4TKB SPORT SHOP 610 1 35 101.4TKB SPORT SHOP 610 2 8.4 101.4

example, to display the name, the orders placed, the item numbers, the total for each item, and the total for each order for customer TKB SPORT SHOP, you will have to join the CUSTOMER, ORD, and ITEM tables.

Page 28: Displaying Data  from Multiple Tables
Page 29: Displaying Data  from Multiple Tables
Page 30: Displaying Data  from Multiple Tables
Page 31: Displaying Data  from Multiple Tables

select e.first_name, e.salary, s.grade_idfrom employee e, salary_grade swhere e.salarybetween s.lower_bound and s.upper_bound;

select e.first_name, e.salary, s.grade_idfrom employee e, salary_grade swhere e.salarybetween s.lower_bound and s.upper_bound;

EMPEMP SALGRADESALGRADE

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

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 700 12002 1201 14003 1401 20004 2001 30005 3001 9999

Page 32: Displaying Data  from Multiple Tables

Non-Equijoins

The relationship between the EMP table and the SALGRADE table is a non-equijoin, meaning that no column in the EMP table corresponds directly to a column in the SALGRADE table. The relationship between the two tables is that the SAL column in the EMP table is between the LOSAL and HISAL column of the SALGRADE table. The relationship is obtained using an operator other than equal (=).

Page 33: Displaying Data  from Multiple Tables

Retrieving Records with Non-EquijoinsRetrieving Records with Non-Equijoins

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

SQL> SELECT e.ename, e.sal, s.grade

2 FROM emp e, salgrade s

3 WHERE e.sal

4 BETWEEN s.losal AND s.hisal;

Page 34: Displaying Data  from Multiple Tables

example

select e.first_name, e.salary, s.grade_id

from employee e, salary_grade s

where e.salary

between s.lower_bound and s.upper_bound;

Page 35: Displaying Data  from Multiple Tables
Page 36: Displaying Data  from Multiple Tables
Page 37: Displaying Data  from Multiple Tables
Page 38: Displaying Data  from Multiple Tables
Page 39: Displaying Data  from Multiple Tables

Outer JoinsOuter JoinsEMP EMP DEPT DEPT

No employee in theNo employee in theOPERATIONS departmentOPERATIONS department

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

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

Page 40: Displaying Data  from Multiple Tables

Outer JoinsOuter JoinsYou use an outer join to also see rows

that do not usually meet the join condition.

Outer join operator is the plus sign (+).

You use an outer join to also see rows that do not usually meet the join condition.

Outer join operator is the plus sign (+).SELECT table1.column, table2.column

FROM table1, table2

WHERE table1.column(+) = table2.column;

SELECT table1.column, table2.column

FROM table1, table2

WHERE table1.column(+) = table2.column;

SELECT table1.column, table2.column

FROM table1, table2

WHERE table1.column = table2.column(+);

SELECT table1.column, table2.column

FROM table1, table2

WHERE table1.column = table2.column(+);

Page 41: Displaying Data  from Multiple Tables

Using Outer JoinsUsing Outer JoinsSQL> 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 42: Displaying Data  from Multiple Tables
Page 43: Displaying Data  from Multiple Tables
Page 44: Displaying Data  from Multiple Tables
Page 45: Displaying Data  from Multiple Tables
Page 46: Displaying Data  from Multiple Tables

Self JoinsSelf JoinsEMP (WORKER)EMP (WORKER) EMP (MANAGER)EMP (MANAGER)

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

EMPNO ENAME MGR----- ------ ---- 7839 KING 7698 BLAKE 7839 7782 CLARK 7839 7566 JONES 7839 7654 MARTIN 7698 7499 ALLEN 7698

EMPNO ENAME----- --------

7839 KING 7839 KING 7839 KING 7698 BLAKE 7698 BLAKE

Page 47: Displaying Data  from Multiple Tables

Joining a Table to ItselfJoining a Table to Itself

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

WORKER.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;

Page 48: Displaying Data  from Multiple Tables

SummarySummary

EquijoinEquijoin Non-equijoinNon-equijoin Outer joinOuter join Self joinSelf join

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

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