Ansi Join SQL
Ansi Join SQL
Speaker Qualifications
Rumpi Gravenstein, Application Developer,
Agenda
Brief History
Review Join Technologies
Analysis
Recommendation
Outerjoin
Cross/Cartesian product
SELECT d.dname,
e.ename
FROM emp d
JOIN dept d USING ( deptno );
SELECT e.empno,
l.loc_id,
No commas between tables
d.dname,
l.state_tx
FROM locations l
JOIN dept d ON ( d.location_id = l.id )
JOIN emp e ON ( d.deptno = e.deptno );
Bring in second table join
SELECT e.ename,
d.dname
OUTER keyword is optional.
FROM dept d
LEFT OUTER JOIN emp e
ON (e.deptno = d.deptno);
USING e.g.
LEFT JOIN emp e USING (deptno)
can be used in an INNER and OUTER join.
SELECT e.ename,
d.dname
FROM dept d
NATURAL LEFT JOIN emp e;
SELECT e.ename,
d.dname
FROM emp e
RIGHT OUTER JOIN dept d
ON (e.deptno = d.deptno);
SELECT e.ename,
d.dname
FROM emp e, dept d
WHERE e.deptno (+) = d.deptno
UNION
SELECT e.ename,
d.dname
FROM emp e, dept d
WHERE e.deptno = d.deptno (+)
This full join syntax can be found
on internet as true full join
Notice Cost 15
Execution Plan
---------------------------------------------------------0
SELECT STATEMENT Optimizer=ALL_ROWS (Cost=15 Card=28 Bytes=588)
1
0
SORT (UNIQUE) (Cost=15 Card=28 Bytes=588)
2
1
UNION-ALL
3
2
HASH JOIN (OUTER) (Cost=7 Card=14 Bytes=294)
4
3
TABLE ACCESS (FULL) OF 'DEPT' (TABLE) (Cost=3 Card=5 Bytes=60)
5
3
TABLE ACCESS (FULL) OF 'EMP' (TABLE) (Cost=3 Card=14 Bytes=126)
6
2
HASH JOIN (OUTER) (Cost=7 Card=14 Bytes=294)
7
6
TABLE ACCESS (FULL) OF 'EMP' (TABLE) (Cost=3 Card=14 Bytes=126)
8
6
TABLE ACCESS (FULL) OF 'DEPT' (TABLE) (Cost=3 Card=5 Bytes=60)
Execution Plan
---------------------------------------------------------0
SELECT STATEMENT Optimizer=ALL_ROWS (Cost=13 Card=16 Bytes=324)
1
0
UNION-ALL
2
1
HASH JOIN (OUTER) (Cost=7 Card=14 Bytes=294)
3
2
TABLE ACCESS (FULL) OF 'EMP' (TABLE) (Cost=3 Card=14 Bytes=126)
4
2
TABLE ACCESS (FULL) OF 'DEPT' (TABLE) (Cost=3 Card=5 Bytes=60)
5
1
HASH JOIN (ANTI) (Cost=7 Card=2 Bytes=30)
6
5
TABLE ACCESS (FULL) OF 'DEPT' (TABLE) (Cost=3 Card=5 Bytes=60)
7
5
TABLE ACCESS (FULL) OF 'EMP' (TABLE) (Cost=3 Card=14 Bytes=42)
SELECT e.ename,
NULL in department name if
d.dname
employee not in a department.
FROM emp e
FULL OUTER JOIN dept d
ON (e.deptno = d.deptno);
OUTER is optional
FULL OUTER denotes that the table to the right
AND the table to the left will have all their
records returned
Cost 13 = Cost of
Traditional True
Execution Plan
---------------------------------------------------------0
SELECT STATEMENT Optimizer=ALL_ROWS (Cost=13 Card=16 Bytes=256)
1
0
VIEW (Cost=13 Card=16 Bytes=256)
2
1
UNION-ALL
3
2
HASH JOIN (OUTER) (Cost=7 Card=14 Bytes=294)
4
3
TABLE ACCESS (FULL) OF 'EMP' (TABLE) (Cost=3 Card=14 Bytes=126)
5
3
TABLE ACCESS (FULL) OF 'DEPT' (TABLE) (Cost=3 Card=5 Bytes=60)
6
2
HASH JOIN (ANTI) (Cost=7 Card=2 Bytes=30)
7
6
TABLE ACCESS (FULL) OF 'DEPT' (TABLE) (Cost=3 Card=5 Bytes=60)
8
6
TABLE ACCESS (FULL) OF 'EMP' (TABLE) (Cost=3 Card=14 Bytes=42)
SELECT empno,
ename
FROM emp e
Only tables in current
FROM clause visible to
WHERE EXISTS
ANSI join logic
( SELECT NULL
FROM dept d
INNER JOIN locations l
ON ( l.loc_id = d.loc )
WHERE d.deptno = e.deptno
);
Not really a mixed
syntax join
ENAME
======
ALLEN
WARD
MARTIN
TURNER
JOB
========
SALESMAN
SALESMAN
SALESMAN
SALESMAN
ENAME
======
ALLEN
WARD
MARTIN
TURNER
JOB
========
SALESMAN
SALESMAN
SALESMAN
SALESMAN
SELECT emp_id,
Oracle will run this without
throwing an error
ename,
dname
FROM emp e
INNER JOIN dept d USING (deptno),
dual
WHERE deptno = 10;
Uhg.
Choose one or the other,
but not both please!
ENAME
---------CLARK
KING
MILLER
3 rows selected.
DNAME
-------------ACCOUNTING
ACCOUNTING
ACCOUNTING
D
X
X
X
SELECT empno,
ename,
dname,
ANSI joins can only see other
dummy
tables taking part in ANSI join
FROM dual,
emp e
INNER JOIN dept d on d.deptno = e.deptno
and dummy IS NOT NULL
WHERE e.deptno = 10;
and dummy IS NOT NULL
*
ERROR at line 8:
ORA-00904: "DUMMY": invalid identifier
Readability
Join Errors
Flexibility
Ease of Use
Developer Training
DBA Training
Legacy Code
Standards
Table
Join
Conditions
Are
Easily
Identified
SELECT col1,
col2,
...
FROM tab1 t1,
tab2 t2,
...
WHERE ...
SELECT col1,
col2,
...
FROM tab1 t1
[join type] tab2 t2
[join condition]
...
Impossible to do
inadvertent CROSS join
WHERE ...
Identifies join condition
USING or ON
Flexibility
(+) Restrictions not present in ANSI
You cannot specify the (+) operator in a query block that also contains FROM clause join syntax.
The (+) operator can appear only in the WHERE clause or, in the context of left-correlation (that is, when specifying the
TABLE clause) in the FROM clause, and can be applied only to a column of a table or view.
If A and B are joined by multiple join conditions, then you must use the (+) operator in all of these conditions. If you do not,
then Oracle Database will return only the rows resulting from a simple join, but without a warning or error to advise you that
you do not have the results of an outer join.
The (+) operator does not produce an outer join if you specify one table in the outer query and the other table in an inner
query.
You cannot use the (+) operator to outer-join a table to itself, although self joins are valid. For example, the following
statement is not valid:
SELECT employee_id, manager_id
FROM employees
WHERE employees.manager_id(+) = employees.employee_id;
Oracle strongly
recommends that you use
the more flexible FROM
clause (ANSI) join syntax
The (+) operator can be applied only to a column, not to an arbitrary expression. However, an arbitrary expression can
contain one or more columns marked with the (+) operator.
A WHERE condition containing the (+) operator cannot be combined with another condition using the OR logical operator.
A WHERE condition cannot use the IN comparison condition to compare a column marked with the (+) operator with an
expression.
A WHERE condition cannot compare any column marked with the (+) operator with a subquery.
ANSI Join
code base
ANSI Joins present in other RDBMS installed
code
oin
lJ
ra y
tu ilit
Na ssib
No Po
le
ac
Or se
d
lle Ba
sta de
In Co
t
or
pp g
Su in
ly Cod
On e yle
On St
Mu
ltiRD Ven
Su BM dor
pp S
ort
Co
de
Cl
ari
ty
Co Few
nd er
itio Jo
n E in
rro
rs
St
an
da
rds
Ba
se
d
Fe
atu
re
Ri
ch
Fishbone Diagram
Only Allow Traditional Join Syntax
Recommendations
Allow both
Provide training so that all are familiar with both
Place some restrictions on ANSI syntax to prevent
problems
Session Goals
Familiarity with ANSI Join Syntax
Understanding the merits of the ANSI join
syntax
Intention to start using the ANSI syntax
?
Questions
Thank You
Please complete the evaluation forms
My Name: Rumpi Gravenstein
Session Title: To ANSI or not to ANSI
Session #: 420
If you have additional questions, I can be
reached at [email protected]