Download presentation
Presentation is loading. Please wait.
Published byDiane Bennett Modified over 9 years ago
1
CS 405G: Introduction to Database Systems SQL III
2
2/16/2016Jinze Liu @ University of Kentucky2 Summary of SQL features covered so far SELECT - FROM - WHERE statements Ordering Set and bag operations Aggregation and grouping Nested queries Next: NULL ’s, outerjoins, data modification, constraints, …
3
2/16/2016Jinze Liu @ University of Kentucky3 Incomplete information Example: Student (SID, name, age, GPA) Value unknown We do not know Nelson’s age Value not applicable Nelson has not taken any classes yet; what is his GPA?
4
2/16/2016Jinze Liu @ University of Kentucky4 Solution 1 A dedicated special value for each domain (type) GPA cannot be –1, so use –1 as a special value to indicate a missing or invalid GPA Leads to incorrect answers if not careful SELECT AVG(GPA) FROM Student; Complicates applications SELECT AVG(GPA) FROM Student WHERE GPA <> -1; Remember the Y2K bug? “00” was used as a missing or invalid year value
5
2/16/2016Jinze Liu @ University of Kentucky5 Solution 2 A valid-bit for every column Student (SID,name, name_is_valid, age, age_is_valid, GPA, GPA_is_valid) Complicates schema and queries SELECT AVG(GPA) FROM Student WHERE GPA_is_valid;
6
2/16/2016Jinze Liu @ University of Kentucky6 Solution 3? Decompose the table; missing row = missing value StudentName (SID, name) StudentAge (SID, age) StudentGPA (SID, GPA) StudentID (SID) Conceptually the cleanest solution Still complicates schema and queries How to get all information about a student in a table? Would natural join work?
7
2/16/2016Jinze Liu @ University of Kentucky7 SQL’s solution A special value NULL For every domain Special rules for dealing with NULL ’s Example: Student (SID, name, age, GPA) h 789, “Nelson”, NULL, NULL i
8
2/16/2016Jinze Liu @ University of Kentucky8 Three-valued logic TRUE = 1, FALSE = 0, UNKNOWN = 0.5 x AND y = min(x, y) x OR y = max(x, y) NOT x = 1 – x WHERE and HAVING clauses only select rows for output if the condition evaluates to TRUE UNKNOWN is not enough ANDTrueFalseNULL True FalseUNK False NULLUNKFalseUNK AND ORTrueFalseNULL True FalseTRUE False UNK NULLTRUEUNK OR
9
2/16/2016Jinze Liu @ University of Kentucky9 Computing with NULL’s (Arithmetic operation) when we operate on a NULL and another value (including another NULL ) using +, –, etc., the result is NULL Aggregate functions ignore NULL, except COUNT(*) (since it counts rows) When we compare a NULL with another value (including another NULL ) using =, >, etc., the result is UNKNOWN
10
2/16/2016Jinze Liu @ University of Kentucky10 Unfortunate consequences SELECT AVG(GPA) FROM Student 3.4 SELECT SUM(GPA)/COUNT(*) FROM Student; 2.72 SELECT * FROM Student; SELECT * FROM Student WHERE GPA = GPA Not equivalent sidnameagegpa 1234John Smith213.5 1123Mary Carter193.8 1011Bob Lee22NULL 1204Susan Wong223.4 1306Kevin Kim182.9
11
2/16/2016Jinze Liu @ University of Kentucky11 Another problem Example: Who has NULL GPA values? SELECT * FROM Student WHERE GPA = NULL; Does not work; never returns anything (SELECT * FROM Student) EXCEPT ALL (SELECT * FROM Student WHERE GPA = GPA) Works, but ugly Introduced built-in predicates IS NULL and IS NOT NULL SELECT * FROM Student WHERE GPA IS NULL;
12
2/16/2016Jinze Liu @ University of Kentucky12 Outerjoin motivation Example: a master class list SELECT c.CID, s.SID FROM Enroll e, Student s, Course c WHERE e.SID = s.SID and c.CID = e.CID; What if a student take no classes For these students, CID column should be NULL What if a course with no student enrolled yet? For these courses, SID should be NULL
13
2/16/2016Jinze Liu @ University of Kentucky13 Outerjoin SELECT * FROM R FULL OUTER JOIN S ON p; A full outer join between R and S (denoted R ! S) includes all rows in the result of R ! p S, plus “Dangling” R rows (those that do not join with any S rows) padded with NULL ’s for S’s columns “Dangling” S rows (those that do not join with any R rows) padded with NULL ’s for R’s columns
14
2/16/2016Jinze Liu @ University of Kentucky14 Outerjoin (II) SELECT * FROM R LEFT OUTER JOIN S ON p;; SELECT * FROM R RIGHT OUTER JOIN S ON p; A left outer join (R ! S) includes rows in R ! S plus dangling R rows padded with NULL ’s A right outer join (R ! S) includes rows in R ! S plus dangling S rows padded with NULL ’s
15
2/16/2016Jinze Liu @ University of Kentucky15 Outerjoin examples Department Employee DidMidDname 41123Research 51234Finance 61312HR EidName 1123John Smith 1234Mary Carter 1311Bob Lee SELECT * FROM Employee LEFT OUTER JOIN Department ON Eid = Mid EidNameDidMidDname 1123John Smith41123Research 1234Mary Carter51234Finance 1311Bob LeeNULL SELECT * FROM Employee RIGHT OUTER JOIN Department ON Eid = Mid EidNameDidMidDname 1123John Smith41123Research 1234Mary Carter51234Finance NULL 61312HR SELECT * FROM Employee FULL OUTER JOIN Department ON Eid = Mid EidNameDidMidDname 1123John Smith41123Research 1234Mary Carter51234Finance 1311Bob LeeNULL 61312HR
16
2/16/2016Jinze Liu @ University of Kentucky16 Summary Query SELECT - FROM - WHERE statements Ordering Set and bag operations Aggregation and grouping Table expressions, subqueries NULL Outerjoins
17
2/16/2016Jinze Liu @ University of Kentucky17 Summary of SQL features covered so far SELECT - FROM - WHERE statements Set and bag operations Ordering Aggregation and grouping Table expressions, subqueries NULL ’s and outerjoins Next: data modification statements, constraints
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.