Presentation is loading. Please wait.

Presentation is loading. Please wait.

INFS614 - Lecture week 9 1 More on SQL Lecture Week 9 INFS 614, Fall 2008.

Similar presentations


Presentation on theme: "INFS614 - Lecture week 9 1 More on SQL Lecture Week 9 INFS 614, Fall 2008."— Presentation transcript:

1 INFS614 - Lecture week 9 1 More on SQL Lecture Week 9 INFS 614, Fall 2008

2 INFS614 - Lecture week 9 2 Example Instances R1 S1 S2

3 INFS614 - Lecture week 9 3 SQL: Seen so far… v Syntax of Basic SQL query: –Relation-list (FROM) –Target-list (SELECT) –Qualification (WHERE) v Semantic: Conceptual Evaluation Strategy; v String operators: LIKE, NOT LIKE; v Set operators: Union (all), Intersect (all), Except (all); v Qualification involving sets: IN, EXISTS, UNIQUE, ANY, ALL (and their negated version); v Nested queries.

4 INFS614 - Lecture week 9 4 Post Processing v Processing on the result of an SQL query: –Sorting: can sort the tuples in the output by any column (even the ones not appearing in the SELECT clause) –Duplicate removal –Example: v Aggregation operators SELECT Distinct S.sname FROM Sailors S, Reserves R WHERE S.sid=R.sid and R.bid=103 Order by S.sid asc;

5 INFS614 - Lecture week 9 5 Order By: Example For each attribute in the sort list, we can specify its order: ASC : Ascending Order DESC : Descending Order Default Order is Ascending SELECT S.sname, S.age FROM Sailors S WHERE S.sname LIKE ‘%U%’ ORDER BY S.age,S.sname S.age ASC, S.sname DESC

6 INFS614 - Lecture week 9 6 Aggregate Operators v Significant extension of relational algebra. COUNT (*) COUNT ( [ DISTINCT ] A) SUM ( [ DISTINCT ] A) AVG ( [ DISTINCT ] A) MAX (A) MIN (A) single column

7 INFS614 - Lecture week 9 7 Aggregate Operators COUNT (*) COUNT ( [ DISTINCT ] A) SUM ( [ DISTINCT ] A) AVG ( [ DISTINCT ] A) MAX (A) MIN (A) SELECT AVG (S.age) FROM Sailors S WHERE S.rating=10 SELECT COUNT (*) FROM Sailors S SELECT AVG ( DISTINCT S.age) FROM Sailors S WHERE S.rating=10 SELECT S.sname FROM Sailors S WHERE S.rating= ( SELECT MAX (S2.rating) FROM Sailors S2) single column SELECT COUNT ( DISTINCT S.rating) FROM Sailors S WHERE S.sname=‘Bob’

8 INFS614 - Lecture week 9 8 Find name and age of the oldest sailor(s) v The first query is illegal! (We’ll look into the reason a bit later, when we discuss GROUP BY.) v The third query is equivalent to the second query, and is allowed in the SQL/92 standard, but is not supported in some systems. SELECT S.sname, MAX (S.age) FROM Sailors S SELECT S.sname, S.age FROM Sailors S WHERE S.age = ( SELECT MAX (S2.age) FROM Sailors S2) SELECT S.sname, S.age FROM Sailors S WHERE ( SELECT MAX (S2.age) FROM Sailors S2) = S.age

9 INFS614 - Lecture week 9 9 GROUP BY and HAVING v So far, we’ve applied aggregate operators to all (qualifying) tuples. Sometimes, we want to apply them to each of several groups of tuples. v Consider: Find the age of the youngest sailor for each rating level. – In general, we don’t know how many rating levels exist, and what the rating values for these levels are! – Suppose we know that rating values go from 1 to 10; we can write 10 queries that look like this (!): SELECT MIN (S.age) FROM Sailors S WHERE S.rating = i For i = 1, 2,..., 10:

10 INFS614 - Lecture week 9 10 Queries With GROUP BY and HAVING v The target-list contains (i) attribute names (ii) terms with aggregate operations (e.g., MIN (S.age)). – The attribute list (i) must be a subset of grouping-list. – Intuitively, each answer tuple corresponds to a group, and these attributes must have a single value per group. (A group is a set of tuples that have the same value for all attributes in grouping-list.) SELECT [DISTINCT] target-list FROM relation-list WHERE qualification GROUP BY grouping-list [HAVING group-qualification]

11 INFS614 - Lecture week 9 11 Conceptual Evaluation v The cross-product of relation-list is computed, tuples that fail qualification are discarded, `unnecessary’ fields are deleted, and the remaining tuples are partitioned into groups by the value of attributes in grouping-list. v The group-qualification is then applied to eliminate some groups. Expressions in group-qualification must have a single value per group! – In effect, an attribute in group-qualification that is not an argument of an aggregate op also appears in grouping-list. (SQL does not exploit primary key semantics here!) v One answer tuple is generated per qualifying group.

12 INFS614 - Lecture week 9 12 Query with GROUP BY and HAVING v Query: Find the age of the youngest sailor for each rating level. SELECT S.rating, MIN (S.age) FROM Sailors S GROUP BY S.rating

13 INFS614 - Lecture week 9 13 Find the age of the youngest sailor older than 18, for each rating with at least 2 such sailors older than 18 Only S.rating and S.age are mentioned in the SELECT, GROUP BY or HAVING clauses; other attributes `unnecessary’. 2nd column of result is unnamed. (Use AS to name it.) SELECT S.rating, MIN (S.age) FROM Sailors S WHERE S.age > 18 GROUP BY S.rating HAVING COUNT (*) > 1 Answer relation

14 INFS614 - Lecture week 9 14 For each red boat, find the number of reservations for this boat v Grouping over a join of two relations. v What do we get if we remove B.color=‘red’ from the WHERE clause and add a HAVING clause with this condition? SELECT B.bid, COUNT (*) AS rescount FROM Boats B, Reserves R WHERE R.bid=B.bid AND B.color=‘red’ GROUP BY B.bid

15 INFS614 - Lecture week 9 15 Find the age of the youngest sailor with age > 18, for each rating with at least 2 sailors (of any age) HAVING clause can also contain a correlated subquery. Compare this with the query where we considered only ratings with 2 sailors over 18! What if HAVING clause is replaced by: – HAVING COUNT(*) >1 SELECT S.rating, MIN (S.age) FROM Sailors S WHERE S.age > 18 GROUP BY S.rating HAVING 1 < ( SELECT COUNT (*) FROM Sailors S2 WHERE S.rating=S2.rating) Computes total number of sailors who have rating equal to S.rating

16 INFS614 - Lecture week 9 16 For each sailor with more than three reservations, find the number of his reservations SELECT R.sid, COUNT (*) AS rescount FROM Reserves R GROUP BY R.sid HAVING COUNT(*) > 3 Find the age of the youngest sailor, for each rating level > 5 with an average age > 25 SELECT S.rating, MIN(S.age) AS minAge FROM Sailors S WHERE S.rating > 5 GROUP BY S.rating HAVING AVG(S.age) > 25 SELECT S.rating, MIN(S.age) AS minAge FROM Sailors S GROUP BY S.rating HAVING S.rating > 5 AND AVG(S.age) > 25

17 INFS614 - Lecture week 9 17 Find those ratings for which the average age of sailors is the minimum over all ratings  Aggregate operations cannot be nested! WRONG: SELECT S.rating FROM Sailors S WHERE AVG(S.age) = ( SELECT MIN ( AVG (S2.age)) FROM Sailors S2 GROUP BY S2.rating)

18 INFS614 - Lecture week 9 18 Continue from previous slide v Correct solution (in SQL/92): SELECT Temp.rating, Temp.avgage FROM ( SELECT S.rating, AVG (S.age) AS avgage FROM Sailors S GROUP BY S.rating) AS Temp WHERE Temp.avgage = ( SELECT MIN (Temp.avgage) FROM Temp)

19 INFS614 - Lecture week 9 19 Note on Oracle SQL in the labs Do not use the keyword “AS” to name a temporary table within the FROM clause.

20 INFS614 - Lecture week 9 20 Note on Oracle SQL in the labs SELECT Temp.rating, Temp.avgage FROM ( SELECT S.rating, AVG (S.age) AS avgage FROM Sailors S GROUP BY S.rating) AS Temp WHERE Temp.avgage = ( SELECT MIN (Temp.avgage) FROM Temp)

21 INFS614 - Lecture week 9 21 Conclusions (so far…) v Post processing on the result of queries is supported. v Aggregation is the most complex “post processing” –“Group by” clause partitions the results into groups –“Having” clause puts condition on groups (just like Where clause on tuples).

22 INFS614 - Lecture week 9 22 Null Values v Field values in a tuple are sometimes unknown (e.g., a rating has not been assigned) or inapplicable (e.g., no spouse’s name). v SQL provides a special value null for such situations.

23 INFS614 - Lecture week 9 23 Dealing with the null value v Special operators are needed to check if value is/is not null. –Attribute_name IS NULL : TRUE if the attribute’s value is null. FALSE otherwise –Attribute_name IS NOT NULL : TRUE if the attribute’s value is not null. FALSE otherwise  Is rating>8 true or false when rating is equal to null? – Actually, it’s unknown.  How about: rating > 8 AND age < 40 ? –Three-valued logic.

24 INFS614 - Lecture week 9 24 Three Valued Logic  Logical operators AND, OR and NOT using a 3- valued logic ( TRUE, FALSE and unknown ).

25 INFS614 - Lecture week 9 25 Null Values: Some issues The presence of null complicates many issues: v WHERE clause eliminates tuples for which the qualification does not evaluate to TRUE. Important in nested queries involving EXISTS or UNIQUE v Two tuples in SQL are duplicates in a relation if corresponding attributes have the same value or both contains null. SELECT S1.sname FROM SAILORS S1 WHERE EXISTS ( SELECT * FROM SAILORS S2 WHERE S1.sname = S2.sname AND AND S1.SID < S2.SID AND S1.RATING <> S2.RATING)

26 INFS614 - Lecture week 9 26 Issues with the null value: Summary  WHERE and HAVING clause eliminates rows (groups) for which the qualification does not evaluate to true (i.e., evaluate to false or unknown). v Aggregate functions ignore null values (except count(*)). v Distinct treats all null values as the same.  The arithmetic operations +, -, *, / return null if one of the arguments is null.

27 INFS614 - Lecture week 9 27 Outer joins ( left outer-join ) =

28 INFS614 - Lecture week 9 28 In Oracle Select * From Sailor S, Reserves R Where S.sid = R.sid (+); How about: Select S.sid, count(R.bid) From Sailor S, Reserves R Where S.sid = R.sid (+) Group by S.sid; OR Select S.sid, count(*) From Sailor S, Reserves R Where S.sid = R.sid (+) Group by S.sid;

29 INFS614 - Lecture week 9 29 More outer joins v Left outer join + sign on the right in Oracle: Select * from R, S where R.id=S.id(+) v Right outer join + sign on the left in Oracle: Select * from R, S where R.id(+)=S.id v Full outer join –not implemented in Oracle

30 INFS614 - Lecture week 9 30 More on value functions v Values can be transformed before aggregated: e.g.: SELECT sum(S.A/2) FROM S ; v An interesting decode function (Oracle specific): decode(value, if1, then1, if2, then2, …, else): SELECT sum(decode(Major, ‘INFS’, 1, 0)) as No_IS_Stu, sum(decode(Major, ‘INFS’, 0, 1)) as No_NonIS_Stu FROM Student ; v Calculating GPA from letter grades?

31 INFS614 - Lecture week 9 31 Examples Department (D-code, D-Name, Chair-SSn) Course (D-code, C-no, Title, Units) Prereq (D-code, C-no, P-code, P-no) Class (Class-no, D-code, C-no, Instructor-SSn) Faculty (Ssn, F-Name, D-Code, Rank) Student (Ssn, S-Name, Major, Status) Enrollment (Class-no, Student-Ssn) Transcript (Student-Ssn, D-Code, C-no, Grade)

32 INFS614 - Lecture week 9 32 Query 1 List the classes (Class-no) currently taken by students whose names start with 'T'. SELECT distinct e.Class-no FROM Enrollment e, Student s WHERE e.Student-Ssn = s.Ssn AND s.S-Name LIKE 'T%' Department (D-code, D-Name, Chair-SSn) Course (D-code, C-no, Title, Units) Prereq (D-code, C-no, P-code, P-no) Class (Class-no, D-code, C-no, Instructor-SSn) Faculty (Ssn, F-Name, D-Code, Rank) Student (Ssn, S-Name, Major, Status) Enrollment (Class-no, Student-Ssn) Transcript (Student-Ssn, D-Code, C-no, Grade)

33 INFS614 - Lecture week 9 33 Query 2 List the students (SSN) who are currently taking exactly one class. SELECT e.Student-Ssn FROM Enrollment e GROUP BY e.Student-Ssn HAVING 1=count(*) Department (D-code, D-Name, Chair-SSn) Course (D-code, C-no, Title, Units) Prereq (D-code, C-no, P-code, P-no) Class (Class-no, D-code, C-no, Instructor-SSn) Faculty (Ssn, F-Name, D-Code, Rank) Student (Ssn, S-Name, Major, Status) Enrollment (Class-no, Student-Ssn) Transcript (Student-Ssn, D-Code, C-no, Grade)

34 INFS614 - Lecture week 9 34 Query 3 Give the percentage of the students (among all students) who are currently taking courses offered by ISE (D-code='ISE'). SELECT count(distinct e.Student-Ssn)/count(distinct s.Ssn) as Percent FROM Enrollment e, Class c, Student s WHERE e.Class-no=c.Class-no and c.D-code='ISE'; Department (D-code, D-Name, Chair-SSn) Course (D-code, C-no, Title, Units) Prereq (D-code, C-no, P-code, P-no) Class (Class-no, D-code, C-no, Instructor-SSn) Faculty (Ssn, F-Name, D-Code, Rank) Student (Ssn, S-Name, Major, Status) Enrollment (Class-no, Student-Ssn) Transcript (Student-Ssn, D-Code, C-no, Grade)

35 INFS614 - Lecture week 9 35 Query 4 List the faculty members (F-Name) who teach 2 or more classes. List these faculty members by the number of classes they teach (ascending order). SELECT f.F-Name FROM Faculty f, Class c WHERE f.Ssn=c.Instructor-SSn GROUP BY f.Ssn, f.F-Name HAVING count(distinct c.Class-no)>=2 ORDER BY count(distinct c.Class-no), F-Name; Department (D-code, D-Name, Chair-SSn) Course (D-code, C-no, Title, Units) Prereq (D-code, C-no, P-code, P-no) Class (Class-no, D-code, C-no, Instructor-SSn) Faculty (Ssn, F-Name, D-Code, Rank) Student (Ssn, S-Name, Major, Status) Enrollment (Class-no, Student-Ssn) Transcript (Student-Ssn, D-Code, C-no, Grade)

36 INFS614 - Lecture week 9 36 Query 5 List the students (SSN and Name) along with the number of classes they are taking. If a student is not taking any class, the student should also be listed (with 0 as the number of classes he/she is taking). The list should be ordered by the number of classes (in an ascending order), and in case of a tie, by the SSN of the students. SELECT s.Ssn, s.S-Name, count(distinct Class-no) FROM Student s, Enrollment e WHERE s.Ssn = e.Student-Ssn (+) GROUP BY s.Ssn, s.S-Name ORDER BY count(distinct Class-no), Ssn Department (D-code, D-Name, Chair-SSn) Course (D-code, C-no, Title, Units) Prereq (D-code, C-no, P-code, P-no) Class (Class-no, D-code, C-no, Instructor-SSn) Faculty (Ssn, F-Name, D-Code, Rank) Student (Ssn, S-Name, Major, Status) Enrollment (Class-no, Student-Ssn) Transcript (Student-Ssn, D-Code, C-no, Grade)

37 INFS614 - Lecture week 9 37 Query 6 List the faculty members (F-Name) who teach more than twice as many classes as Professor Smith (F-Name='Smith') is teaching. (Note that if Professor Smith is not teaching anything, then any professor who teaches at least one class will satisfy the above query.) SELECT f.F-Name FROM Faculty f, Class c WHERE f.Ssn=c.Instructor-SSn GROUP BY f.Ssn, f.F-Name HAVING count(distinct c.Class-no) > (SELECT 2*count(distinct Class-no) FROM Faculty f, Class c WHERE f.F-Name='Smith' and f.Ssn=c.Instructor-SSn) Department (D-code, D-Name, Chair-SSn) Course (D-code, C-no, Title, Units) Prereq (D-code, C-no, P-code, P-no) Class (Class-no, D-code, C-no, Instructor-SSn) Faculty (Ssn, F-Name, D-Code, Rank) Student (Ssn, S-Name, Major, Status) Enrollment (Class-no, Student-Ssn) Transcript (Student-Ssn, D-Code, C-no, Grade)

38 INFS614 - Lecture week 9 38 Query 7 Find the number of departments which do not have a chairman (Chair_ssn is 'NULL'). SELECT count(d.D-code) FROM Department d WHERE d.Chair-SSn is NULL Department (D-code, D-Name, Chair-SSn) Course (D-code, C-no, Title, Units) Prereq (D-code, C-no, P-code, P-no) Class (Class-no, D-code, C-no, Instructor-SSn) Faculty (Ssn, F-Name, D-Code, Rank) Student (Ssn, S-Name, Major, Status) Enrollment (Class-no, Student-Ssn) Transcript (Student-Ssn, D-Code, C-no, Grade)

39 INFS614 - Lecture week 9 39 Query 8 For each department (i.e., student’s Major), give the number of graduate students (status='Grad') and the number of other students (status <> 'Grad'). The two numbers must be shown in the same row as the department code. Hint: use decode. SELECT Major, sum(decode(Status, 'Grad', 1,0)) as Grad, sum(decode(Status, 'Grad', 0,1)) as NoGrad FROM Student GROUP BY Major Department (D-code, D-Name, Chair-SSn) Course (D-code, C-no, Title, Units) Prereq (D-code, C-no, P-code, P-no) Class (Class-no, D-code, C-no, Instructor-SSn) Faculty (Ssn, F-Name, D-Code, Rank) Student (Ssn, S-Name, Major, Status) Enrollment (Class-no, Student-Ssn) Transcript (Student-Ssn, D-Code, C-no, Grade)

40 INFS614 - Lecture week 9 40 Query 9 For each department (D-code), give the highest rank of the professors in the department along with the number of the faculty with that highest rank. The output contains one row for each department. Assume Full>Associate>Assistant, i.e., lexicographic order is fine. Note that some department may not have professors in some ranks (e.g., a department may not have full, or associate or assistant professors). SELECT f.D-Code as dept, f.maxrank, count(e.Ssn) as num FROM (SELECT D-code, max(Rank) as maxrank FROM Faculty GROUP BY D-code) AS f, Faculty e WHERE f.D-code=e.D-code AND e.Rank=f.maxrank GROUP BY f.D-code, f.maxrank ORDER BY f.D-code


Download ppt "INFS614 - Lecture week 9 1 More on SQL Lecture Week 9 INFS 614, Fall 2008."

Similar presentations


Ads by Google