Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 480: Database Systems Lecture 12 February 11, 2013.

Similar presentations


Presentation on theme: "CS 480: Database Systems Lecture 12 February 11, 2013."— Presentation transcript:

1 CS 480: Database Systems Lecture 12 February 11, 2013

2 SQL Basic Query Format SELECT A1,A2,…,An FROM r1,r2,…,rm WHERE P
Suppose the ri’s have scheme ri(Ri) where Ri is a set of attributes. Then the Ai’s are attributes in R1  …  Rm. P is a boolean predicate in which an atom is a selection atom on r1  r2  …  rm or other types of SQL boolean predicates: string predicates (LIKE,CONTAINS) t IN ri t θ ALL(ri), t θ SOME(ri) others MAKE NOTE: SQL is case insensitive

3 SQL Basic Query Format SELECT A1,A2,…,An FROM r1,r2,…,rm WHERE P
Queries are written in SELECT, FROM, WHERE order It’s important to understand the operational order: FROM: Cartesian product of the given relations WHERE: Selection based on the given predicate SELECT: Projection of the given attributes.

4 SQL Basic Query Format SELECT A1,A2,…,An FROM r1,r2,…,rm WHERE P
In relational algebra:

5 Completeness of SQL Projection (Π) SELECT A1,A2,…,An FROM r

6 Completeness of SQL Selection (σ) σP(r)

7 Completeness of SQL Selection (σ) σP(r) SELECT * FROM r WHERE P

8 σP(r) Completeness of SQL SELECT * FROM r WHERE P Selection (σ)
* denotes “all attributes”

9 Completeness of SQL Union () r  s

10 Completeness of SQL Union () r  s (SELECT * FROM r) UNION FROM s)

11 Completeness of SQL Difference (–) r – s

12 r – s Completeness of SQL (SELECT * FROM r) MINUS FROM s)
Difference (–) r – s (SELECT * FROM r) MINUS FROM s)

13 r – s Completeness of SQL (SELECT * FROM r) EXCEPT FROM s)
Difference (–) r – s (SELECT * FROM r) EXCEPT FROM s) Book uses except. Both are accepted.

14 Completeness of SQL Cartesian Product () r  s

15 Completeness of SQL Cartesian Product () r  s SELECT * FROM r,s

16 ΠR(r  s) Completeness of SQL SELECT r.* FROM r,s
Cartesian Product () ΠR(r  s) SELECT r.* FROM r,s

17 Completeness of SQL Rename (ρ) ρd(r)

18 Completeness of SQL Rename (ρ) ρd(r) SELECT * FROM r AS d

19 Set Membership () SELECT student_id FROM enrol
Retrieve the student_id’s of students that took both CS480 and CS580 and got an A in both. ENROL(student_id,course,grade) SELECT student_id FROM enrol WHERE course=‘CS480’ AND grade=‘A’ AND student_id IN (SELECT student_id FROM enrol WHERE course=‘CS580’ AND grade=‘A’) WHAT WOULD BE AN ALTERNATIVE TO THIS? Cartesian product of enrol with itself with renaming. OR use the Intersection operator.

20 Set Membership () Retrieve the student_id’s of students that took CS480 but have not taken CS580. ENROL(student_id,course,grade) SELECT student_id FROM enrol WHERE course=‘CS480’ AND student_id NOT IN (SELECT student_id FROM enrol WHERE course=‘CS580’) An alternative to this one would be to do it with the EXCEPT one… ADD NOTE THAT YOU COULD HAVE MORE THAN ONE ATTRIBUTE before the NOT IN… or before the IN for that matter.

21 Set Comparison Retrieve the names of instructors that have a salary higher than at least one instructor in the Biology department. INSTRUCTOR(name,dept,salary) SELECT name FROM instructor as i WHERE i.salary > SOME(SELECT salary FROM instructor as j WHERE j.dept=‘Biology’)

22 Set Containment The CONTAINS clause is a mechanism by which SQL implements the division operator. ENROL(id,course,grade) STUDENT(id,name,major) Names and majors of students that took all the courses that John Doe took. SELECT name,major FROM student as d WHERE (SELECT course FROM enrol enrol.id = d.id) CONTAINS (SELECT course FROM enrol as e,student as s WHERE e.id=s.id AND s.name=‘John Doe’)

23 Set Containment The CONTAINS clause is a mechanism by which SQL implements the division operator. ENROL(id,course,grade) STUDENT(id,name,major) Names and majors of students that took all the courses that John Doe took. SELECT name,major FROM student as d WHERE (SELECT course FROM enrol enrol.id = d.id) CONTAINS (SELECT course FROM enrol as e,student as s WHERE e.id=s.id AND s.name=‘John Doe’)

24 Set Containment The CONTAINS clause is a mechanism by which SQL implements the division operator. ENROL(id,course,grade) STUDENT(id,name,major) Names and majors of students that took all the courses that John Doe took. SELECT name,major FROM student as d WHERE (SELECT course FROM enrol enrol.id = d.id) CONTAINS (SELECT course FROM enrol as e,student as s WHERE e.id=s.id AND s.name=‘John Doe’)

25 Set Containment The CONTAINS clause is a mechanism by which SQL implements the division operator. ENROL(id,course,grade) STUDENT(id,name,major) Names and majors of students that took all the courses that John Doe took. SELECT name,major FROM student as d WHERE (SELECT course FROM enrol enrol.id = d.id) CONTAINS (SELECT course FROM enrol as e,student as s WHERE e.id=s.id AND s.name=‘John Doe’)

26 Set Containment The CONTAINS clause is a mechanism by which SQL implements the division operator. ENROL(id,course,grade) STUDENT(id,name,major) Names and majors of students that took all the courses that John Doe took. SELECT name,major FROM student as d WHERE (SELECT course FROM enrol enrol.id = d.id) CONTAINS (SELECT course FROM enrol as e,student as s WHERE e.id=s.id AND s.name=‘John Doe’)

27 Set Containment The CONTAINS clause is a mechanism by which SQL implements the division operator. ENROL(id,course,grade) STUDENT(id,name,major) Names and majors of students that took all the courses that John Doe took. Πname,major(student (Πid,course(enrol)  Πcourse(σname=‘John Doe’(student enrol))) SELECT name,major FROM student as d WHERE (SELECT course FROM enrol enrol.id = d.id) CONTAINS (SELECT course FROM enrol as e,student as s WHERE e.id=s.id AND s.name=‘John Doe’)

28 Set Containment Names and majors of students that only took all the courses that John Doe took. SELECT name,major FROM student as d WHERE (SELECT course FROM enrol enrol.id = d.id) CONTAINS (SELECT course FROM enrol as e,student as s WHERE e.id=s.id AND s.name=‘John Doe’) AND

29 Set Containment Names and majors of students that only took all the courses that John Doe took. SELECT name,major FROM student as d WHERE (SELECT course FROM enrol enrol.id = d.id) CONTAINS (SELECT course FROM enrol as e,student as s WHERE e.id=s.id AND s.name=‘John Doe’) AND

30 Set Containment Names and majors of students that only took all the courses that John Doe took. SELECT name,major FROM student as d WHERE (SELECT course FROM enrol enrol.id = d.id) CONTAINS (SELECT course FROM enrol as e,student as s WHERE e.id=s.id AND s.name=‘John Doe’) AND

31 Set Containment SELECT name,major FROM student as d
CONTAINS no longer part of the current SQL standards. Now, “a contains b” can be restated as “NOT EXISTS (b EXCEPT a) SELECT name,major FROM student as d WHERE (SELECT course FROM enrol enrol.id = d.id) CONTAINS (SELECT course FROM enrol as e,student as s WHERE e.id=s.id AND s.name=‘John Doe’)

32 Set Cardinality SELECT name,major FROM student as d WHERE NOT EXISTS
CONTAINS no longer part of the current SQL standards. Now, “a contains b” can be restated as “NOT EXISTS (b EXCEPT a) SELECT name,major FROM student as d WHERE NOT EXISTS ((SELECT course FROM enrol as e,student as s WHERE e.id=s.id AND s.name=‘John Doe’) EXCEPT (SELECT course FROM enrol enrol.id = d.id))

33 Set Cardinality SELECT name,major FROM student as d WHERE NOT EXISTS
CONTAINS no longer part of the current SQL standards. Now, “a contains b” can be restated as “NOT EXISTS (b EXCEPT a) SELECT name,major FROM student as d WHERE NOT EXISTS ((SELECT course FROM enrol as e,student as s WHERE e.id=s.id AND s.name=‘John Doe’) EXCEPT (SELECT course FROM enrol enrol.id = d.id)) THIS EXAMPLE ILLUSTRATES AN ARBITRARY LEVEL OF NESTING. (Subqueries inside of subqueries). NOT EXISTS – Tests if a relation is empty EXISTS – Tests if a relation is nonempty

34 Set Cardinality ENROL(id,course,grade) STUDENT(id,name,major)
Retrieve the id’s of students that didn’t take any course that John Doe took. SELECT d.id FROM student as d WHERE NOT EXISTS ((SELECT course FROM enrol enrol.id = d.id) INTERSECT (SELECT course FROM enrol as e,student as s WHERE e.id=s.id AND s.name=‘John Doe’))

35 Set Cardinality ENROL(id,course,grade) STUDENT(id,name,major)
Retrieve the id’s of students that didn’t take any course that John Doe took. SELECT d.id FROM student as d WHERE NOT EXISTS ((SELECT course FROM enrol enrol.id = d.id) INTERSECT (SELECT course FROM enrol as e,student as s WHERE e.id=s.id AND s.name=‘John Doe’))

36 Set Cardinality ENROL(id,course,grade) STUDENT(id,name,major)
Retrieve the id’s of students that didn’t take any course that John Doe took. SELECT d.id FROM student as d WHERE NOT EXISTS ((SELECT course FROM enrol enrol.id = d.id) INTERSECT (SELECT course FROM enrol as e,student as s WHERE e.id=s.id AND s.name=‘John Doe’))

37 Set Cardinality ENROL(id,course,grade) STUDENT(id,name,major)
Retrieve the id’s of students that didn’t take any course that John Doe took. SELECT d.id FROM student as d WHERE NOT EXISTS ((SELECT course FROM enrol enrol.id = d.id) INTERSECT (SELECT course FROM enrol as e,student as s WHERE e.id=s.id AND s.name=‘John Doe’)) Arbitrary Level of Nesting

38 Aggregate Operations Operations that take a collection (set or multiset) as input and return a single value. Average: AVG Minimum: MIN Maximum: MAX Total: SUM Count: COUNT Sum and avg must be a collection of numbers.

39 Aggregate Operations INSTRUCTOR(name,dept,salary) TEACHES(name,course,semester,year) Ex: Retrieve the average salary of professors that have taught ‘CS480’. SELECT AVG(salary) FROM instructor as i,teaches as t WHERE i.name=t.name AND i.name IN (SELECT name FROM teaches WHERE course=‘CS480’) Result is a relation with a single attribute and a single tuple. Discuss why I did it with the IN command. This is because otherwise we would get the same instructor counted more than once. DISTINCT will not work because that will just eliminate salaries that are the same but there could be two professors with the same salary (therefore, we don’t want to do that).

40 Aggregate Operations INSTRUCTOR(name,dept,salary) TEACHES(name,course,semester,year) Ex: Retrieve the average salary of professors that have taught ‘CS480’. SELECT AVG(salary) FROM instructor as i,teaches as t WHERE i.name=t.name AND i.name IN (SELECT name FROM teaches WHERE course=‘CS480’) Result is a relation with a single attribute and a single tuple. Discuss why I did it with the IN command. This is because otherwise we would get the same instructor counted more than once. DISTINCT will not work because that will just eliminate salaries that are the same but there could be two professors with the same salary (therefore, we don’t want to do that).

41 Aggregate Operations INSTRUCTOR(name,dept,salary) TEACHES(name,course,semester,year) Ex: Retrieve the total number of professors that have taught CS480. SELECT COUNT(name) FROM instructor as i,teaches as t WHERE i.name=t.name AND t.course=‘CS480’

42 Aggregate Operations INSTRUCTOR(name,dept,salary) TEACHES(name,course,semester,year) Ex: Retrieve the total number of professors that have taught CS480. SELECT COUNT(name) FROM instructor as i,teaches as t WHERE i.name=t.name AND t.course=‘CS480’

43 Aggregate Operations INSTRUCTOR(name,dept,salary) TEACHES(name,course,semester,year) Ex: Retrieve the total number of professors that have taught CS480. SELECT COUNT(name) FROM instructor as i,teaches as t WHERE i.name=t.name AND t.course=‘CS480’ This will count instructors that have taught CS480 multiple times more than once.

44 Aggregate Operations INSTRUCTOR(name,dept,salary) TEACHES(name,course,semester,year) Ex: Retrieve the total number of professors that have taught CS480. SELECT COUNT(DISTINCT name) FROM instructor as i,teaches as t WHERE i.name=t.name AND t.course=‘CS480’ Now, any professor that has taught the course will be counted only once.

45 Aggregation with GROUP BY
Sometimes we want to apply aggregate functions to a single set of tuples (relation), but also to a group of sets of tuples (subsets of the relation). To do that we use the GROUP BY clause. The attributes given in the GROUP BY clause are used to form groups. Then the aggregation is performed for each group.

46 Aggregation with GROUP BY
INSTRUCTOR(name,dept,salary) Retrieve the faculty budget for each department (what they pay in total to their professors). SELECT SUM(salary) FROM instructor GROUP BY dept

47 Aggregation with GROUP BY
INSTRUCTOR(name,dept,salary) Retrieve the faculty budget for each department (what they pay in total to their professors). SELECT SUM(salary) FROM instructor GROUP BY dept

48 Aggregation with GROUP BY
INSTRUCTOR(name,dept,salary) Retrieve the faculty budget for each department (what they pay in total to their professors). SELECT dept,SUM(salary) FROM instructor GROUP BY dept

49 Aggregation with GROUP BY
INSTRUCTOR(name,dept,salary) Retrieve the faculty budget for each department (what they pay in total to their professors). SELECT dept,SUM(salary) FROM instructor GROUP BY dept This will return a relation with 2 columns, one for dept and the other for the sum of all the professor salaries.

50 Aggregation with GROUP BY
INSTRUCTOR(name,dept,salary) Retrieve the faculty budget for each department (what they pay in total to their professors). SELECT dept,SUM(salary) AS budget FROM instructor GROUP BY dept This will return a relation with 2 columns, one for dept and the other for budget.

51 Aggregation with GROUP BY
INSTRUCTOR(name,dept,salary) Retrieve the number of professors that earn more than $100,000 in each department SELECT COUNT(name) FROM instructor WHERE salary>100000 GROUP BY dept

52 Aggregation with GROUP BY
INSTRUCTOR(name,dept,salary) Retrieve the number of professors that earn more than $100,000 in each department SELECT dept,COUNT(name) FROM instructor WHERE salary>100000 GROUP BY dept

53 Aggregation with GROUP BY and HAVING
It may be useful to state a condition that applies to the groups rather than the tuples. The HAVING clause applies conditions to the groups formed by the GROUP BY clause. Like a WHERE clause but for the groups.

54 Aggregation with GROUP BY and HAVING
INSTRUCTOR(name,dept,salary) Query: Retrieve the faculty budget of each department that has 25 or more professors. SELECT SUM(salary) AS budget FROM instructor GROUP BY dept HAVING COUNT(name)>=25 If a department has less than 25 professors, it won’t appear.

55 Aggregation with GROUP BY and HAVING
INSTRUCTOR(name,dept,salary) Query: Retrieve the faculty budget of each department that has 25 or more professors. SELECT dept, SUM(salary) AS budget FROM instructor GROUP BY dept HAVING COUNT(name)>=25 If a department has less than 25 professors, it won’t appear.

56 Aggregation with GROUP BY and HAVING
INSTRUCTOR(name,dept,salary) Query: Retrieve the faculty budget of each department that has 25 or more professors. SELECT dept,SUM(salary) AS budget FROM instructor GROUP BY dept HAVING COUNT(name)>=25 If a department has less than 25 professors, it won’t appear.

57 Aggregation with GROUP BY and HAVING
STUDENT(id,name,address,GPA) ENROL(id,course,section,semester,year) Query: For each course section offered in 2012, find the average GPA of all the students enrolled in the section, if the section had at least 10 students. SELECT course,semester,year,section,AVG(GPA) FROM enrol NATURAL JOIN student WHERE year=2012 GROUP BY course,semester,year,section HAVING COUNT(id)>=10

58 Aggregation with GROUP BY and HAVING
STUDENT(id,name,address,GPA) ENROL(id,course,section,semester,year) Query: For each course section offered in 2012, find the average GPA of all the students enrolled in the section, if the section had at least 10 students. SELECT course,semester,year,section,AVG(GPA) FROM enrol NATURAL JOIN student WHERE year=2012 GROUP BY course,semester,year,section HAVING COUNT(id)>=10

59 Aggregation with GROUP BY and HAVING
STUDENT(id,name,address,GPA) ENROL(id,course,section,semester,year) Query: For each course section offered in 2012, find the average GPA of all the students enrolled in the section, if the section had at least 10 students. SELECT course,semester,year,section,AVG(GPA) FROM enrol NATURAL JOIN student WHERE year=2012 GROUP BY course,semester,year,section HAVING COUNT(id)>=10

60 Aggregation with GROUP BY and HAVING
STUDENT(id,name,address,GPA) ENROL(id,course,section,semester,year) Query: For each course section offered in 2012, find the average GPA of all the students enrolled in the section, if the section had at least 10 students. SELECT course,semester,year,section,AVG(GPA) FROM enrol NATURAL JOIN student WHERE year=2012 GROUP BY course,semester,year,section HAVING COUNT(id)>=10

61 Aggregation with GROUP BY and HAVING
STUDENT(id,name,address,GPA) ENROL(id,course,section,semester,year) Query: For each course section offered in 2012, find the average GPA of all the students enrolled in the section, if the section had at least 10 students. SELECT course,semester,year,section,AVG(GPA) FROM enrol NATURAL JOIN student WHERE year=2012 GROUP BY course,semester,year,section HAVING COUNT(id)>=10

62 Aggregation with GROUP BY and HAVING
STUDENT(id,name,address,GPA) ENROL(id,course,section,semester,year) Query: For each course section offered in 2012, find the average GPA of all the students enrolled in the section, if the section had at least 10 students. SELECT course,semester,year,section,AVG(GPA) FROM enrol NATURAL JOIN student WHERE year=2012 GROUP BY course,semester,year,section HAVING COUNT(id)>=10

63 Aggregation with GROUP BY and HAVING
STUDENT(id,name,address,GPA) ENROL(id,course,section,semester,year) Query: For each course section offered in 2012, find the average GPA of all the students enrolled in the section, if the section had at least 10 students. SELECT course,semester,year,section,AVG(GPA) FROM enrol NATURAL JOIN student WHERE year=2012 GROUP BY course,semester,year,section HAVING COUNT(id)>=10

64 Aggregation with GROUP BY and HAVING
STUDENT(id,name,address,GPA) ENROL(id,course,section,semester,year) Query: For each course section offered in 2012, find the average GPA of all the students enrolled in the section, if the section had at least 10 students. SELECT course,semester,year,section,AVG(GPA) FROM enrol NATURAL JOIN student WHERE year=2012 GROUP BY course,semester,year,section HAVING COUNT(id)>=10

65 Aggregation with GROUP BY and HAVING
STUDENT(id,name,address,GPA) ENROL(id,course,section,semester,year) Query: For each course section offered in 2012, find the average GPA of all the students enrolled in the section, if the section had at least 10 students. SELECT course,semester,year,section,AVG(GPA) FROM enrol NATURAL JOIN student WHERE year=2012 GROUP BY course,semester,year,section HAVING COUNT(id)>=10

66 Aggregation with GROUP BY and HAVING
STUDENT(id,name,address,GPA) ENROL(id,course,section,semester,year) Query: For each course section offered in 2012, find the average GPA of all the students enrolled in the section, if the section had at least 10 students. SELECT course,semester,year,section,AVG(GPA) FROM enrol NATURAL JOIN student WHERE year=2012 GROUP BY course,semester,year,section HAVING COUNT(id)>=10

67 Aggregation with GROUP BY and HAVING
STUDENT(id,name,address,GPA) ENROL(id,course,section,semester,year) Query: For each course section offered in 2012, find the average GPA of all the students enrolled in the section, if the section had at least 10 students. SELECT course,semester,year,section,AVG(GPA) FROM enrol NATURAL JOIN student WHERE year=2012 GROUP BY course,semester,year,section HAVING COUNT(id)>=10

68 Aggregation with GROUP BY and HAVING
STUDENT(id,name,address,GPA) ENROL(id,course,section,semester,year) Query: For each course section offered in 2012, find the average GPA of all the students enrolled in the section, if the section had at least 10 students. SELECT course,semester,year,section,AVG(GPA) FROM enrol NATURAL JOIN student WHERE year=2012 GROUP BY course,semester,year,section HAVING COUNT(id)>=10


Download ppt "CS 480: Database Systems Lecture 12 February 11, 2013."

Similar presentations


Ads by Google