Professional Documents
Culture Documents
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.
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;
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
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;