Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 IT420: Database Management and Organization SQL part 3 7 February 2006 Adina Crăiniceanu www.cs.usna.edu/~adina.

Similar presentations


Presentation on theme: "1 IT420: Database Management and Organization SQL part 3 7 February 2006 Adina Crăiniceanu www.cs.usna.edu/~adina."— Presentation transcript:

1 1 IT420: Database Management and Organization SQL part 3 7 February 2006 Adina Crăiniceanu www.cs.usna.edu/~adina

2 2 Announcements  Exam next Tuesday  2 hours  Closed book/closed notes  No computers  Covers all material  Labs returned on Friday

3 3 Previously  SQL DDL: Data Definition Language  CREATE, DROP, ALTER  DML: Data Manipulation Language  INSERT  DELETE  UPDATE  SELECT

4 4 Today  More about SELECT

5 5 The SQL SELECT Statement  Basic SQL Query: SELECT[DISTINCT] column_name(s) FROMtable_name(s) [WHEREconditions] [ORDER BYsome_column_names [ASC/DESC] ] *Terms between [ ] are optional

6 6 WHERE Clause Options  AND, OR  IN, NOT IN, BETWEEN  =, >,, >= SELECT SNb FROM Students S, Enrolled E WHERE S.SNb = E.Nb AND E.Cid NOT IN (‘ComSci’, ‘Math’)

7 7 Calculations in SQL  Simple arithmetic  Five SQL aggregate operators:  COUNT  SUM  AVG  MIN  MAX

8 8 Find the age of the youngest student  Students(Alpha, LName, FName, Class, Age)  SELECT MIN(Age) FROM Students

9 9 Aggregate Operators  So far, aggregate operations applied to all (qualifying) rows  We want to apply them to each of several groups of rows  Students(Alpha, LName, SName, Class, Age)  Find the age of the youngest student for each class

10 10 Example  Students(Alpha, LName, FName, Class, Age)  Find the age of the youngest student for each class  If class values go from 1 to 4 we can write 4 queries that look like this:  SELECT MIN (S.Age) FROM Students S WHERE S.Class = i

11 11 GROUP-BY Clause  SELECT grouping_columns(s), aggregates FROM table_name(s) [WHERE conditions] GROUP BY grouping_columns  SELECT Class, MIN(Age) FROM Students GROUP BY Class

12 12 Conceptual Evaluation  SQL query semantics:  Compute the cross-product of table_names  Discard resulting rows if they fail conditions  Delete columns that are not specified in SELECT  Remaining rows are partitioned into groups by the value of the columns in grouping-columns  One answer row is generated per group  Note: Does not imply query will actually be evaluated this way!

13 13 GROUP BY Exercise  Students(Alpha, LName, FName, Class, Age)  For each last name, find the number of students with same last name

14 14 HAVING Clause  SELECT [grouping_columns(s), aggregates FROM table_name(s) [WHERE conditions] GROUP BY grouping_columns HAVING group_conditions  HAVING restricts the groups presented in the result

15 15 Example  SELECT Class, MIN(Age) FROM Students WHERE MajDeptName = ‘ComSci’ GROUP BY Class HAVING Class > 2 What does the query compute?

16 16 Another GROUP BY Example  SKU_DATA(SKU, SKU_description, Buyer, Department)  SELECTDepartment, COUNT(*) AS Dept_SKU_Count FROMSKU_DATA WHERESKU <> 302000 GROUP BY Department HAVING COUNT (*) > 1 ORDER BYDept_SKU_Count;

17 17 Select students with age higher than average  SELECT * FROM Students WHERE Age > AVG(Age) Illegal!

18 18 Subqueries  SELECT * FROM Students WHERE Age > (SELECT AVG(Age) FROM Students)  Second select is a subquery (or nested query)  You can have subqueries in FROM or HAVING clause also

19 19 Subqueries Exercise  Students(Alpha, LName, FName, Class, Age)  Enroll(Alpha, CourseID, Semester, Grade) 1.Find alpha for students enrolled in both ‘IT420’ and ‘IT334’ 2.Find name of students enrolled in both ‘IT420’ and ‘IT334’

20 20 Class Exercise  Students(Alpha, LName, FName, Class, Age)  Enroll(Alpha, CourseID, Semester, Grade)  Find the name of students enrolled in ‘IT420’  Usual way  Use subqueries

21 21  SELECT FName, LName FROM Students S WHERE S.Alpha IN (SELECT Alpha FROM Enroll E WHERE E.CourseID = ‘IT420’)

22 22 Correlated Subqueries  SELECT FName, LName FROM Students S WHERE EXISTS (SELECT * FROM Enroll E WHERE E.CourseID = ‘IT420’ AND E.Alpha = S.Alpha)

23 23 Subqueries versus Joins  Subqueries and joins both process multiple tables.  Subquery can only be used to retrieve data from the top table.  Join can be used to obtain data from any number of tables  Correlated subquery can do work that is not possible with joins.

24 24 Class Exercise  What does this query compute:  SELECT FName, LName FROM Students S, Enroll E1, Enroll E2 WHERE S.Alpha = E1.Alpha AND S.Alpha = E2.Alpha AND E1.CourseID = ‘IT420’ AND E2.CourseID = ‘IT344’

25 25 JOIN ON Syntax  Students(Alpha, LName, FName, Class, Age)  Courses(CourseID, Description, Textbook)  Enroll(Alpha, CourseID, Semester, Grade)  Find the names of students enrolled in ‘IT420’ SELECT LName, FName FROM Students S JOIN Enroll C ON S.Alpha = C.Alpha WHERE CourseID = ‘IT420’  Find the names of students enrolled in ‘Database Management’

26 26 Multiple JOIN ON  Find the names of students enrolled in ‘Database Management’  SELECTLName, FName FROMEnroll E JOIN Courses C ON E.CourseID = C.CourseID JOIN Students ON E.Alpha = S.Alpha WHERE C.Description = ‘Database Management’

27 27 Outer Joins  Find all students and courses in which they are enrolled  SELECT S.Alpha, S.LName, S.FName, E.CourseID FROM Students S LEFT JOIN Enrolled E ON S.Alpha = E.Alpha

28 28 Class Exercise  Students(Alpha, LName, FName, Class, Age)  Courses(CourseID, Description, Textbook)  Enroll(Alpha, CourseID, Semester, Grade)  Find the age of youngest student older than 18, for each course with at least one such student enrolled in it


Download ppt "1 IT420: Database Management and Organization SQL part 3 7 February 2006 Adina Crăiniceanu www.cs.usna.edu/~adina."

Similar presentations


Ads by Google