SQL and Relational Algebra Zaki Malik September 02, 2008.

Slides:



Advertisements
Similar presentations
1 Relational Algebra & Calculus Chapter 4, Part A (Relational Algebra) Query Formulation Exercise.
Advertisements

1 Database Systems 10.8, 2008 Lecture #4. 2 Course Administration Assignment #1 is due next Wed (outside 336/338). Course slides will now have black background.
1 Advanced SQL Queries. 2 Example Tables Used Reserves sidbidday /10/04 11/12/04 Sailors sidsnameratingage Dustin Lubber Rusty.
COMP 5138 Relational Database Management Systems Semester 2, 2007 Lecture 5A Relational Algebra.
Relational Algebra Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY courtesy of Joe Hellerstein for some slides.
1 TRC vs DRC 한욱신. 2 Queries in TRC and DRC  TRC Q = {t| f(t)} where t is a (free) tuple variable and f(t) is a well-formed formula  DRC.
Chapter (7): Advanced SQL
Relational Algebra Rohit Khokher. Relational Algebra Set Oriented Operations UnionIntersectionDifference Cartesian Product Relation Oriented Operations.
D ATABASE S YSTEMS I R ELATIONAL A LGEBRA. 22 R ELATIONAL Q UERY L ANGUAGES Query languages (QL): Allow manipulation and retrieval of data from a database.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A Modified by Donghui Zhang.
INFS614, Fall 08 1 Relational Algebra Lecture 4. INFS614, Fall 08 2 Relational Query Languages v Query languages: Allow manipulation and retrieval of.
1 Relational Algebra & Calculus. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
SQL Review.
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 90 Database Systems I SQL Queries.
FALL 2004CENG 351 File Structures and Data Managemnet1 Relational Algebra.
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental.
1 SQL (Simple Query Language). 2 Query Components A query can contain the following clauses –select –from –where –group by –having –order by Only select.
1 Relational Algebra. 2 Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational model supports.
CS 4432query processing1 CS4432: Database Systems II.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
Relational Algebra Chapter 4 - part I. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem.
Exercises ReservesSailorsBoats Find names of sailors who’ve reserved boat #103 Basic operations: Selection ( σ ) gives a subset of rows. Projection ( π.
Introduction to Database Systems 1 Relational Algebra Relational Model: Topic 3.
Relational Algebra. Relational Query Languages n Query = “retrieval program” n Language examples: ù Theoretical : 1. Relational Algebra 2. Relational.
Rutgers University Relational Algebra 198:541 Rutgers University.
Relational Algebra Chapter 4 - part I. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
CSCD343- Introduction to databases- A. Vaisman1 Relational Algebra.
Relational Algebra.
Relational Algebra, R. Ramakrishnan and J. Gehrke (with additions by Ch. Eick) 1 Relational Algebra.
Midterm Review Zaki Malik October 09, Basic Operators Covered.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L6_SQL(1) 1 SQL: Queries, Constraints, Triggers Chapter 5 – Part 1.
SQL Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby debby lilly.
SQL Examples CS3754 Class Note 11 CS3754 Class Note 11, John Shieh,
ICS 321 Fall 2009 SQL: Queries, Constraints, Triggers Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 9/8/20091Lipyeow.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra.
CS 4432query processing1 CS4432: Database Systems II Lecture #11 Professor Elke A. Rundensteiner.
1 Relational Algebra & Calculus Chapter 4, Part A (Relational Algebra)
1.1 CAS CS 460/660 Introduction to Database Systems Relational Algebra.
Database Management Systems 1 Raghu Ramakrishnan Relational Algebra Chpt 4 Xin Zhang.
Relational Algebra.
ICS 321 Fall 2011 The Relational Model of Data (i) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 8/29/20111Lipyeow.
1 Relational Algebra Chapter 4, Sections 4.1 – 4.2.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 4 Relational Algebra.
Database Management Systems 1 Raghu Ramakrishnan Relational Algebra Chpt 4 Xin Zhang.
CSCD34-Data Management Systems - A. Vaisman1 Relational Algebra.
Database Management Systems, R. Ramakrishnan1 Relational Algebra Module 3, Lecture 1.
CMPT 258 Database Systems Relational Algebra (Chapter 4)
CMPT 258 Database Systems SQL Queries (Chapter 5).
CSCI 4333 Database Design and Implementation – Exercise (2) Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
Final Review Zaki Malik November 20, Basic Operators Covered.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
1 CS122A: Introduction to Data Management Lecture #7 Relational Algebra I Instructor: Chen Li.
1 CS122A: Introduction to Data Management Lecture 8 Introduction to SQL Instructor: Chen Li.
Relational Algebra. CENG 3512 Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational model supports.
Database Systems (資料庫系統)
© פרופ' יהושע שגיב, האוניברסיטה העברית
CS4432: Database Systems II
01/31/11 SQL Examples Edited by John Shieh CS3754 Classnote #10.
Relational Algebra Chapter 4, Part A
Relational Algebra 461 The slides for this text are organized into chapters. This lecture covers relational algebra, from Chapter 4. The relational calculus.
Database Systems 10/13/2010 Lecture #4.
Relational Algebra.
LECTURE 3: Relational Algebra
CENG 351 File Structures and Data Managemnet
Relational Algebra Chapter 4 - part I.
Presentation transcript:

SQL and Relational Algebra Zaki Malik September 02, 2008

Basics of Relational Algebra Four types of operators: – Select/Show parts of a single relation: projection and selection. – Usual set operations (union, intersection, difference). – Combine the tuples of two relations, such as cartesian product and joins. – Renaming.

Projection The projection operator produces from a relation R a new relation containing only some of R’s columns. “Delete” (i.e. not show) attributes not in projection list. Duplicates eliminated To obtain a relation containing only the columns A 1,A 2,... A n of R RA: π A 1,A 2,... A n (R) SQL: SELECT A 1,A 2,... A n FROM R;

Selection The selection operator applied to a relation R produces a new relation with a subset of R’s tuples. The tuples in the resulting relation satisfy some condition C that involves the attributes of R. – with duplicate removal RA: σ (R) SQL: SELECT * FROM R WHERE C; The WHERE clause of a SQL command corresponds to σ ( ). c

Union The union of two relations R and S is the set of tuples that are in R or in S or in both. – R and S must have identical sets of attributes and the types of the attributes must be the same. – The attributes of R and S must occur in the same order. What is the schema of the result ? RA: R U S SQL: (SELECT * FROM R) UNION (SELECT * FROM S);

Union S1 S2

Intersection The intersection of two relations R and S is the set of tuples that are in both R and S. Same conditions hold on R and S as for the union operator. – R and S must have identical sets of attributes and the types of the attributes must be the same. – The attributes of R and S must occur in the same order. RA: R ∩ S SQL: (SELECT * FROM R) INTERSECT (SELECT * FROM S);

Intersection S1 S2

Difference The difference of two relations R and S is the set of tuples that are in R but not in S. Same conditions hold on R and S as for the union operator. – R and S must have identical sets of attributes and the types of the attributes must be the same. – The attributes of R and S must occur in the same order. RA: R - S SQL: (SELECT * FROM R) EXCEPT (SELECT * FROM S); R – (R – S)= R ∩ S

Difference S1 S2

Cartesian Product The Cartesian product (or cross-product or product) of two relations R and S is a the set of pairs that can be formed by pairing each tuple of R with each tuple of S. – The result is a relation whose schema is the schema for R followed by the schema for S. RA: R X S SQL: SELECT * FROM R, S ;

Cartesian Product R1 S1 S1 x R1 We rename attributes to avoid ambiguity or we prefix attribute with the name of the relation it belongs to. ?

Theta-Join The theta-join of two relations R and S is the set of tuples in the Cartesian product of R and S that satisfy some condition C. RA: R ∞ S SQL: SELECT * FROM R, S WHERE C; R ∞ S = C CC σ (R x S)

Theta-Join R1 S1

Natural Join The natural join of two relations R and S is a set of pairs of tuples, one from R and one from S, that agree on whatever attributes are common to the schemas of R and S. The schema for the result contains the union of the attributes of R and S. Assume the schemas R(A,B, C) and S(B, C,D) RA: R ∞ S SQL: SELECT * FROM R, S WHERE R.B = S.B AND R.C = S.C;

Operators Covered So far

Renaming If two relations have the same attribute, disambiguate the attributes by prefixing the attribute with the name of the relation it belongs to. How do we answer the query “Name pairs of students who live at the same address”? Students(Name, Address) – We need to take the cross-product of Students with itself? – How do we refer to the two “copies” of Students? – Use the rename operator. RA: : give R the name S; R has n attributes, which are called A 1,A 2,...,A n in S SQL: Use the AS keyword in the FROM clause: Students AS Students1 renames Students to Students1. SQL: Use the AS keyword in the SELECT clause to rename attributes. S ( A 1,A 2,... A n ) (R)

Renaming Are these correct ? No !!! the result includes tuples where a student is paired with himself/herself Solution: Add the condition S1.name <> S2.name.

Practicing Relational Algebra

Q1: Find names of sailors who have reserved boat #103 Reserves(sid, bid, day) Sailors(sid, sname, rating, age) Solution 3 (using rename operator) P(Temp1 (σ bid = 103 Reserves)) P(Temp2 (Temp1 ∞ Sailors)) π sname (Temp2) Solution 2 (more efficient) π sname ((σ bid = 103 Reserves) ∞ Sailors) Solution 1: π sname (σ bid = 103 (Reserves ∞ Sailors))

Q2: Find names of sailors who have reserved a red boat Reserves(sid, bid, day) Sailors(sid, sname, rating, age) Boats(bid, bname, color) Solution 2 (more efficient) π sname (π sid ((π bid σ color = ‘ red ’ Boats)∞ Reserves )∞ Sailors ) Solution 1: π sname ((σ color = ‘ red ’ Boats) ∞ Reserves ∞ Sailors )

Q3: Find the colors of boats reserved by Lubber Reserves(sid, bid, day) Sailors(sid, sname, rating, age) Boats(bid, bname, color) Solution: π color ((σ sname = ‘ Lubber ’ Sailor)∞ Reserves ∞ Boats ) Reserves(sid, bid, day) Sailors(sid, sname, rating, age) Boats(bid, bname, color)

Q4: Find the names of sailors who have reserved at least one boat Reserves(sid, bid, day) Sailors(sid, sname, rating, age) Boats(bid, bname, color) Solution: π sname (Sailor∞ Reserves) Reserves(sid, bid, day) Sailors(sid, sname, rating, age) Boats(bid, bname, color)

Q5: Find the names of sailors who have reserved a red or a green boat Reserves(sid, bid, day) Sailors(sid, sname, rating, age) Boats(bid, bname, color) Solution: π sname (σ color= ‘ red ’ or color = ‘ green ’ Boats ∞ Reserves ∞ Sailors) Reserves(sid, bid, day) Sailors(sid, sname, rating, age) Boats(bid, bname, color)

Q6: Find the names of sailors who have reserved a red and a green boat Reserves(sid, bid, day) Sailors(sid, sname, rating, age) Boats(bid, bname, color) Solution: π sname (σ color= ‘ red ’ and color = ‘ green ’ Boats ∞ Reserves ∞ Sailors) Reserves(sid, bid, day) Sailors(sid, sname, rating, age) Boats(bid, bname, color) A ship cannot have TWO colors at the same time π sname (σ color= ‘ red ’ Boats ∞ Reserves ∞ Sailors) ∩ π sname (σ color = ‘ green ’ Boats ∞ Reserves ∞ Sailors)

Q7: Find the sids of sailors with age over 20 who have not reserved a red boat Solution: π sid (σ age>20 Sailors) – π sid ((σ color= ‘ red ’ Boats) ∞ Reserves) Reserves(sid, bid, day) Sailors(sid, sname, rating, age) Boats(bid, bname, color) Strategy ? ? ? Find all sailors (sids) with age over 20 Find all sailors (sids) who have reserved a red boat Take their set difference