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 l 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. l Use NULL to represent missing data. NULL is the absence of a value. NULL 0NULL ‘ ’ 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. space character
Display of SQL NULLs Blank space in Oracle. Keyword NULL in other SQL DBMSs. Other possibilities in other DBMSs.
Dealing with NULLs in SQL Tables Three situations arise : l Comparisons of column values. This occurs in the SQL equivalents of the Restrict and the various Join operations, plus Deletions and Updates. l Calculations involving column values. This occurs in the SQL equivalents of the GroupBy and Extend operations. l Comparisons of row values. This occurs in the SQL equivalents of the Project, GroupBy, Union, Intersect, and Difference operations.
Comparison of Column Values (1) SQL provides special comparators to check for NULL :- X IS NULL X IS NOT NULL Let X be a numeric column. If X has a value, the comparison X = 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 = 3 because 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.
Comparisons of Column Values (2) l Let X and Y be a numeric columns. Consider the comparison X = Y Suppose X and Y are both NULL. The result is maybe not true. l NULL is not the same as maybe. Absence of a value. A truth value. l SQL uses NULL to mean maybe !
Restricts, Joins, Updates and Deletions RestrictSELECT * FROM TableName WHERE condition ; JoinSELECT * FROM Table1 NATURAL JOIN Table2 ; DeleteDELETE FROM TableName WHERE condition ; UpdateUPDATE TableName SET column(s) = new value(s) WHERE condition ; l Restrict / Join / Delete / Update action taken only where condition evaluates to true, not where it evaluates to maybe or false. Column comparison used as a condition Similarly for other kinds of Join.
Unexpected Results (1) They arise when forgetting that the condition can evaluate to maybe. Example :- SELECT* FROMEMP WHERESal >= UNION SELECT* FROMEMP WHERESal < ; the 2 Restrictions will not necessarily contain all the rows of EMP between them. If column ‘Sal’ contains any NULLs, the result will not re-create table EMP.
Unexpected Results (2) To ensure the table is re-created, re-write the query as follows :- SELECT* FROMEMP WHERESal >= UNION SELECT* FROMEMP WHERESal < UNION SELECT* FROMEMP WHERESal IS NULL ; In general, adjust statements to reflect the NULL possibility.
Join involving NULLs : Example This row does not appear in the result. SELECT * FROM R Natural Join S ; R S
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.
Calculations Involving Column Values These arise in two situations : Scalar calculations along rows Extend Aggregate calculations along columns GroupBy
Scalar Calculations Any calculation involving a NULL results in a NULL. Examples : let n be NULL. Then :- l n + 1 NULL l n concatenate “ABCD” NULL l n - n NULL (not zero) Example :- SELECT Sal AS NewSal FROM EMP ; So “NewSal” will be NULL whenever “Sal” is NULL.
Aggregate Calculations If the columns being aggregated contain one or more NULLs, then the answer from : l Sum l Avg l Minignores the NULLs. l Max l Count( Distinct ) l Count(*) includes the NULLs.
Example : Aggregation in GroupBy SELECT Sum(Sal) AS Total, M-S FROM EMP GROUP BY M-S ;
Comparisons of Rows l 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. l In an SQL row comparison, a NULL compared to a NULL true l In an SQL column comparison (e.g. for a Join operation) a NULL compared to a NULL maybe Different !!
Example : Row Comparison Comparison of M-S column values : l Row Comparison : 2 NULLs are defined to be identical. A comparison yields true !! these 2 rows are identical. l Column Comparison : 2 NULLs are not assumed identical. A comparison yields maybe !! these rows are rejected.
Project, GroupBy, & Set Operators ProjectSELECT DISTINCT ColumnName(s) FROM TableName ; GroupBySELECT “Aggregation(s)”, GroupingCol(s) FROM TableName GROUP BY GroupingCol(s) ; Set OpsSELECT * FROM TableName1 UNION SELECT * FROM TableName2 ; l Project / GroupBy (grouping rows) / Union / Intersect / Except action taken on the basis that all NULLs are identical. Similarly for the other Set Ops, Intersect & Except/Minus.
Example : Projection SELECT DISTINCT M-S FROM EMP ;
Example : Grouping in ‘GroupBy’ SELECT “Aggregation”, M-S FROM EMP GROUP BY M-S ;
Example : Union Operation Union
Joins - Inner versus Outer l 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. l Sometimes it is useful to have unmatched tuples in the result as well. Outer Join Three kinds of Outer Join, to retain in the result all the unmatched tuples from : ‘Left’ operand, ‘Right’ operand, ‘Left’ and ‘Right’ operands.
Inner Joins (Natural) Unmatched tuples are not in the result. unmatched
Outer Join : Left ? ? (Natural) Some unmatched tuples are in the result. unmatched ‘padding’ unmatched
Outer Join : Right ? ? ? ? ? ? ? ? (Natural) Some unmatched tuples are in the result. unmatched ‘padding’ unmatched
Outer Join : Full ? ? ? ? ? ? ? ? ? ? (Natural) All unmatched tuples are in the result. unmatched ‘padding’ unmatched
Outer Joins in SQL l What “padding” attribute values are used with the unmatched columns ? l What syntax is used for outer joins ? l Natural Join, l Join Using( ColNames ), l 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.
SQL2 Outer Natural Joins SELECT * FROM R Natural Join S ; Left Right Full Outer optionally 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.
The Other Two SQL2 Outer Joins l SELECT * FROM R Join S Using ( attribute(s) ) ; l 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. Left Right Full Outer optionally inserted Left Right Full
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 AttributeNames FROM SUPP, SHIP WHERE SUPP.S# = SHIP.S#(+) ; l Old fashioned SQL1 join syntax is required. l ‘Left’ & ‘right’ refer to columns in the WHERE phrase, not tables in the FROM phrase. l Full join ≡ Union of left and right outer joins. Do NOT use unless desparate !