15 more rows
Joins (1/2)
Equi-join
R3 = R1 o
nC R2
C is a condition on attributes of R1 and/or R2
Actors Movies Return all information about actors and
Name Age Addr Name Year Title their movies
BN 67 LA 2009 Valkyrie
BN 67 LA null null
Allmovies = Actors o
nA.N ame=M.N ame M ovies
M ovies1 = Age<50 AND Y ear=2011 (AllM ovies)
Result = N ame (M ovies1)
Notation (1/2)
Return the names and addresses of actors
over 35
N ame,Addr ( Age>35 (Actor)) N ame,Addr R2
R1 = Age>35 (Actor)
Age>35
R2 = N ame,Addr (R1)
Actor
Notation (2/2)
Return the names of actors below the age of 50 who
have acted in a movie in 2011
N ame ( Age<50 AND Y ear=2011 (Actors o
nA.N ame=M.N ame M ovies))
Allmovies = Actors o
nA.N ame=M.N ame M ovies
M ovies1 =
Age<50 AND Y ear=2011 (AllM ovies)
Result = N ame (M ovies1)
Actors Movies
Relational algebra for bags
Efficiency issues if we consider relations as sets
Extra effort to eliminate duplicates
Select, project, join (SPJ) work exactly the same
Applied to one tuple at a time
Set operations become bag operations
Need to be careful about semantics
Union, Intersection, Difference
More operators
Duplicate elimination (R)
Extended projection
Aggregation
count, min, max, sum, avg
Grouping
Sorting
Extended Projection
Same as projection, but some operations can
be directly expressed
Prices Return the total price of petrol
City Petrol Tax City,P etrol+T ax (P rices)
Bangalore 39.2 33.2
Delhi 38.2 31.5 City Petrol+Tax
Mumbai 41.47 30 Bangalore 72.4
Delhi 69.7
Mumbai 71.47
Aggregation and grouping (1/2)
Grouping L (R)
L is a list of grouping attributes and/or aggregate
operators
Return total boxoffice returns per movie
Movie City Boxoffice
aggregate
MI-IV LA 2,000,000 grouping attribute
Don-II LA 500,000
M ovie,Sum(Boxof f ice) (M ovies)
MI-IV NY 3,000,000
Abhishek 40 Mumbai
Bachchan SELECT *
FROM Actors
WHERE Addr = Mumbai
Selection (2/2)
Return all actors whose age is more Return all actors whose age is more
than 35. than 35 and who live in Mumbai
Age>35 (Actors) Age>35 and Addr=0 M umbai (Actors)
SELECT * SELECT *
FROM Actors FROM Actors
WHERE Age > 35 WHERE Age > 35 AND
Addr = Mumbai
Projection
Actors
Name Age Addr Return the name and age of all actors
Priyanka 34 Mumbai
Chopra N ame,Age (Actors)
Tom Cruise 54 LA
SELECT Name, Age
Bill Nighy 67 LA
FROM Actors
Abhishek 40 Mumbai
Bachchan
Return the addresses of the actors SELECT Addr
FROM Actors
Addr (Actors)
SELECT DISTINCT (Addr)
FROM Actors
Equi-Joins
Actors Movies
Name Age Addr Name Year Title Return all information about actors and
PC 34 Mumbai PC 2011 Don-II their movies
TC 54 LA TC 2011 MI-IV Actors o
nA.N ame=M.N ame M ovies
BN 67 LA BN 2009 Valkyrie
SELECT *
AB 40 Mumbai AB 2010 Raavan
FROM Actors, Movies
WHERE Actors.Name = Movies.Name
Left outer joins
Return all information about actors and
Actors Movies their movies
Name Age Addr Name Year Title Actors o
nA.N ame=M.N ame M ovies
PC 34 Mumbai PC 2011 Don-II
TC 54 LA TC 2011 MI-IV
SELECT *
BN 67 LA AB 2010 Raavan
FROM Actors LEFT OUTER JOIN Movies
AB 40 Mumbai ON (Actors.Name = Movies.Name)
What happens when a you compare something with a null value? Or when you
compare a null with a null?
Self Join
Return all grandparents and their
grand children
Actors Actors_1
Name Age Addr Parent Name Age Addr Parent
PC 34 Mumbai Madhu PC 34 Mumbai Madhu
TC 54 LA Mary TC 54 LA Mary
BN 67 LA Catherine BN 67 LA Catherine
AB 40 Mumbai Jaya AB 40 Mumbai Jaya
Jaya 68 Mumbai Indira Jaya 68 Mumbai Indira
SELECT *
FROM Actors AS Actors1, Actors AS Actors2
WHERE Actors1.Parent = Actors2.Name
Composition of operators (1/2)
Actors
Return the names and addresses of actors
Name Age Addr over 35
PC 34 Mumbai N ame,Addr ( Age>35 (Actor))
TC 54 LA
Allmovies = Actors o
nA.N ame=M.N ame M ovies
M ovies1 = Age<50 AND Y ear=2011 (AllM ovies)
Result = N ame (M ovies1)
SELECT Actors.Name
FROM Actors, Movies
WHERE Age < 50
AND Year = 2011
AND Actors.Name = Movies.Name
SQL 2
More operators
Duplicate elimination
Extended projection
Aggregation
count, min, max, sum, avg
Grouping
Sorting
Aggregation and grouping (1/2)
Grouping L (R)
L is a list of grouping attributes and/or aggregate
operators
Return total boxoffice returns per movie
Movie City Boxoffice
aggregate
MI-IV LA 2,000,000 grouping attribute
Don-II LA 500,000
M ovie,Sum(Boxof f ice) (M ovies)
MI-IV NY 3,000,000
SELECT L
FROM R
WHERE C1 op v1 Replace the value with a query
which returns a constant
Actors SELECT *
Name Age Addr
FROM Actors
WHERE Age <
PC 34 Mumbai (SELECT AVG(Age)
TC 54 LA FROM Actors)
BN 67 LA
AB 40 Mumbai
Conditions involving relations (1/2)
SELECT L
FROM R
WHERE C1 op v1 Replace the value with a query
which returns a constant
SELECT *
FROM Actors
WHERE Age < ANY
(SELECT Age
FROM Actors)
Correlated subqueries
Actors SELECT *
FROM Actors AS A
Name Age Addr
WHERE Age < ALL
PC 34 Mumbai (SELECT Age
TC 54 LA FROM Actors
WHERE Name = A.Name)
BN 67 LA
AB 40 Mumbai
Indexes and Views
Indexes (1/4)
Data structure to access specific tuples fast
Very important for query processing and query optimization
Useful when no. of results very small compared to the total no. of
tuples
Indexes (2/4)
SELECT * FROM Actor
WHERE Name = Priyanka
CREATE INDEX
NameIndex
ON Actor (Name)
Priyanka
Abhishek Tom
Aamir
Indexes (4/4)
Many variations
Multi-column
Clustered index
Multi-dimensional
Many data structures
B-tree and variants
Hash indexes
Views (1/5)
Views are tables created from existing tables
Materialized (physically exist)
CREATE MATERIALIZED VIEW
Virtual (dont physically exist)
CREATE VIEW
Offer a simplified view of the data
Secure data from non-authorized users
Materialized views used in speeding up query processing
Views (2/5)
Actors
PC 34
Can have any query here.
TC 54
BN 67
AB 40
Views (3/5)
Querying views same as querying any other
table ActorsView
Name Age
SELECT * SELECT * PC 34
FROM ActorView FROM ActorView
TC 54
WHERE Name = PC
BN 67
AB 40
SELECT Name, Addr
FROM ActorView Error! Addr does not exist in
WHERE Name = PC the view
Views (4/5)
Actors Movies
SELECT Name
FROM Actors55
WHERE Year = 2011
Queries with views
Actors Movies
CREATE VIEW ActorMovie2009 AS Name Age Addr Name Year Title
SELECT Actors.Name, Movies.Title,
Priyanka Chopra 34 Mumbai Priyanka Chopra 2011 Don-II
Movies.Year
FROM Actors, Movies Tom Cruise 54 LA Tom Cruise 2011 MI-IV
WHERE Actors.Name = Movies.Name Bill Nighy 67 LA Bill Nighy 2009 Valkyrie
AND Movies.Year >= 2009
Abhishek Bachchan 40 Mumbai Abhishek Bachchan 2010 Raavan
SELECT Actors.Name
FROM Actors, Movies SELECT Name
WHERE Actors.Name = Movies.Name FROM ActorMovie2009
AND Movies.Year >= 2009 WHERE Name = PC
AND Actors.Name = PC
SELECT Actors.Name
FROM Actors, Movies
WHERE Actors.Name = Movies.Name
AND Movies.Year >= 2008
AND Actors.Name = PC
Updating Views (1/3)
Can we update views?
Insertion, deletion, update CREATE VIEW ActorView AS
SELECT Name, Age
What happens to the base table? FROM Actors
INSERT INTO Actors Abhishek Bachchan 40 Mumbai Abhishek Bachchan 2010 Raavan
VALUES (Bruce Willis, NULL, NULL)
Type of constraint
Keyword, indicating a
constraint to follow
Name of constraint
Foreign key
Key or Unique constraint
SELECT Actors.Name
SELECT Name FROM Actors, Movies
FROM ActorMovie2009 WHERE Actors.Name = Movies.Name
WHERE Name = PC AND Movies.Year >= 2009
AND Actors.Name = PC
CREATE RULE X AS
ON <SELECT/INSERT> TO <table>
DO INSTEAD <task>