1 / 28

IT420: Database Management and Organization

Learn advanced SQL query techniques, including data manipulation, aggregate operators, subqueries, and joins. Practice exercises included for hands-on learning.

Télécharger la présentation

IT420: Database Management and Organization

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. IT420: Database Management and Organization SQL part 3 7 February 2006 Adina Crăiniceanu www.cs.usna.edu/~adina

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

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

  4. Today • More about SELECT

  5. The SQL SELECT Statement • Basic SQL Query: SELECT [DISTINCT] column_name(s) FROM table_name(s) [WHERE conditions] [ORDER BY some_column_names [ASC/DESC] ] *Terms between [ ] are optional

  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. Calculations in SQL • Simple arithmetic • Five SQL aggregate operators: • COUNT • SUM • AVG • MIN • MAX

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

  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. 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. 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. 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. GROUP BY Exercise • Students(Alpha, LName, FName, Class, Age) • For each last name, find the number of students with same last name

  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. Example • SELECT Class, MIN(Age) FROM Students WHERE MajDeptName = ‘ComSci’ GROUP BY Class HAVING Class > 2 What does the query compute?

  16. Another GROUP BY Example • SKU_DATA(SKU, SKU_description, Buyer, Department) • SELECT Department, COUNT(*) AS Dept_SKU_Count FROM SKU_DATA WHERE SKU <> 302000 GROUP BY Department HAVING COUNT (*) > 1 ORDER BY Dept_SKU_Count;

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

  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. Subqueries Exercise • Students(Alpha, LName, FName, Class, Age) • Enroll(Alpha, CourseID, Semester, Grade) • Find alpha for students enrolled in both ‘IT420’ and ‘IT334’ • Find name of students enrolled in both ‘IT420’ and ‘IT334’

  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. SELECT FName, LName FROM Students S WHERE S.Alpha IN (SELECT Alpha FROM Enroll E WHERE E.CourseID = ‘IT420’)

  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. 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. 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. 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. Multiple JOIN ON • Find the names of students enrolled in ‘Database Management’ • SELECT LName, FName FROM Enroll E JOIN Courses C ON E.CourseID = C.CourseID JOIN Students ON E.Alpha = S.Alpha WHERE C.Description = ‘Database Management’

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

More Related