nulls & outer joins

30
NULLs & Outer Joins Objectives of the Lecture : •To consider the use of NULLs in SQL. •To consider Outer Join Operations, and their implementation in SQL.

Upload: armen

Post on 06-Jan-2016

41 views

Category:

Documents


0 download

DESCRIPTION

NULLs & Outer Joins. Objectives of the Lecture :. To consider the use of NULLs in SQL. To consider Outer Join Operations, and their implementation in SQL. Missing Values : Possible Strategies. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: NULLs & Outer Joins

NULLs & Outer Joins

Objectives of the Lecture :

•To consider the use of NULLs in SQL.

•To consider Outer Join Operations, and their implementation in SQL.

Page 2: NULLs & Outer Joins

Missing Values : Possible Strategies Use a special value to represent missing data.

E.g. ‘N/A’, ‘T.B.A.’The special value must have the same data type as the data that is missing, so it can be stored with the data that is known.Requires no special facility from the DBMS.

Use NULL to represent missing data. NULL is the absence of a value. NULL 0 NULL ‘ ’ NULL is not part of any data type.Requires special support from the DBMS. SQL DBMSs provide this support.So most DBs use NULLs to represent missing data.

spacecharacter

Page 3: NULLs & Outer Joins

Display of SQL NULLs

EmpNo EName M-S Sal

E3 Smith S 18,000

E5 Robinson M 24,000

E9 Graham S

E1 Robson D 32,500

E2 Atkins 24,000

E6 Blakelaw M 54,000

E7 Mortimer D 28,000

E4 Fenwick S 40,000

Blank spacein Oracle.

Keyword NULL inother SQL DBMSs.

Other possibilitiesin other DBMSs.

Page 4: NULLs & Outer Joins

Dealing with NULLs in SQL Tables

Three situations arise :

Comparisons of column values. This occurs in the SQL equivalents of the Restrict and the various Join operations, plus Deletions and Updates.

Calculations involving column values. This occurs in the SQL equivalents of the GroupBy and Extend operations.

Comparisons of row values. This occurs in the SQL equivalents of the Project, GroupBy, Union, Intersect, and Difference operations.

Page 5: NULLs & Outer Joins

Comparison of Column Values (1)SQL provides special comparators to check for NULL :-

X IS NULLX IS NOT NULL

Let X be a numeric column. If X has a value, the comparisonX = 3

makes sense. It should yield true or false.

Suppose X is NULL. An error should arise.In fact SQL treats the NULL as representing an existing but unknown value. Comparison returns maybe.

Rationale : We don’t know if X = 3because X is NULL (= not available).

Note : X may represent some other case of missing data(e.g. not applicable, does not exist).The result is still maybe even though this is then illogical.

Page 6: NULLs & Outer Joins

Comparisons of Column Values (2)

Let X and Y be a numeric columns. Consider the comparison X = YSuppose X and Y are both NULL.

The result is maybe not true.

NULL is not the same as maybe.

Absenceof a value.

A truthvalue.

SQL uses NULL to mean maybe !

Page 7: NULLs & Outer Joins

Restricts, Joins, Updates and DeletionsRestrict SELECT *

FROM TableNameWHERE condition ;

Join SELECT *FROM Table1 NATURAL JOIN Table2 ;

Delete DELETE FROM TableNameWHERE condition ;

Update UPDATE TableNameSET column(s) = new value(s)WHERE condition ;

Restrict / Join / Delete / Update action taken only where condition evaluates to true,

not where it evaluates to maybe or false.

Columncomparison

used as acondition

Similarly for otherkinds of Join.

Page 8: NULLs & Outer Joins

Unexpected Results (1)

They arise when forgetting that the condition can evaluate to maybe.

Example :-SELECT *FROM EMPWHERE Sal >= 20000

UNION SELECT *FROM EMPWHERE Sal < 20000 ;

the 2 Restrictions will not necessarily contain all the rows of EMP between them.

If column ‘Sal’contains any NULLs, the result will notre-create table EMP.

Page 9: NULLs & Outer Joins

Unexpected Results (2)

To ensure the table is re-created, re-write the query as follows :-

SELECT *FROM EMPWHERE Sal >= 20000

UNIONSELECT *FROM EMPWHERE Sal < 20000

UNIONSELECT *FROM EMPWHERE Sal IS NULL ;

In general,adjust

statementsto reflect the

NULL possibility.

Page 10: NULLs & Outer Joins

Join involving NULLs : Example

P# S# Qty

P1 S1 5

P2 10

P2 S2 7

S# Details

S 1 ……..

S 2 ……..

S 3 ……..

P# S# Qty Details

P1 S1 5 ………..

P2 S2 7 ………..

This row does notappear in the result. SELECT *

FROM R Natural Join S ;

R S

Page 11: NULLs & Outer Joins

Oracle’s “NVL” Function

NVL supplies a value to use whenever a NULL is encountered.It can be used in SELECT and WHERE phrases.

Example : NVL( Sal, 0 )

This yields the value of the ‘Sal’ column, except that if ‘Sal’ is NULL, then a value of zero is returned.

NVL can be used to ensure a comparison always yields true or false, and never maybe.

Example : ……... WHERE NVL( M-S, ‘S’ ) <> ‘M’

Put a column name in the first position.

Put a value in the second position.

Value ‘S’ is used in the comparison when ‘M-S’ is NULL.Comparison can never return maybe.

Page 12: NULLs & Outer Joins

Calculations Involving Column Values

These arise in two situations :

Scalar calculations along rows Extend

Aggregate calculationsalong columns GroupBy

EmpNo EName M-S Sal

E3 Smith S

E5 Robinson M 24,000

E9 Graham S

E1 Robson D 32,500

E2 Atkins M

E6 Blakelaw M

E7 Mortimer D 28,000

E4 Fenwick S 40,000

Page 13: NULLs & Outer Joins

Scalar Calculations

Any calculation involving a NULL results in a NULL.

Examples : let n be NULL. Then :- n + 1 NULL n concatenate “ABCD” NULL n - n NULL (not zero)

Example :-SELECT Sal + 100 AS NewSalFROM EMP ;

So “NewSal” will be NULL whenever “Sal” is NULL.

Page 14: NULLs & Outer Joins

Aggregate Calculations

If the columns being aggregated contain one or more NULLs, then the answer from :

Sum Avg Min ignores the NULLs. Max Count( Distinct )

Count(*) includes the NULLs.

Page 15: NULLs & Outer Joins

Example : Aggregation in GroupBy

SELECT Sum(Sal) AS Total, M-SFROM EMPGROUP BY M-S ;

EmpNo EName M-S Sal

E3 Smith S

E5 Robinson M 24,000

E9 Graham S

E1 Robson D 32,500

E2 Atkins M

E6 Blakelaw M

E7 Mortimer D 28,000

E4 Fenwick S 40,000

Total M-S

40,000 S

24,000 M

60,500 D

Page 16: NULLs & Outer Joins

Comparisons of Rows In SQL, two rows are identical if :

• they have the same number of attributes;

• corresponding attributes are of the same data type;

• corresponding attributes have the same value.

In an SQL row comparison,

a NULL compared to a NULL true

In an SQL column comparison(e.g. for a Join operation)

a NULL compared to a NULL maybe

Different !!

Page 17: NULLs & Outer Joins

Example : Row Comparison

Comparison of M-S column values : Row Comparison : 2 NULLs are defined to be identical.

A comparison yields true !! these 2 rows are identical.

Column Comparison : 2 NULLs are not assumed identical. A comparison yields maybe !!

these rows are rejected.

E2 Atkins 24,000

E2 Atkins 24,000

E_No E_Name M_S Sal

Page 18: NULLs & Outer Joins

Project, GroupBy, & Set Operators

Project SELECT DISTINCT ColumnName(s)FROM TableName ;

GroupBy SELECT “Aggregation(s)”, GroupingCol(s)FROM TableNameGROUP BY GroupingCol(s) ;

Set Ops SELECT *FROM TableName1

UNION SELECT *

FROM TableName2 ;

Project / GroupBy (grouping rows) / Union / Intersect / Except action taken on the basis that all NULLs are identical.

Similarly forthe otherSet Ops,

Intersect &Except/Minus.

Page 19: NULLs & Outer Joins

Example : Projection

SELECT DISTINCT M-SFROM EMP ;

EmpNo EName M-S Sal

E3 Smith S 18,000

E5 Robinson M 24,000

E9 Graham 18,000

E1 Robson D 32,500

E2 Atkins 24,000

E6 Blakelaw M 54,000

E7 Mortimer D 28,000

E4 Fenwick W 40,000

M-S

S

M

D

W

Page 20: NULLs & Outer Joins

Example : Grouping in ‘GroupBy’

SELECT “Aggregation”, M-SFROM EMPGROUP BY M-S ;

Aggregate M-S

“Agg-Val1” S

“Agg-Val2” M

“Agg-Val3”

“Agg-Val4” D

EmpNo EName M-S Sal

E3 Smith S 18,000

E5 Robinson M 24,000

E9 Graham 18,000

E1 Robson D 32,500

E2 Atkins 24,000

E6 Blakelaw M 54,000

E7 Mortimer D 28,000

E4 Fenwick S 40,000

Page 21: NULLs & Outer Joins

Example : Union Operation

Union

EmpNo EName M-S Sal

E3 Smith S 18,000

E5 Robinson M 24,000

E9 Graham S

E1 Robson D 32,500

E2 Atkins 24,000

EmpNo EName M-S Sal

E1 Robson D 32,500

E2 Atkins 24,000

E6 Blakelaw M 54,000

EmpNo EName M-S Sal

E3 Smith S 18,000

E5 Robinson M 24,000

E9 Graham S

E1 Robson D 32,500

E2 Atkins 24,000

E6 Blakelaw M 54,000

Page 22: NULLs & Outer Joins

Joins - Inner versus Outer All joins considered so far are Inner Joins.

Only a subset of each operand’s tuples appear in the result.These are the tuples that match each other in the 2 operands.

(Match the comparison (of whatever kind) is true).The unmatched tuples don’t appear in the result.

Sometimes it is useful to have unmatched tuples in the result as well.

Outer JoinThree kinds of Outer Join, to retain in the result all the unmatched tuples from :

• ‘Left’ operand,• ‘Right’ operand,• ‘Left’ and ‘Right’ operands.

Page 23: NULLs & Outer Joins

Inner Joins

(Natural)

Unmatched tuples are not in the result.

unmatched

unmatched

Page 24: NULLs & Outer Joins

Outer Join : Left

? ? ? ?

(Natural)

Some unmatched tuples are in the result.

unmatched ‘padding’

unmatched

unmatched

Page 25: NULLs & Outer Joins

Outer Join : Right

? ? ? ? ? ? ? ?

(Natural)

Some unmatched tuples are in the result.

unmatched‘padding’

unmatched

unmatched

Page 26: NULLs & Outer Joins

Outer Join : Full

? ? ? ? ? ? ? ?

? ? ? ?

(Natural)

All unmatched tuples are in the result.

unmatched

unmatched ‘padding’

‘padding’

unmatched

unmatched

Page 27: NULLs & Outer Joins

Outer Joins in SQL

What “padding” attribute values are used with the unmatched columns ?

What syntax is used for outer joins ?

Natural Join, Join Using( ColNames ), Join On( condition ).

Each of these can be used for Left, Right and Full outer joins. 9 possibilities.

SQL uses NULLs.

An extension of the FROM phrase inner join syntax.

Page 28: NULLs & Outer Joins

SQL2 Outer Natural Joins

SELECT *FROM R Natural Join S ;

LeftRightFull

Outeroptionally inserted

Examples :-

SELECT * FROM SUPP Natural Left Outer Join SHIP ;Result retains all the unmatched rows of LHS table, i.e. SUPP.

SELECT * FROM SUPP Natural Right Join SHIP ;Result retains all the unmatched rows of RHS table, i.e. SHIP.

Page 29: NULLs & Outer Joins

The Other Two SQL2 Outer Joins SELECT *

FROM R Join S Using ( attribute(s) ) ;

SELECT *FROM R Join S On ( condition ) ;

Example :- SELECT * FROM SUPP Left Outer Join SHIP Using( S# ) ;

Left and right refer to the tables written to left and right of the join operator.Logically only left or right is required, but it is convenient to have both.

LeftRightFull

Outeroptionally insertedLeft

RightFull

Page 30: NULLs & Outer Joins

Oracle : Outer Joins

Original Oracle syntax is completely non-standard.The idea is to add a (+) suffix to

the name of the column that is in the table whose columns will receive the NULLs as ‘padding’.

Regarding ‘left’ and ‘right’, this is the exact opposite of the SQL standard.

Example :-SELECT AttributeNamesFROM SUPP, SHIPWHERE SUPP.S# = SHIP.S#(+) ;

Old fashioned SQL1 join syntax is required. ‘Left’ & ‘right’ refer to columns in the WHERE phrase, not

tables in the FROM phrase. Full join ≡ Union of left and right outer joins.

Do NOT useunless desparate !