Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to SQL, the Structured Query Language Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems September 16, 2003.

Similar presentations


Presentation on theme: "Introduction to SQL, the Structured Query Language Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems September 16, 2003."— Presentation transcript:

1 Introduction to SQL, the Structured Query Language Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems September 16, 2003 Some slide content courtesy of Susan Davidson & Raghu Ramakrishnan

2 2 Administrivia  Homework 1 due Thursday, on paper at start of class (electronic submission to Dinkar is also OK)  Homework 2 will be handed out Thursday  Will involve writing SQL  We should have Oracle available for you on eniac  Groups and project choices due by email by end of Friday – send to zives@cis, dinkar@gradient.cisdinkar@gradient.cis  Groups will be given supplemental reading to help them on their projects

3 3 Recall the Relational Algebra  Relational algebra operations operate on relations and produce relations (“closure”) f: Relation -> Relationf: Relation x Relation -> Relation  Six basic operations:  Projection   (R)  Selection   (R)  UnionR 1 [ R 2  DifferenceR 1 – R 2  ProductR 1 £ R 2  (Rename)   (R)  And some other useful ones:  JoinR 1 ⋈  R 2  SemijoinR 1 ⊲  R 2  IntersectionR 1 Å R 2  DivisionR 1 ¥ R 2

4 4 Recall the Domain Relational Calculus Queries of form: { | p} Predicate: boolean expression over x 1,x 2, …, x n  Expressions:  RX op Y X op constconst op X where op is , , , , ,  x i,x j,… are domain variables  Complex expressions: e 1  e 2, e 1  e 2,  e, and e 1  e 2  Universal and existential quantifiers domain variables predicate

5 5 Tuple Relational Calculus Queries of form: { | p} Predicate: boolean expression over T x attribs  Expressions: T x  RT X.a op T Y.b T X.a op constconst op T X.a where op is , , , , ,  T x,… are tuple variables, T x.a, … are attributes  Complex expressions: e 1  e 2, e 1  e 2,  e, and e 1  e 2  Universal and existential quantifiers domain variables predicate

6 6 From the Abstract to the Concrete Can’t do:  Aggregate operations  Recursive queries  Complex (non-tabular) structures  Today we’ll see SQL, which is relationally complete and can also do the first two things

7 7 Basic SQL: A Friendly Face Over the Tuple Relational Calculus SELECT [DISTINCT] {T 1.attrib, …, T 2.attrib} FROM {relation} T 1, {relation} T 2, … WHERE {predicates} Let’s do some examples, which will leverage your knowledge of the relational calculus…  Faculty ids  Course IDs for courses with students expecting a “C”  Courses taken by Jill select-list from-list qualification

8 8 Example Data Instance sidname 1Jill 2Qun 3Nitin 4Marty fidname 1Ives 2Saul 8Roth sidexp-gradecid 1A550-0103 1A700-1003 3A 3C500-0103 4C cidsubjsem 550-0103DBF03 700-1003AIS03 501-0103ArchF03 fidcid 1550-0103 2700-1003 8501-0103 STUDENT Takes COURSE PROFESSOR Teaches

9 9 Some Nice Features  SELECT *  All STUDENTs  AS  As a “range variable” (tuple variable): optional  As an attribute rename operator  Revisit last Thursday’s TRC query:  Which students (names) have taken more than one course from the same professor?  But rename the student name to “person”

10 10 Expressions in SQL  Can do computation over scalars (int, real or string) in the select-list or the qualification  Show all student IDs decremented by 1  Strings:  Fixed (CHAR(x)) or variable length (VARCHAR(x))  Use single quotes: ’A string’  Special comparison operator: LIKE  Not equal: <>  Typecasting:  CAST(S.sid AS VARCHAR(255))

11 11 Set Operations  Set operations default to set semantics, not bag semantics: (SELECT … FROM … WHERE …) {op} (SELECT … FROM … WHERE …)  Where op is one of:  UNION  INTERSECT, MINUS/EXCEPT (many DBs don’t support these last ones!)  Bag semantics: ALL

12 12 Exercise  Find all students who have taken DB but not AI  Hint: use EXCEPT

13 13 Nested Queries in SQL  Simplest: IN/NOT IN  Example: Students who have taken subjects that have (at any point) been taught by Roth

14 14 Correlated Subqueries  Most common: EXISTS/NOT EXISTS  Find all students who have taken DB but not AI

15 15 Universal and Existential Quantification  Generally used with subqueries:  {op} ANY, {op} ALL  Find the students with the best expected grades

16 16 Table Expressions  Can substitute a subquery for any relation in the FROM clause: SELECT S.sid FROM (SELECT sid FROM STUDENT WHERE sid = 5) S WHERE S.sid = 4 Notice that we can actually simplify this query! What is this equivalent to?

17 17 Aggregation  GROUP BY SELECT {group-attribs}, {aggregate-operator}(attrib) FROM {relation} T 1, {relation} T 2, … WHERE {predicates} GROUP BY {group-list}  Aggregate operators  AVG, COUNT, SUM, MAX, MIN  DISTINCT keyword for AVG, COUNT, SUM

18 18 Some Examples  Number of students in each course offering  Number of different grades expected for each course offering  Number of (distinct) students taking AI courses

19 19 What If You Want to Only Show Some Groups?  The HAVING clause lets you do a selection based on an aggregate (there must be 1 value per group): SELECT C.subj, COUNT(S.sid) FROM STUDENT S, Takes T, COURSE C WHERE S.sid = T.sid AND T.cid = C.cid GROUP BY subj HAVING COUNT(S.sid) > 5  Exercise: For each subject taught by at least two professors, list the minimum expected grade

20 20 Aggregation and Table Expressions  Sometimes need to compute results over the results of a previous aggregation: SELECT subj, AVG(size) FROM ( SELECT C.cid AS id, C.subj AS subj, COUNT(S.sid) AS size FROM STUDENT S, Takes T, COURSE C WHERE S.sid = T.sid AND T.cid = C.cid GROUP BY cid, subj) GROUP BY subj

21 21 Something to Ponder  Tables are great, but…  Not everyone is uniform – I may have a cell phone but not a fax  We may simply be missing certain information  We may be unsure about values  How do we handle these things?

22 22 Remember…  Homework 1 due Thursday  Groups and project choices due by email by end of Friday – send to zives@cis, dinkar@gradient.cisdinkar@gradient.cis


Download ppt "Introduction to SQL, the Structured Query Language Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems September 16, 2003."

Similar presentations


Ads by Google