You are on page 1of 41

Displaying Data

from Multiple Tables


(Join)
Obtaining
Obtaining Data
Data from
from
EMP DEPT
Multiple
Multiple
EMPNO ENAME ... DEPTNO
------ ----- ... ------
Tables
Tables
DEPTNO DNAME LOC
------ ---------- --------
7839 KING ... 10 10 ACCOUNTING NEW YORK
7698 BLAKE ... 30 20 RESEARCH DALLAS
... 30 SALES CHICAGO
7934 MILLER ... 10 40 OPERATIONS BOSTON

EMPNO
EMPNO DEPTNO
DEPTNO LOC
LOC
----- ------- --------
----- ------- --------
7839
7839 10
10 NEW
NEW YORK
YORK
7698
7698 30 CHICAGO
30 CHICAGO
7782
7782 10
10 NEW
NEW YORK
YORK
7566
7566 20 DALLAS
20 DALLAS
7654
7654 30
30 CHICAGO
CHICAGO
7499
7499 30 CHICAGO
30 CHICAGO
...
...
14
14 rows
rows selected.
selected.
What
What Is
Is a
a Join?
Join?

 Use
Use aa join
join to
to query
query data
data from
from
more
more than
than one
one table.
table.
SELECT
SELECT table1.column,
table1.column, table2.column
table2.column
FROM
FROM table1,
table1, table2
table2
WHERE
WHERE table1.column1
table1.column1 == table2.column2;
table2.column2;

 Write
 Write the
the join
join condition
condition in
in the
the
WHERE
WHERE clause.
clause.
 Prefix
 Prefix the
the column
column name
name with
with the
the
table
table name
name when
when the
the same
same column
column
name
name appears
appears in
in more
more than
than one
one
table.
table.
Cartesian
Cartesian Product
Product

A
 A Cartesian
Cartesian product
product is
is formed
formed
when:
when:
A
 A join
join condition
condition is
is omitted
omitted
A
 A join
join condition
condition is
is invalid
invalid
 All
 All rows
rows in
in the
the first
first table
table are
are joined
joined to
to
all
all rows
rows in
in the
the second
second table
table
 To
 To avoid
avoid aa Cartesian
Cartesian product,
product,
always
always include
include aa valid
valid join
join
condition
condition in
in aa WHERE
WHERE clause.
clause.
Generating
Generating a a Cartesian
Cartesian
EMP (14 rows)
EMPNO ENAME ...
Product
Product
DEPTNO
DEPT (4 rows)
EMPNO ENAME ... DEPTNO DEPTNO
DEPTNO DNAME
DNAME LOC
LOC
------
------ -----
----- ...
... ------
------ ------
------ ----------
---------- --------
--------
7839
7839 KING
KING ...
... 10
10 10
10 ACCOUNTING
ACCOUNTING NEW
NEW YORK
YORK
7698
7698 BLAKE
BLAKE ...
... 30
30 20
20 RESEARCH
RESEARCH DALLAS
DALLAS
...
... 30
30 SALES
SALES CHICAGO
CHICAGO
7934
7934 MILLER
MILLER ...
... 10
10 40
40 OPERATIONS
OPERATIONS BOSTON
BOSTON

ENAME
ENAME DNAME
DNAME
------
------ ----------
----------
KING
KING ACCOUNTING
ACCOUNTING
“Cartesian BLAKE
BLAKE ACCOUNTING
ACCOUNTING
product: ...
...
KING
KING RESEARCH
RESEARCH
14*4=56 rows” BLAKE RESEARCH
BLAKE RESEARCH
...
...
56
56 rows
rows selected.
selected.
Types
Types of
of Joins
Joins

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

Foreign key Primary key


Retrieving
Retrieving Records
Records
with
with Equijoins
Equijoins
SQL> SELECT emp.empno, emp.ename, emp.deptno,
2 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.
Additional
Additional Search Search
Conditions
Conditions
Using
Using
EMP
the
EMPNO ENAME the
AND
AND
DEPTNO
Operator
DEPTNO Operator
DEPT
DNAME LOC
------ ------- ------- ------ --------- --------
7839 KING 10 10 ACCOUNTING NEW YORK
7698 BLAKE 30 30 SALES CHICAGO
7782 CLARK 10 10 ACCOUNTING NEW YORK
7566 JONES 20 20 RESEARCH DALLAS
7654 MARTIN 30 30 SALES CHICAGO
7499 ALLEN 30 30 SALES CHICAGO
7844 TURNER 30 30 SALES CHICAGO
7900 JAMES 30 30 SALES CHICAGO
7521 WARD 30 30 SALES CHICAGO
7902 FORD 20 20 RESEARCH DALLAS
7369 SMITH 20 20 RESEARCH DALLAS
... ...
14 rows selected. 14 rows selected.
Using
Using Table
Table Aliases
Aliases

 Simplify
Simplify queries
queries byby using
using table
table
aliases.
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
Joining MoreMore Than Than TwoTwo
CUSTOMER Tables
Tables
ORD
NAME
NAME CUSTID CUSTID ORDID
CUSTID CUSTID ORDID
-----------
----------- ------
------ -------
------- -------
-------
JOCKSPORTS
JOCKSPORTS 100
100 101
101 610
610
TKB
TKB SPORT
SPORT SHOP
SHOP 101
101 102
102 611
611
VOLLYRITE
VOLLYRITE 102
102 104
104 612
612
JUST TENNIS
JUST TENNIS 103
103 106
106 601
601
K+T SPORTS
K+T SPORTS 105
105 102
102 602
602 ITEM
SHAPE UP
SHAPE UP 106
106 106
106 604
604 ITEMID
ORDID
ORDID ITEMID
WOMENS
WOMENS SPORTS
SPORTS 107
107 106
106 ------605
605 -------
... ... ... ------ -------
... ... ... 610 33
99 rows 610
rows selected.
selected. 21 rows selected.
21 rows selected.
611 11
611
612
612 11
601
601 11
602
602 11
...
...
64
64 rows
rows selected.
selected.
Non-Equijoins
Non-Equijoins
EMP SALGRADE
EMPNO ENAME SAL GRADE LOSAL HISAL
------ ------- ------ ----- ----- ------
7839 KING 5000 1 700 1200
7698 BLAKE 2850 2 1201 1400
7782 CLARK 2450 3 1401 2000
7566 JONES 2975 4 2001 3000
7654 MARTIN 1250 5 3001 9999
7499 ALLEN 1600
7844 TURNER 1500
7900 JAMES 950
... “salary in the EMP
14 rows selected. table is between
low salary and high
salary in the SALGRADE
table”
Retrieving
Retrieving Records
Records
with
with Non-Equijoins
Non-Equijoins
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;

ENAME SAL GRADE


---------- --------- ---------
JAMES 950 1
SMITH 800 1
ADAMS 1100 1
...
14 rows selected.
Outer
Outer Joins
Joins
EMP DEPT
ENAME DEPTNO DEPTNO DNAME
----- ------ ------ ----------
KING 10 10 ACCOUNTING
BLAKE 30 30 SALES
CLARK 10 10 ACCOUNTING
JONES 20 20 RESEARCH
... ...
40 OPERATIONS

No employee in the
OPERATIONS department
Outer
Outer Joins
Joins
 You
 You use
use an
an outer
outer join
join to
to also
also see
see
rows
rows that
that do
do not
not usually
usually meet
meet the
the
join
join condition.
condition.
 Outer
 Outer join
join operator
operator is is the
the plus
plus sign
sign
(+)
(+)
SELECT
SELECT ..
table1.column,
table1.column, table2.column
table2.column
FROM
FROM table1,
table1, table2
table2
WHERE
WHERE table1.column(+)
table1.column(+) == table2.column;
table2.column;

SELECT
SELECT table1.column,
table1.column, table2.column
table2.column
FROM
FROM table1,
table1, table2
table2
WHERE
WHERE table1.column
table1.column == table2.column(+);
table2.column(+);
Using
Using Outer
Outer Joins
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 ACCOUNTING
CLARK 10 ACCOUNTING
...
40 OPERATIONS
15 rows selected.
Outer Joins

 Student 105 (Michael Connoly) does not


have any ENROLLMENT records
Outer Joins
 No records retrieved for Michael:
Outer Joins

 To include records in first (inner) table,


even when they do not have matching
records in second (outer) table, place
outer join marker (+) beside outer table
name in join clause
Outer Joins
Outer join marker
Self
Self Joins
Joins
EMP (WORKER) EMP (MANAGER)
EMPNO ENAME MGR EMPNO ENAME
----- ------ ---- ----- --------
7839 KING
7698 BLAKE 7839 7839 KING
7782 CLARK 7839 7839 KING
7566 JONES 7839 7839 KING
7654 MARTIN 7698 7698 BLAKE
7499 ALLEN 7698 7698 BLAKE

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


MANAGER table”
Joining
Joining a
a Table
Table to
to Itself
Itself
SQL> SELECT worker.ename||' works for '||manager.ename
2 FROM emp worker, emp manager
3 WHERE worker.mgr = manager.empno;

WORKER.ENAME||'WORKSFOR'||MANAG
WORKER.ENAME||'WORKSFOR'||MANAG
-------------------------------
-------------------------------
BLAKE
BLAKE works
works for
for KING
KING
CLARK
CLARK works
works for
for KING
KING
JONES
JONES works
works for
for KING
KING
MARTIN
MARTIN works
works for
for BLAKE
BLAKE
...
...
13
13 rows
rows selected.
selected.
Summary
Summary

SELECT
SELECT table1.column,
table1.column, table2.column
table2.column
FROM
FROM table1,
table1, table2
table2
WHERE
WHERE table1.column1
table1.column1 == table2.column2;
table2.column2;

Equijoin
Equijoin Non-equijoin
Non-equijoin Outer
Outer join
join Self
Self join
join
Subqueries
Subqueries
SELECT select_list
FROM table
WHERE expr operator
(SELECT select_list
FROM table);

 The subquery (inner query) executes once before the main query.
The subquery (inner query) executes once before the main query.
 The result of the subquery is used by the main query (outer query).
The result of the subquery is used by the main query (outer query).
Using
Using a
a Subquery
Subquery
SQL> SELECT ename
2 FROM emp 2975
3 WHERE sal >
4 (SELECT sal
5 FROM emp
6 WHERE empno=7566);

ENAME
ENAME
----------
----------
KING
KING
FORD
FORD
SCOTT
SCOTT
Guidelines
Guidelines for
for Using
Using
Subqueries
Subqueries
 Enclose
 Enclose subqueries
subqueries in
in parentheses.
parentheses.
 Place
 Place subqueries
subqueries on
on the
the right
right side
side
of
of the
the comparison
comparison operator.
operator.
 Do
 Do not
not add
add an
an ORDER
ORDER BYBY clause
clause to
to
aa subquery.
subquery.
 Use
 Use single-row
single-row operators
operators with
with
single-row
single-row subqueries.
subqueries.
 Use
 Use multiple-row
multiple-row operators
operators with
with
multiple-row
multiple-row subqueries.
subqueries.
Types
Types of
of Subqueries
Subqueries

 Single-row
Single-row subquery
subquery
Main query
returns
Subquery CLERK

•• Multiple-row
Multiple-row subquery
subquery
Main query

Subquery
returns CLERK
MANAGER
•• Multiple-column
Multiple-column subquery
subquery
Main query
returns
Subquery CLERK 7900
MANAGER 7698
Executing
Executing Single-Row Single-Row
SQL> SELECT Subqueries
2 FROM
Subqueries
ename, job
emp
3 WHERE job = CLERK
4 (SELECT job
5 FROM emp
6 WHERE empno = 7369)
7 AND sal > 1100
8 (SELECT sal
9 FROM emp
10 WHERE empno = 7876);

ENAME
ENAME JOB
JOB
----------
---------- ---------
---------
MILLER
MILLER CLERK
CLERK
Using
Using Group
Group Functions
Functions
in
in a
a Subquery
Subquery
SQL> SELECT ename, job, sal
800
2 FROM emp
3 WHERE sal =
4 (SELECT MIN(sal)
5 FROM emp);

ENAME
ENAME JOB
JOB SAL
SAL
----------
---------- ---------
--------- ---------
---------
SMITH
SMITH CLERK
CLERK 800
800
HAVING
HAVING Clause
Clause with
with
Subqueries
Subqueries

 The
The Oracle
Oracle Server
Server executes
executes
subqueries
subqueries first.
first.
 The
 The Oracle
Oracle Server
Server returns
returns results
results
into
into the
the HAVING
HAVING clause
clause of
of the
the
main
main query.
query.
SQL> SELECT deptno, MIN(sal)
2 FROM emp
3 GROUP BY deptno
800
4 HAVING MIN(sal) >
5 (SELECT MIN(sal)
6 FROM emp
7 WHERE deptno = 20);
Multiple-Row
Multiple-Row Subqueries
Subqueries
 Return
 Return more
more than
than one
one row
row
 Use
 Use multiple-row
multiple-row comparison
comparison
operators
operators
Operator Meaning

IN Equal to any member in the list

ANY Compare value to each value returned by


the subquery

Compare value to every value returned by


ALL
the subquery
Database Views

 Logical table based on a query


 Does not physically exist in the
database
 Presents data in a different format
from underlying tables
 Uses:
 Security
 Simplifying complex queries
Creating
Creating a
a View
View
 You embed a subquery within the CREATE VIEW statement.
 You embed a subquery within the CREATE VIEW statement.

 The subquery can contain complex SELECT syntax.


 The subquery can contain complex SELECT syntax.
 The subquery cannot contain an ORDER BY clause.
 The subquery cannot contain an ORDER BY clause.

CREATE
CREATE [OR
[OR REPLACE]
REPLACE] VIEW
VIEW view_name
view_name
AS
AS
subquery;
subquery;
Creating
Creating a
a View
View
 Create
 Create aa view,
view, EMPVU10,
EMPVU10, that
that contains
contains
details
details of
of employees
employees in
in department
department 10.
10.
SQL> CREATE VIEW empvu10
2 AS SELECT empno, ename, job
3 FROM emp
4 WHERE deptno = 10;
View created.

•• Describe
Describe the
the structure
structure of
of the
the view
view by
by
using
using the
the SQL*Plus
SQL*Plus DESCRIBE
DESCRIBE command.
command.

SQL>
SQL> DESCRIBE
DESCRIBE empvu10
empvu10
Retrieving
Retrieving Data
Data from
from a
a
View
View
SQL> SELECT *
2 FROM salvu30;

EMPLOYEE_NUMBER
EMPLOYEE_NUMBER NAME
NAME SALARY
SALARY
---------------
--------------- ----------
---------- ---------
---------
7698
7698 BLAKE
BLAKE 2850
2850
7654
7654 MARTIN
MARTIN 1250
1250
7499
7499 ALLEN
ALLEN 1600
1600
7844
7844 TURNER
TURNER 1500
1500
7900
7900 JAMES
JAMES 950
950
7521
7521 WARD
WARD 1250
1250

66 rows
rows selected.
selected.
Removing
Removing a
a View
View

 Remove
Remove aa view
view without
without losing
losing data
data
because
because aa view
view is
is based
based onon
underlying
underlying tables
tables in
in the
the database.
database.
DROP
DROP VIEW
VIEW view;
view;

SQL> DROP VIEW empvu10;


View dropped.
Using Set Operators in
Queries
 Performs set operations on outputs
of two unrelated queries
 Both queries must have:
 same number of display fields
 corresponding display fields must have
same data type
Query Set Operators
 UNION: combines results,
suppresses duplicate rows
 UNION ALL: combines results,
displays duplicates
 INTERSECT: finds matching rows
 MINUS: returns the difference
between returned record sets
Synonyms
 Alternate name for a table
 Allows you to not have to preface
table with owner’s username when
you are querying a table that
belongs to another user
Public Synonyms
 Can only be created by a DBA
 Syntax:
CREATE PUBLIC SYNONYM synonym_name
FOR owner_name.tablename;
 All users with privileges to use table
can then use synonym instead of
owner_name.tablename
Private Synonyms
 You can create private synonyms for
any tables that you have privileges
to use
 Only you can use the synonym
 Syntax:
CREATE SYNONYM synonym_name
FOR table_name.table_name;

You might also like