Presentation is loading. Please wait.

Presentation is loading. Please wait.

Relational Algebra and SQL Prof. Sin-Min Lee Department of Computer Science.

Similar presentations


Presentation on theme: "Relational Algebra and SQL Prof. Sin-Min Lee Department of Computer Science."— Presentation transcript:

1 Relational Algebra and SQL Prof. Sin-Min Lee Department of Computer Science

2 The Role of Relational Algebra in a DBMS

3 Relational Algebra Operations

4 More Relational Algebra

5

6 Select Extracts specified tuples (rows) from a specified relation (table).

7 Select Operator Produce table containing subset of rows of argument table satisfying condition  condition (relation) Example: Person Person Person  Hobby=‘stamps’ ( Person ) 1123 John 123 Main stamps 1123 John 123 Main coins 5556 Mary 7 Lake Dr hiking 9876 Bart 5 Pine St stamps 1123 John 123 Main stamps 9876 Bart 5 Pine St stamps Id Name Address Hobby

8 Selection Condition Operators:, =,  Simple selection condition: – operator AND OR NOT

9 Selection Condition - Examples Person  Id>3000 OR Hobby=‘hiking’ (Person) Person  Id>3000 AND Id <3999 (Person) Person  NOT (Hobby=‘hiking’) (Person) Person  Hobby  ‘hiking’ (Person)

10 Project Extracts specified attributes(columns) from a specified relation.

11 Project Operator Produces table containing subset of columns of argument table  attribute list (relation) Example: PersonPerson Person  Name,Hobby (Person) 1123 John 123 Main stamps 1123 John 123 Main coins 5556 Mary 7 Lake Dr hiking 9876 Bart 5 Pine St stamps John stamps John coins Mary hiking Bart stamps Id Name Address Hobby Name Hobby

12 Project Operator 1123 John 123 Main stamps 1123 John 123 Main coins 5556 Mary 7 Lake Dr hiking 9876 Bart 5 Pine St stamps John 123 Main Mary 7 Lake Dr Bart 5 Pine St Result is a table (no duplicates); can have fewer tuples than the original Id Name Address Hobby Name Address Example: PersonPerson Person  Name,Address (Person)

13 Expressions 1123 John 123 Main stamps 1123 John 123 Main coins 5556 Mary 7 Lake Dr hiking 9876 Bart 5 Pine St stamps 1123 John 9876 Bart Id Name Address Hobby Id Name Person Result Person  Id, Name (  Hobby=’stamps’ OR Hobby=’coins’ (Person) )

14 Set Operators Relation is a set of tuples, so set operations should apply: , ,  (set difference) Result of combining two relations with a set operator is a relation => all its elements must be tuples having same structure union compatible relationsHence, scope of set operations limited to union compatible relations

15 Union Compatible Relations union compatibleTwo relations are union compatible if –Both have same number of columns –Names of attributes are the same in both –Attributes with the same name in both relations have the same domain unionintersectionset differenceUnion compatible relations can be combined using union, intersection, and set difference

16

17 Example Tables: Person Person (SSN, Name, Address, Hobby) Professor Professor (Id, Name, Office, Phone) are not union compatible. But PersonProfessor  Name (Person) and  Name (Professor) are union compatible so PersonProfessor  Name (Person) -  Name (Professor) makes sense.

18 Cartesian Product RSRS R SIf R and S are two relations, R  S is the set of all concatenated tuples, where x is a tuple in R and y is a tuple in S –RS –R and S need not be union compatible RSR  S is expensive to compute: –Factor of two in the size of each row –Quadratic in the number of rows A B C D A B C D x1 x2 y1 y2 x1 x2 y1 y2 x3 x4 y3 y4 x1 x2 y3 y4 x3 x4 y1 y2 RS R S x3 x4 y3 y4 RS R  S

19 Renaming Result of expression evaluation is a relation Attributes of relation must have distinct names. This is not guaranteed with Cartesian product –e.g., suppose in previous example a and c have the same name Renaming operator tidies this up. To assign the names A 1, A 2,… A n to the attributes of the n column relation produced by expression expr use expr [A 1, A 2, … A n ]

20 Example This is a relation with 4 attributes: StudId, CrsCode1, ProfId, CrsCode2 Transcript Transcript (StudId, CrsCode, Semester, Grade) Teaching Teaching (ProfId, CrsCode, Semester) Transcript  StudId, CrsCode (Transcript)[StudId, CrsCode1] Teaching   ProfId, CrsCode (Teaching) [ProfId, CrsCode2]

21 Join Builds a relation from two specified relations consisting of all possible concatenated pairs, one from each of the two relations, such that in each pair the two tuples satisfy some condition. (E.g., equal values in a given col.) A1 B1 A2 B1 A3 B2 B1 C1 B2 C2 B3 C3 A1 B1 C1 A2 B1 C1 A3 B2 C2 (Natural or Inner) Join

22 Outer Join Outer Joins are similar to PRODUCT -- but will leave NULLs for any row in the first table with no corresponding rows in the second. A1 B1 A2 B1 A3 B2 A4 B7 B1 C1 B2 C2 B3 C3 A1 B1 C1 A2 B1 C1 A3 B2 C2 A4 * * Outer Join

23 Join Items

24 Derived Operation: Join generalthetajoin A (general or theta) join of R and S is the expression R join-condition S where join-condition is a conjunction of terms: A i oper B i in which A i is an attribute of R; B i is an attribute of S; and oper is one of =,,  , . The meaning is:  join-condition ´ (R  S) where join-condition and join-condition ´ are the same, except for possible renamings of attributes (next)

25 Join Join is a derivative of Cartesian product. Equivalent to performing a Selection, using join predicate as selection formula, over Cartesian product of the two operand relations. One of the most difficult operations to implement efficiently in an RDBMS and one reason why RDBMSs have intrinsic performance problems. Various forms of join operation –Natural join (defined by Codd) –Outer join –Theta join –Equijoin (a particular type of Theta join) –Semijoin

26 Natural Join List the names and comments of all clients who have viewed a property for rent. (  clientNo, fName, lName (Client)) Join (  clientNo, propertyNo, comment (Viewing)) Or Client [ clientNo, fName, lName ] Join Viewing [ clientNo, propertyNo, comment ]

27 Outer Join To display rows in the result that do not have matching values in the join column, use Outer join. R Left Outer Join S –(Left) outer join is join in which tuples from R that do not have matching values in common columns of S are also included in result relation. Example: Produce a status report on property viewings.  propertyNo, street, city (PropertyForRent) Left Outer Join Viewing

28 Join and Renaming Problem: R and S might have attributes with the same name – in which case the Cartesian product is not defined Solutions: 1.Rename attributes prior to forming the product and use new names in join-condition´. Transcript.CrsCodeTeaching.CrsCode 2.Qualify common attribute names with relation names (thereby disambiguating the names). For instance: Transcript.CrsCode or Teaching.CrsCode –This solution is nice, but doesn’t always work: consider RR R join_condition R R In R.A, how do we know which R is meant?

29 Theta Join – Example Employee(Name,Id,MngrId,Salary Employee(Name,Id,MngrId,Salary) Manager(Name,Id,Salary Manager(Name,Id,Salary) Output the names of all employees that earn more than their managers. Employee EmployeeManager  Employee.Name ( Employee MngrId=Id AND Salary>Salary Manager ) The join yields a table with attributes: EmployeeEmployeeEmployee Employee.Name, Employee.Id, Employee.Salary, MngrId ManagerManagerManager Manager.Name, Manager.Id, Manager.Salary

30 Equijoin Join - Example Student  Name,CrsCode ( Student Transcript Id=StudId  Grade=‘A’ (Transcript)) Id Name Addr Status 111 John ….. ….. 222 Mary ….. ….. 333 Bill ….. ….. 444 Joe ….. ….. StudId CrsCode Sem Grade 111 CSE305 S00 B 222 CSE306 S99 A 333 CSE304 F99 A Mary CSE306 Bill CSE304 The equijoin is used very frequently since it combines related data in different relations. Student Transcript Equijoin Equijoin: Join condition is a conjunction of equalities.

31 Natural Join Special case of equijoin: –join condition equates all and only those attributes with the same name (condition doesn’t have to be explicitly stated) –duplicate columns eliminated from the result Transcript Transcript (StudId, CrsCode, Sem, Grade) Teaching ( Teaching (ProfId, CrsCode, Sem) Transcript Teaching Teaching =  StudId, Transcript.CrsCode, Transcript.Sem, Grade, ProfId Transcript ( Transcript Sem Teaching CrsCode=CrsCode AND Sem=Sem Teaching ) [ StudId, CrsCode, Sem, Grade, ProfId ]

32 Natural Join (cont’d) More generally: R SRS S =  attr-list (  join-cond (R × S) ) where RS attr-list = attributes (R)  attributes (S) (duplicates are eliminated) and join-cond has the form: A 1 = A 1 AND … AND A n = A n where RS {A 1 … A n } = attributes(R)  attributes(S)

33 Natural Join Example List all Ids of students who took at least two different courses:  StudId (  CrsCode  CrsCode2 ( Transcript Transcript Transcript [ StudId, CrsCode2, Sem2, Grade2 ] )) We don’t want to join on CrsCode, Sem, and Grade attributes, hence renaming!

34

35 Aggregates Functions that operate on sets: –COUNT, SUM, AVG, MAX, MIN Produce numbers (not tables) Not part of relational algebra SELECT COUNT(*) Professor FROM Professor P SELECT MAX (Salary) Employee FROM Employee E

36 Aggregates SELECT COUNT ( T.CrsCode ) Teaching FROM Teaching T WHERE T.Semester = ‘S2000’ SELECT COUNT (DISTINCT T.CrsCode ) Teaching FROM Teaching T WHERE T.Semester = ‘S2000’ Count the number of courses taught in S2000 But if multiple sections of same course are taught, use:

37 Aggregates: Proper and Improper Usage SELECT COUNT (T.CrsCode), T. ProfId – makes no sense (in the absence of GROUP BY clause) SELECT COUNT (*), AVG (T.Grade) – but this is OK WHERE T.Grade > COUNT (SELECT ….) – aggregate cannot be applied to result of SELECT statement

38 Grouping But how do we compute the number of courses taught in S2000 per professor? –Strategy 1: Fire off a separate query for each professor: SELECT COUNT( T.CrsCode ) Teaching FROM Teaching T WHERE T.Semester = ‘S2000’ AND T.ProfId = 123456789 Cumbersome What if the number of professors changes? Add another query? grouping operator –Strategy 2: define a special grouping operator: SELECT T.ProfId, COUNT( T.CrsCode ) Teaching FROM Teaching T WHERE T.Semester = ‘S2000’ GROUP BY T.ProfId

39 GROUP BY

40 GROUP BY - Example SELECT T.StudId, AVG( T.Grade ), COUNT (*) Transcript FROM Transcript T GROUP BY T.StudId Transcript Attributes: -student’s Id -avg grade -number of courses 1234 3.3 4 1234

41 HAVING Clause Eliminates unwanted groups (analogous to WHERE clause ) HAVING condition constructed from attributes of GROUP BY list and aggregates of attributes not in list SELECT T.StudId, AVG (T.Grade) AS CumGpa, COUNT (*) AS NumCrs Transcript FROM Transcript T WHERE T.CrsCode LIKE ‘CS%’ GROUP BY T.StudId HAVING AVG (T.Grade) > 3.5

42 Evaluation of GroupBy with Having

43 Example Output the name and address of all seniors on the Dean’s List SELECT S.Id, S.Name StudentTranscript FROM Student S, Transcript T WHERE S.Id = T.StudId AND S.Status = ‘senior’ GROUP BY HAVING AVG (T.Grade) > 3.5 AND SUM (T.Credit) > 90 S.Id -- wrong S.Id, S.Name -- right Every attribute that occurs in SELECT clause must also occur in GROUP BY or it must be an aggregate. S.Name does not.

44 ORDER BY Clause Causes rows to be output in a specified order SELECT T.StudId, COUNT (*) AS NumCrs, AVG (T.Grade) AS CumGpa Transcript FROM Transcript T WHERE T.CrsCode LIKE ‘CS%’ GROUP BY T.StudId HAVING AVG (T.Grade) > 3.5 ORDER BY DESC CumGpa, ASC StudId

45 Query Evaluation Strategy 1Evaluate FROM : produces Cartesian product, A, of tables in FROM list 2Evaluate WHERE : produces table, B, consisting of rows of A that satisfy WHERE condition 3Evaluate GROUP BY : partitions B into groups that agree on attribute values in GROUP BY list 4Evaluate HAVING : eliminates groups in B that do not satisfy HAVING condition 5Evaluate SELECT : produces table C containing a row for each group. Attributes in SELECT list limited to those in GROUP BY list and aggregates over group 6Evaluate ORDER BY : orders rows of C

46 Nested Queries List all courses that were not taught in S2000 SELECT C.CrsName FROM Course C WHERE C.CrsCode NOT IN (SELECT T.CrsCode --subquery FROM Teaching T WHERE T.Sem = ‘S2000’) Evaluation strategy: subquery evaluated once to produces set of courses taught in S2000. Each row (as C) tested against this set.

47 Correlated Nested Queries Output a row if prof has taught a course in dept. SELECT T.ProfId --subquery FROM Teaching T, Course C WHERE T.CrsCode=C.CrsCode AND C.DeptId=D.DeptId --correlation SELECT P.Name, D.Name --outer query FROM Professor P, Department D WHERE P.Id IN (set of Id’s of all profs who have taught a course in D.DeptId)

48 Correlated Nested Queries (con’t) Tuple variables T and C are local to subquery Tuple variables P and D are global to subquery Correlation: subquery uses a global variable, D The value of D.DeptId parameterizes an evaluation of the subquery Subquery must (at least) be re-evaluated for each distinct value of D.DeptId Correlated queries can be expensive to evaluate

49 Division Goal: Produce the tuples in one relation, r, that match all tuples in another relation, s –r –r (A 1, …A n, B 1, …B m ) –s –s (B 1 …B m ) –rs s r –r/s, with attributes A 1, …A n, is the set of all tuples such that for every tuple in s, is in r Can be expressed in terms of projection, set difference, and cross-product

50 Division (con’t)

51 Division Identify all clients who have viewed all properties with three rooms. (  clientNo, propertyNo (Viewing))  (  propertyNo (  rooms = 3 (PropertyForRent)))

52 Division - Example List the Ids of students who have passed all courses that were taught in spring 2000 Numerator: –StudId and CrsCode for every course passed by every student: Transcript  StudId, CrsCode (  Grade  ‘F’ (Transcript) ) Denominator: – CrsCode of all courses taught in spring 2000 Teaching  CrsCode (  Semester=‘S2000’ (Teaching) ) Result is numerator/denominator

53 Division Query type: Find the subset of items in one set that are related to all items in another set Example: Find professors who have taught courses in all departments –Why does this involve division? ProfId DeptIdDeptId All department Ids Contains row if professor p has taught a course in department d  ProfId,DeptId (Professor) /  DeptId (Department)

54 Division Strategy for implementing division in SQL: –Find set, A, of all departments in which a particular professor, p, has taught a course –Find set, B, of all departments –Output p if A  B, or, equivalently, if B–A is empty

55 Division – SQL Solution SELECT P.Id Professor FROM Professor P WHERE NOT EXISTS (SELECT D.DeptId -- set B of all dept Ids Department FROM Department D EXCEPT SELECT C.DeptId -- set A of dept Ids of depts in -- which P has taught a course TeachingCourse FROM Teaching T, Course C WHERE T.ProfId=P.Id -- global variable AND T.CrsCode=C.CrsCode)

56 Division Query type: Find the subset of items in one set that are related to all items in another set Example: Find professors who have taught courses in all departments –Why does this involve division? ProfId DeptIdDeptId All department Ids Contains row if professor p has taught a course in department d

57 Division Strategy for implementing division in SQL: –Find set of all departments in which a particular professor, p, has taught a course - A –Find set of all departments - B –Output p if A  B, or equivalently if B-A is empty

58 Division – SQL Solution SELECT P.Id FROM Professor P WHERE NOT EXISTS (SELECT D.DeptId -- B: set of all dept Ids FROM Department D EXCEPT SELECT C.DeptId -- A: set of dept Ids of depts in -- which P has taught a course FROM Teaching T, Course C WHERE T.ProfId=P.Id --global variable AND T.CrsCode=C.CrsCode)

59 GROUP BY Table output by WHERE clause: - Divide rows into groups based on subset of attributes; - All members of a group agree on those attributes Each group can be described by a single row in a table with attributes limited to: -Attributes all group members share (listed in GROUP BY clause) -Aggregates over group group GROUP BY attributes aabbcccccdddaabbcccccddd

60 GROUP BY - Example SELECT T.StudId, AVG(T.Grade), COUNT (*) FROM Transcript T GROUP BY T.StudId Transcript Attributes: -student’s Id -avg grade -number of courses 1234 3.3 4 1234

61 HAVING Clause Eliminates unwanted groups (analogous to WHERE clause ) HAVING condition constructed from attributes of GROUP BY list and aggregates of attributes not in list SELECT T.StudId, AVG( T.Grade ) AS CumGpa, COUNT (*) AS NumCrs FROM Transcript T WHERE T.CrsCode LIKE ‘CS%’ GROUP BY T.StudId HAVING AVG (T.Grade) > 3.5

62 Example Output the name and address of all seniors on the Dean’s List SELECT S.Name, S.Address FROM Student S, Transcript T WHERE S.StudId = T.StudId AND S.Status = ‘senior’ GROUP BY HAVING AVG (T.Grade) > 3.5 AND SUM (T.Credit) > 90 S.StudId -- wrong S.Name, S.Address -- right

63 SQL and Relational Algebra RELATIONAL ALGEBRASQL PSEUDO-CODE 1) R = customers where city = 'Dallas' SELECT * from customers where city = 'Dallas'; For c1 = first row of customers until c1 = last row of customers If c1.city = ‘Dallas’ Display c1.* End-If End-For 2) R = customers [cid, cname] SELECT cid, cname from customers; For c1 = first row of customers until c1 = last row of customers Display c1.cid, c1.cname End-For

64 Constructing SQL

65

66

67 http://www.cs.ru.nl/~gerp/IS0/sheets/IS0_Relatio nele_Algebra_SQL2.pdf


Download ppt "Relational Algebra and SQL Prof. Sin-Min Lee Department of Computer Science."

Similar presentations


Ads by Google