Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos & A. Pavlo Lecture#6: Rel. model - SQL part1 (R&G, chapter.

Similar presentations


Presentation on theme: "CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos & A. Pavlo Lecture#6: Rel. model - SQL part1 (R&G, chapter."— Presentation transcript:

1 CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos & A. Pavlo Lecture#6: Rel. model - SQL part1 (R&G, chapter 5)

2 CMU SCS Faloutsos, PavloCMU SCS 15-415/6152 General Overview - rel. model Formal query languages –rel algebra and calculi Commercial query languages –SQL –QBE, (QUEL)

3 CMU SCS Faloutsos, PavloCMU SCS 15-415/6153 Overview - detailed - SQL DML –select, from, where, renaming –set operations –ordering –aggregate functions –nested subqueries other parts: DDL, embedded SQL, auth etc

4 CMU SCS Faloutsos, PavloCMU SCS 15-415/6154 Relational Query Languages A major strength of the relational model: supports simple, powerful querying of data. Two sublanguages: DDL – Data Definition Language –define and modify schema (at all 3 levels) DML – Data Manipulation Language –Queries can be written intuitively.

5 CMU SCS Faloutsos, PavloCMU SCS 15-415/6155 Relational languages The DBMS is responsible for efficient evaluation. –Query optimizer: re-orders operations and generates query plan

6 CMU SCS Faloutsos, PavloCMU SCS 15-415/6156 The SQL Query Language The most widely used relational query language. – Major standard is SQL-1999 (=SQL3) Introduced “Object-Relational” concepts SQL 2003, SQL 2008 have small extensions – SQL92 is a basic subset

7 CMU SCS Faloutsos, PavloCMU SCS 15-415/6157 SQL (cont’d) –PostgreSQL has some “unique” aspects (as do most systems).

8 CMU SCS Faloutsos, PavloCMU SCS 15-415/6158 DML General form select a1, a2, … an from r1, r2, … rm where P [order by ….] [group by …] [having …]

9 CMU SCS Faloutsos, PavloCMU SCS 15-415/6159 Reminder: our Mini-U db

10 CMU SCS Faloutsos, PavloCMU SCS 15-415/61510 DML - eg: find the ssn(s) of everybody called “smith” select ssn from student where name=“smith”

11 CMU SCS Faloutsos, PavloCMU SCS 15-415/61511 DML - observation General form select a1, a2, … an from r1, r2, … rm where P equivalent rel. algebra query?

12 CMU SCS Faloutsos, PavloCMU SCS 15-415/61512 DML - observation General form select a1, a2, … an from r1, r2, … rm where P

13 CMU SCS Faloutsos, PavloCMU SCS 15-415/61513 DML - observation General form select distinct a1, a2, … an from r1, r2, … rm where P

14 CMU SCS Faloutsos, PavloCMU SCS 15-415/61514 select clause select [distinct | all ] name from student where address=“main”

15 CMU SCS Faloutsos, PavloCMU SCS 15-415/61515 where clause find ssn(s) of all “smith”s on “main” select ssn from student where address=“main” and name = “smith”

16 CMU SCS Faloutsos, PavloCMU SCS 15-415/61516 where clause boolean operators (and or not …) comparison operators (, =, …) and more…

17 CMU SCS Faloutsos, PavloCMU SCS 15-415/61517 What about strings? find student ssns who live on “main” (st or str or street - ie., “main st” or “main str” …)

18 CMU SCS Faloutsos, PavloCMU SCS 15-415/61518 What about strings? find student ssns who live on “main” (st or str or street) select ssn from student where address like “main%” %: variable-length don’t care _: single-character don’t care

19 CMU SCS Faloutsos, PavloCMU SCS 15-415/61519 from clause find names of people taking 15-415

20 CMU SCS Faloutsos, PavloCMU SCS 15-415/61520 from clause find names of people taking 15-415 select name from student, takes where ???

21 CMU SCS Faloutsos, PavloCMU SCS 15-415/61521 from clause find names of people taking 15-415 select name from student, takes where student.ssn = takes.ssn and takes.c-id = “15-415”

22 CMU SCS Faloutsos, PavloCMU SCS 15-415/61522 Overview - detailed - SQL DML –select, from, where, renaming –set operations –ordering –aggregate functions –nested subqueries other parts: DDL, embedded SQL, auth etc

23 CMU SCS Faloutsos, PavloCMU SCS 15-415/61523 renaming - tuple variables find names of people taking 15-415 select name from ourVeryOwnStudent, studentTakingClasses where ourVeryOwnStudent.ssn = studentTakingClasses.ssn and studentTakingClasses.c-id = “15-415”

24 CMU SCS 2 reasons to rename: #1) shorthand #2) self-joins Faloutsos, PavloCMU SCS 15-415/61524

25 CMU SCS Faloutsos, PavloCMU SCS 15-415/61525 #1) renaming - tuple variables find names of people taking 15-415 select name from ourVeryOwnStudent, studentTakingClasses where ourVeryOwnStudent.ssn =studentTakingClasses.ssn and studentTakingClasses.c-id = “15-415”

26 CMU SCS Faloutsos, PavloCMU SCS 15-415/61526 #1) renaming - tuple variables find names of people taking 15-415 select name from ourVeryOwnStudent as S, studentTakingClasses as T where S.ssn =T.ssn and T.c-id = “15-415”

27 CMU SCS Faloutsos, PavloCMU SCS 15-415/61527 #2) renaming - self-join self -joins: find Tom’s grandparent(s)

28 CMU SCS Faloutsos, PavloCMU SCS 15-415/61528 #2) renaming - self-join find grandparents of “Tom” (PC(p-id, c-id)) select gp.p-id from PC as gp, PC where gp.c-id= PC.p-id and PC.c-id = “Tom”

29 CMU SCS Faloutsos, PavloCMU SCS 15-415/61529 #2) renaming - theta join find course names with more units than 15- 415

30 CMU SCS Faloutsos, PavloCMU SCS 15-415/61530 #2) renaming - theta join find course names with more units than 15- 415 select c1.c-name from class as c1, class as c2 where c1.units > c2.units and c2.c-id = “15-415”

31 CMU SCS Faloutsos, PavloCMU SCS 15-415/61531 find course names with more units than 15-415 select c1.name from class as c1, class as c2 where c1.units > c2.units and c2.c-id = “15-415”

32 CMU SCS Faloutsos, PavloCMU SCS 15-415/61532 find course names with more units than 15-415 select c2.name from class as c1, class as c2 where c2.units > c1.units and c1.c-id = “15-415”

33 CMU SCS Faloutsos, PavloCMU SCS 15-415/61533 Overview - detailed - SQL DML –select, from, where –set operations –ordering –aggregate functions –nested subqueries other parts: DDL, embedded SQL, auth etc

34 CMU SCS Faloutsos, PavloCMU SCS 15-415/61534 set operations find ssn of people taking both 15-415 and 15- 413

35 CMU SCS Faloutsos, PavloCMU SCS 15-415/61535 set operations find ssn of people taking both 15-415 and 15- 413 select ssn from takes where c-id=“15-415” and c-id=“15-413”

36 CMU SCS Faloutsos, PavloCMU SCS 15-415/61536 set operations find ssn of people taking both 15-415 and 15- 413 (select ssn from takes where c-id=“15-415” ) intersect (select ssn from takes where c-id=“15-413” ) other ops: union, except

37 CMU SCS Faloutsos, PavloCMU SCS 15-415/61537 Overview - detailed - SQL DML –select, from, where –set operations –ordering –aggregate functions –nested subqueries other parts: DDL, embedded SQL, auth etc

38 CMU SCS Faloutsos, PavloCMU SCS 15-415/61538 Ordering find student records, sorted in name order select * from student where

39 CMU SCS Faloutsos, PavloCMU SCS 15-415/61539 Ordering find student records, sorted in name order select * from student order by name asc asc is the default

40 CMU SCS Faloutsos, PavloCMU SCS 15-415/61540 Ordering find student records, sorted in name order; break ties by reverse ssn select * from student order by name, ssn desc

41 CMU SCS Faloutsos, PavloCMU SCS 15-415/61541 Overview - detailed - SQL DML –select, from, where –set operations –ordering –aggregate functions –nested subqueries other parts: DDL, embedded SQL, auth etc

42 CMU SCS Faloutsos, PavloCMU SCS 15-415/61542 Aggregate functions find avg grade, across all students select ?? from takes

43 CMU SCS Faloutsos, PavloCMU SCS 15-415/61543 Aggregate functions find avg grade, across all students select avg(grade) from takes result: a single number Which other functions?

44 CMU SCS Faloutsos, PavloCMU SCS 15-415/61544 Aggregate functions A: sum count min max (std)

45 CMU SCS Faloutsos, PavloCMU SCS 15-415/61545 Aggregate functions find total number of enrollments select count(*) from takes

46 CMU SCS Faloutsos, PavloCMU SCS 15-415/61546 Aggregate functions find total number of students in 15-415 select count(*) from takes where c-id=“15-415”

47 CMU SCS Faloutsos, PavloCMU SCS 15-415/61547 Aggregate functions find total number of students in each course select count(*) from takes where ???

48 CMU SCS Faloutsos, PavloCMU SCS 15-415/61548 Aggregate functions find total number of students in each course select c-id, count(*) from takes group by c-id

49 CMU SCS Faloutsos, PavloCMU SCS 15-415/61549 Aggregate functions find total number of students in each course select c-id, count(*) from takes group by c-id order by c-id

50 CMU SCS Faloutsos, PavloCMU SCS 15-415/61550 Aggregate functions find total number of students in each course, and sort by count, decreasing select c-id, count(*) as pop from takes group by c-id order by pop desc

51 CMU SCS Faloutsos, PavloCMU SCS 15-415/61551 Aggregate functions- ‘having’ find students with GPA > 3.0

52 CMU SCS Faloutsos, PavloCMU SCS 15-415/61552 Aggregate functions- ‘having’ find students with GPA > 3.0 select ???, avg(grade) from takes group by ???

53 CMU SCS Faloutsos, PavloCMU SCS 15-415/61553 Aggregate functions- ‘having’ find students with GPA > 3.0 select ssn, avg(grade) from takes group by ssn ???

54 CMU SCS Faloutsos, PavloCMU SCS 15-415/61554 Aggregate functions- ‘having’ find students with GPA > 3.0 select ssn, avg(grade) from takes group by ssn having avg(grade)>3.0 ‘having’ ‘where’ for groups

55 CMU SCS Faloutsos, PavloCMU SCS 15-415/61555 Aggregate functions- ‘having’ find students and GPA, for students with > 5 courses select ssn, avg(grade) from takes group by ssn having count(*) > 5

56 CMU SCS FaloutsosCMU SCS 15-415/61556 Overview - detailed - SQL DML –select, from, where, renaming –set operations –ordering –aggregate functions –nested subqueries other parts: DDL, embedded SQL, auth etc

57 CMU SCS FaloutsosCMU SCS 15-415/61557 DML General form select a1, a2, … an from r1, r2, … rm where P [order by ….] [group by …] [having …]

58 CMU SCS FaloutsosCMU SCS 15-415/61558 Reminder: our Mini-U db

59 CMU SCS FaloutsosCMU SCS 15-415/61559 DML - nested subqueries find names of students of 15-415 select name from student where... “ssn in the set of people that take 15-415”

60 CMU SCS FaloutsosCMU SCS 15-415/61560 DML - nested subqueries find names of students of 15-415 select name from student where ………... select ssn from takes where c-id =“15-415”

61 CMU SCS FaloutsosCMU SCS 15-415/61561 DML - nested subqueries find names of students of 15-415 select name from student where ssn in ( select ssn from takes where c-id =“15-415”)

62 CMU SCS FaloutsosCMU SCS 15-415/61562 DML - nested subqueries ‘in’ compares a value with a set of values ‘in’ can be combined other boolean ops it is redundant (but user friendly!): select name from student ….. where c-id = “15-415” ….

63 CMU SCS FaloutsosCMU SCS 15-415/61563 DML - nested subqueries ‘in’ compares a value with a set of values ‘in’ can be combined other boolean ops it is redundant (but user friendly!): select name from student, takes where c-id = “15-415” and student.ssn=takes.ssn

64 CMU SCS FaloutsosCMU SCS 15-415/61564 DML - nested subqueries find names of students taking 15-415 and living on “main str” select name from student where address=“main str” and ssn in (select ssn from takes where c-id =“15-415”)

65 CMU SCS FaloutsosCMU SCS 15-415/61565 DML - nested subqueries ‘in’ compares a value with a set of values other operators like ‘in’ ??

66 CMU SCS FaloutsosCMU SCS 15-415/61566 DML - nested subqueries find student record with highest ssn select * from student where ssn is greater than every other ssn

67 CMU SCS FaloutsosCMU SCS 15-415/61567 DML - nested subqueries find student record with highest ssn select * from student where ssn greater than every select ssn from student

68 CMU SCS FaloutsosCMU SCS 15-415/61568 DML - nested subqueries find student record with highest ssn select * from student where ssn > all ( select ssn from student) almost correct

69 CMU SCS FaloutsosCMU SCS 15-415/61569 DML - nested subqueries find student record with highest ssn select * from student where ssn >= all ( select ssn from student)

70 CMU SCS FaloutsosCMU SCS 15-415/61570 DML - nested subqueries find student record with highest ssn - without nested subqueries? select S1.ssn, S1.name, S1.address from student as S1, student as S2 where S1.ssn > S2.ssn is not the answer (what does it give?) Puzzle

71 CMU SCS FaloutsosCMU SCS 15-415/61571 DML - nested subqueries S1 S2 S1 x S2 S1.ssn>S2.ssn Puzzle

72 CMU SCS FaloutsosCMU SCS 15-415/61572 DML - nested subqueries select S1.ssn, S1.name, S1.address from student as S1, student as S2 where S1.ssn > S2.ssn gives all but the smallest ssn - aha! Puzzle

73 CMU SCS FaloutsosCMU SCS 15-415/61573 DML - nested subqueries find student record with highest ssn - without nested subqueries? select S1.ssn, S1.name, S1.address from student as S1, student as S2 where S1.ssn < S2.ssn gives all but the highest - therefore…. Puzzle

74 CMU SCS FaloutsosCMU SCS 15-415/61574 DML - nested subqueries find student record with highest ssn - without nested subqueries? (select * from student) except (select S1.ssn, S1.name, S1.address from student as S1, student as S2 where S1.ssn < S2.ssn) Puzzle

75 CMU SCS FaloutsosCMU SCS 15-415/61575 DML - nested subqueries (select * from student) except (select S1.ssn, S1.name, S1.address from student as S1, student as S2 where S1.ssn < S2.ssn) select * from student where ssn >= all (select ssn from student) Puzzle

76 CMU SCS FaloutsosCMU SCS 15-415/61576 DML - nested subqueries Drill: Even more readable than select * from student where ssn >= all (select ssn from student)

77 CMU SCS FaloutsosCMU SCS 15-415/61577 DML - nested subqueries Drill: Even more readable than select * from student where ssn >= all (select ssn from student) select * from student where ssn in (select max(ssn) from student)

78 CMU SCS FaloutsosCMU SCS 15-415/61578 DML - nested subqueries Drill: find the ssn of the student with the highest GPA

79 CMU SCS FaloutsosCMU SCS 15-415/61579 DML - nested subqueries Drill: find the ssn and GPA of the student with the highest GPA select ssn, avg(grade) from takes where

80 CMU SCS FaloutsosCMU SCS 15-415/61580 DML - nested subqueries Drill: find the ssn and GPA of the student with the highest GPA select ssn, avg(grade) from takes group by ssn having avg( grade) …... greater than every other GPA on file

81 CMU SCS FaloutsosCMU SCS 15-415/61581 DML - nested subqueries Drill: find the ssn and GPA of the student with the highest GPA select ssn, avg(grade) from takes group by ssn having avg( grade) >= all ( select avg( grade ) from student group by ssn ) } all GPAs

82 CMU SCS FaloutsosCMU SCS 15-415/61582 DML - nested subqueries ‘in’ and ‘>= all’ compares a value with a set of values other operators like these?

83 CMU SCS FaloutsosCMU SCS 15-415/61583 DML - nested subqueries all()... ‘<>all’ is identical to ‘not in’ >some(), >= some ()... ‘= some()’ is identical to ‘in’ exists

84 CMU SCS FaloutsosCMU SCS 15-415/61584 DML - nested subqueries Drill for ‘exists’: find all courses that nobody enrolled in select c-id from class …. with no tuples in ‘takes’

85 CMU SCS FaloutsosCMU SCS 15-415/61585 DML - nested subqueries Drill for ‘exists’: find all courses that nobody enrolled in select c-id from class where not exists (select * from takes where class.c-id = takes.c-id)

86 CMU SCS FaloutsosCMU SCS 15-415/61586 DML - derived relations find the ssn with the highest GPA select ssn, avg(grade) from takes group by ssn having avg( grade) >= all ( select avg( grade ) from takes group by ssn )

87 CMU SCS FaloutsosCMU SCS 15-415/61587 DML - derived relations find the ssn with the highest GPA Query would be easier, if we had a table like: helpfulTable (ssn, gpa): then what?

88 CMU SCS FaloutsosCMU SCS 15-415/61588 DML - derived relations select ssn, gpa from helpfulTable where gpa in (select max(gpa) from helpfulTable)

89 CMU SCS FaloutsosCMU SCS 15-415/61589 DML - derived relations find the ssn with the highest GPA - Query for helpfulTable (ssn, gpa)?

90 CMU SCS FaloutsosCMU SCS 15-415/61590 DML - derived relations find the ssn with the highest GPA Query for helpfulTable (ssn, gpa)? select ssn, avg(grade) from takes group by ssn

91 CMU SCS FaloutsosCMU SCS 15-415/61591 DML - derived relations find the ssn with the highest GPA helpfulTable(ssn,gpa) select ssn, avg(grade) from takes group by ssn select ssn, gpa from helpfulTable where gpa = (select max(gpa) from helpfulTable)

92 CMU SCS FaloutsosCMU SCS 15-415/61592 DML - derived relations find the ssn with the highest GPA select ssn, gpa from (select ssn, avg(grade) from takes group by ssn) as helpfulTable(ssn, gpa) where gpa in (select max(gpa) from helpfulTable)

93 CMU SCS FaloutsosCMU SCS 15-415/61593 Overview - detailed - SQL DML –select, from, where, renaming –set operations –ordering –aggregate functions –nested subqueries other parts: DDL, embedded SQL, auth etc ✔ ✔ ✔ ✔ ✔

94 CMU SCS FaloutsosCMU SCS 15-415/61594 Next lecture: DML –select, from, where, renaming –set operations –ordering –aggregate functions –nested subqueries other parts: DDL, embedded SQL, auth etc ✔ ✔ ✔ ✔ ✔


Download ppt "CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415/615 - DB Applications C. Faloutsos & A. Pavlo Lecture#6: Rel. model - SQL part1 (R&G, chapter."

Similar presentations


Ads by Google