Unit-2 CH II
Unit-2 CH II
AND
CALCULUS
UNIT-II
Relational Query Languages
• Query languages: Allow manipulation and retrieval of
data from a database.
• Relational model supports simple, powerful query
languages:
– Strong formal foundation based on logic.
– Allows for much optimization.
• Query Languages != programming languages!
– QLs not expected to be “Turing complete”.
– QLs not intended to be used for complex calculations.
– QLs support easy, efficient access to large data sets.
2
Formal Relational Query Languages
Two mathematical Query Languages form the
basis for “real” languages (e.g. SQL), and for
implementation:
• Relational Algebra: More operational, very useful
for representing execution plans.
• Relational Calculus: Lets users describe what
they want, rather than how to compute it. (Non-
operational, declarative.)
4
R1 sid bid day
Example Instances 22 101 10/10/96
• “Sailors” and “Reserves” 58 103 11/12/96
relations for our examples.
• We’ll use positional or named S1 sid sname rating age
field notation, assume that 22 dustin 7 45.0
names of fields in query results
are `inherited’ from names of 31 lubber 8 55.5
fields in query input relations.
58 rusty 10 35.0
R c S = σc(RxS)
• Thus is defined to be a cross-product followed by a
0
selection.
• Note that the condition c can (and typically does) refer
to attributes of both R and S.
Condition/Theta (θ) Join
Student_detail
SID Name Std Class Subject
101 Alex 10 10 Math
101 Alex 10 10 English
102 Maria 11 11 Music
102 Maria 11 11 Sports
Natural Join
• Natural join does not use any comparison
operator.
• It does not concatenate the way a Cartesian
product does.
• We can perform a Natural Join only if there is at
least one common attribute that exists between
two relations.
• In addition, the attributes must have the same
name and domain.
• Natural join acts on those matching attributes
where the values of attributes in both the
relations are same.
Courses
CID Course Dept
CS01 Database CS
ME01 Mechanics ME
EE01 Electronics EE
HoD
Dept Head
CS Alex
ME Meena
EE Mira
Courses ⋈ HoD
Dept CID Course Head
CS CS01 Database Alex
ME ME01 Mechanics Meena
EE EE01 Electronics Mira
Outer Joins
• Theta Join, Equijoin, and Natural Join are called inner
joins.
• An inner join includes only those tuples with matching
attributes and the rest are discarded in the resulting
relation.
• Therefore, we need to use outer joins to include all the
tuples from the participating relations in the resulting
relation.
• There are three kinds of outer joins
- left outer join
- right outer join, and
- full outer join.
Left Outer Join(R S)
• All the tuples from the Left relation, R, are
included in the resulting relation.
• If there are tuples in R without any matching
tuple in the Right relation S, then the S-
attributes of the resulting relation are made
NULL.
• It gives the matching rows and the rows which
are in left table but not in right table.
Left
A B
100 Database
101 Mechanics Left Right
102 Electronics
A B D
Right 100 Database Alex
A D 101 Mechanics ---
100 Alex 102 Electronics Meena
102 Meena
104 Mira
Right Outer Join: ( R S)
• All the tuples from the Right relation, S, are
included in the resulting relation.
• If there are tuples in S without any matching
tuple in R, then the R-attributes of resulting
relation are made NULL.
• It gives the matching rows and the rows which
are in right table but not in left table.
Left
A B
100 Database Left Right
101 Mechanics A B D
102 Electronics 100 Database Alex
102 Electronics Meena
Right 104 --- Mira
A D
100 Alex
102 Meena
104 Mira
Full Outer Join: ( R S)
• All the tuples from both participating Left
relations are included in the resulting
relation. A B
• If there are no matching tuples for both 100 Database
relations, their respective unmatched
attributes are made NULL. 101 Mechanics
102 Electronics
• Left Right
A B D Right
100 Database Alex A D
101 Mechanics ---
100 Alex
102 Electronics Meena
104 --- Mira 102 Meena
104 Mira
Division
1 Badminton Badminton
2 Cricket Cricket
2 Badminton
4 Badminton
• To apply division operator as
STUDENT_SPORTS/ ALL_SPORTS
(Q4) Find the names of sailors who have reserved at least one boat.
A) Select s.sname from sailor s, reserves r, boats b where r.bid=b.bid and
s.sid=r.sid ;
(Q5) Find the names of sailors who have reserved a red boat and green boat.
D) A) Select s.sname from sailor s, reserves r, boats b where b.color = “red”
AND “green” and r.bid=b.bid and s.sid=r.sid;
Find sid’s of the sailors whose age>35
Select s.sid from sailor s where age>35
Find the name of the sailors and dates reserved by the sailors
Select s.sname, r.day from sailor s , reserves r where s.sid=r.sid;
Find the name and color of the boat of that sailor
select s.sname, b.color from sailor s, boat b, reserves r where s.sid=r.sid and r.bid=b.bid;
Find name and boat names of the sailors.
Select s.sname , b.bname from sailor s , boat b, reserves r where s.sid=r.sid and r.bid=b.bid;
Find the name of the sailor who have reserved a boat
select s.sname from sailor s , reserves r where s.sid=r.sid;
Find the name of the sailor who has reserved a boat 101
select s.sname from sailor s , reserves r where s.sid=r.sid and r.bid=101;
Find name of the sailor who has reserved a green boat
Select s.sname from sailor s , reserves r, boats b where b.color=“green” and s.sid=r.sid and
r.bid=b.bid;
Find the color of the boat reserved by Horatio.
select b.color from sailor s, reserves r, boats b where s.sid=r.sid and r.bid=b.bid and
s.sname=“horatio”;
Find the name of the sailor who has reserved a red or blue boat.
Select s.sname from sailor s, reserves r, boats b where s.sid=r.sid and r.bid=b.bid and
b.color=“red” or “blue”;
Find sid’s of sailor whose age>35 and who has not reserved a green boat.
Select s.sid from sailor s, reserves r, boat b where s.age>35 and s.sid=r.sid and r.bid=b.bid and
b.color!=“green”;
11)Find the name of the sailor’s who has reserved atleast two boats.
12)Find the name of all sailors who has reserved all boats
13)Find the names of sailors who has reserved all Interlake boat
14) Find the name of all sailors who has reserved all dipper boat
Find the name of all the sailors whose rating>8 and age>30
Select s.sname from sailor s where s.rating>8 INTERSECT Select s.sname from sailor s where
s.age >30
Find the name of the sailor who has reserved a red or a green boat
Select s.sname from sailor s, reserves r, boats b where s.sid=r.sid and r.bid=b.bid and
b.color=“red” UNION Select s.sname from sailor s, reserves r, boats b where s.sid=r.sid and
r.bid=b.bid and b.color=“green”;
Find the name of the sailors who has reserved both red and green boat
Select s.sname from sailor s, resreves r, boats b where s.sid=r.sid and r.bid=b.bid and
b.color=“red” INTERSECT Select s.sname from sailor s, resreves r, boats b where s.sid=r.sid and
r.bid=b.bid and b.color=“green” ;
Find the name of sailors who has reserved a red boat but not green boat
Select s.sname from sailor s, resreves r, boats b where s.sid=r.sid and r.bid=b.bid and
b.color=“red” MINUS Select s.sname from sailor s, resreves r, boats b where s.sid=r.sid and
r.bid=b.bid and b.color=“green” ;
Relational Calculus
• Relational Algebra is a procedural query language to
fetch data and which also explains how it is done
• Relational Calculus in non-procedural query language
and has no description about how the query will work
or the data will be fetched.
• It only focusses on what to do, and not on how to do it.
• Relational Calculus exists in two forms:
1. Tuple Relational Calculus (TRC)
2. Domain Relational Calculus (DRC)
Tuple Relational Calculus (TRC)
• The tuple relational calculus is specified to select
the tuples in a relation.
• TRC is used for selecting those tuples that satisfy
the given condition.
• In TRC, filtering variable uses the tuples of a
relation.
• The result of the relation can have one or more
tuples.
• Notation:
{t | P(t)} or {t | Condition (t)}
• where t = resulting tuples,
P(t) = known as Predicate and these are the
conditions that are used to fetch t
• Thus, it generates set of all tuples , such that
Predicate P(t) is true for t.
• P(t) may have various conditions logically combined
with OR (∨), AND (∧), NOT(¬).
• We can also specify column name using a . dot
operator, with the tuple variable to only get a certain
attribute(column) in result.
It also uses quantifiers:
Table: Student
First_Name Last_Name Age
Ajeet Singh 30
Chaitanya Singh 31
Rajeev Bhatia 27
Carl Pratap 28