1 / 30

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. Missing Values : Possible Strategies.

armen
Download Presentation

NULLs & Outer Joins

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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.

  2. 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 ‘ ’NULLis 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

  3. Display of SQL NULLs Blank spacein Oracle. Keyword NULL inother SQL DBMSs. Other possibilitiesin other DBMSs.

  4. 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.

  5. 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 = 3makes 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.

  6. Comparisons of Column Values (2) • Let X and Y be a numeric columns. Consider the comparisonX = YSuppose X and Y are both NULL. The result is maybe not true. • NULLis not the same asmaybe. Absenceof a value. A truthvalue. • SQL uses NULL to mean maybe !

  7. Restricts, Joins, Updates and Deletions Restrict SELECT *FROMTableName WHEREcondition; JoinSELECT * FROM Table1 NATURAL JOIN Table2 ; Delete DELETE FROM TableName WHEREcondition; UpdateUPDATE TableNameSET column(s) = new value(s) WHEREcondition; • Restrict / Join / Delete / Update action takenonly where condition evaluates to true,not where it evaluates to maybe or false. Columncomparisonused as acondition Similarly for otherkinds of Join.

  8. Unexpected Results (1) They arise when forgetting that the condition can evaluate to maybe. Example :- SELECT *FROMEMPWHERE Sal >= 20000 UNION SELECT *FROMEMPWHERE 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.

  9. Unexpected Results (2) To ensure the table is re-created, re-write the query as follows :- SELECT *FROMEMPWHERE Sal >= 20000 UNIONSELECT *FROMEMPWHERE Sal < 20000 UNIONSELECT *FROMEMPWHERE Sal IS NULL; In general,adjuststatementsto reflect theNULL possibility.

  10. Join involving NULLs : Example R S This row does notappear in the result. SELECT *FROM R Natural Join S ;

  11. 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 yieldstrue or false, and never maybe. Example : ……... WHERENVL( 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.

  12. Calculations Involving Column Values These arise in two situations : Scalar calculations along rowsExtend Aggregate calculationsalong columnsGroupBy

  13. Scalar Calculations Any calculation involving a NULL results in a NULL. Examples : let n be NULL. Then :- • n + 1  NULL • nconcatenate“ABCD”  NULL • n - n  NULL(not zero) Example :- SELECT Sal + 100 AS NewSalFROM EMP ; So “NewSal” will be NULL whenever “Sal” is NULL.

  14. 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.

  15. Example : Aggregation in GroupBy SELECTSum(Sal) AS Total, M-SFROMEMPGROUP BY M-S ;

  16. 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 !!

  17. 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.

  18. Project, GroupBy, & Set Operators Project SELECT DISTINCT ColumnName(s)FROMTableName ; GroupBySELECT “Aggregation(s)”, GroupingCol(s) FROM TableName GROUP BY GroupingCol(s) ; Set Ops SELECT * FROM TableName1UNIONSELECT * 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.

  19. Example : Projection SELECT DISTINCT M-SFROMEMP ;

  20. Example : Grouping in ‘GroupBy’ SELECT “Aggregation”, M-SFROMEMPGROUP BY M-S ;

  21. Example : Union Operation Union

  22. 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 Join Three kinds of Outer Join, to retain in the result all the unmatched tuples from : • ‘Left’ operand, • ‘Right’ operand, • ‘Left’ and ‘Right’ operands.

  23. (Natural) Inner Joins Unmatched tuples are notin the result. unmatched unmatched

  24. (Natural) ? ? ? ? Outer Join : Left Some unmatched tuples are in the result. unmatched unmatched ‘padding’ unmatched

  25. (Natural) ? ? ? ? ? ? ? ? Outer Join : Right Some unmatched tuples are in the result. unmatched unmatched unmatched ‘padding’

  26. (Natural) ? ? ? ? ? ? ? ? ? ? ? ? Outer Join : Full All unmatched tuples are in the result. unmatched unmatched ‘padding’ unmatched unmatched ‘padding’

  27. 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.

  28. SQL2 Outer Natural Joins SELECT *FROM RNaturalJoinS ; Outeroptionally inserted LeftRightFull Examples :- SELECT * FROM SUPPNatural Left Outer JoinSHIP ; Result retains all the unmatched rows of LHS table, i.e. SUPP. SELECT * FROM SUPPNaturalRightJoinSHIP ; Result retains all the unmatched rows of RHS table, i.e. SHIP.

  29. LeftRightFull Outeroptionally inserted LeftRightFull The Other Two SQL2 Outer Joins • SELECT *FROM RJoinS Using( attribute(s) ) ; • SELECT *FROM RJoinS On( condition ) ; Example :- SELECT * FROM SUPPLeft Outer JoinSHIP Using( S# ) ; Leftand rightrefer to the tables written to left and right of the join operator.Logically only leftorright is required, but it is convenient to have both.

  30. 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,SHIPWHERESUPP.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 !

More Related