You are on page 1of 36

Query languages: Allow manipulation and retrieval of data

from a database.
Relational model supports simple, powerful QLs:
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.
Two mathematical Query Languages form the basis for
real languages (e.g. SQL), and for implementation:
Relational Algebra: More operational (procedural), very useful
for representing execution plans.
Relational Calculus: Describes what users want, rather than
how to compute it: Non-operational, declarative.
A query is applied to relation instances, and the result of a
query is also a relation instance.
Schemas of input relations for a query are fixed.
The schema for the result of a given query is also fixed! -
determined by definition of query language constructs.
A relation schema can be thought of as the basic
information describing a table or relation.
This includes a set of column names, the data types
associated with each column, and the name
associated with the entire table. For example, a
relation schema for the relation called Students could
be expressed using the followingrepresentation:
Students(sid: string, name: string, login: string,age:
integer, gpa: real)
A relational database schema is a collection of relation
schemas, describing one or more relations.

Domain is synonymous with data type.

A relation instance is a set of tuples (also known as rows or


records) that each conform to the schema of the relation.

The relation cardinality is the number of tuples in the relation.

The relation degree is the number of fields (or columns) in the


relation.
R1 sid bid day
22 101 10/10/96
58 103 11/12/96
Sailors and Reserves
S1
sid sname rating age
relations for our examples.
Well use positional or named 22 dustin 7 45.0
field notation, assume that 31 lubber 8 55.5
names of fields in query
results are `inherited from 58 rusty 10 35.0
names of fields in query input
relations. S2 sid sname rating age
28 yuppy 9 35.0
31 lubber 8 55.5
44 guppy 5 35.0
58 rusty 10 35.0
Basic operations:
Selection ( ) Selects a subset of rows from relation.
Projection ( ) Deletes unwanted columns from relation.


Cross-product ( ) Allows us to combine two relations.
Set-difference ( ) Tuples in reln. 1, but not in reln. 2.
Union ( ) Tuples in reln. 1 and in reln. 2.
Additional operations:
Intersection, join, division, renaming: Not essential, but (very!)
useful.
Since each operation returns a relation, operations can be
composed: algebra is closed.
sname rating
yuppy 9
lubber 8
Deletes attributes that are not in guppy 5
projection list.
Extract columns from a relation. rusty 10
Schema of result contains exactly the
fields in the projection list, with the
sname,rating(S2)
same names that they had in the input
relation.
Projection operator has to eliminate
duplicates! Why? age
Note: real systems typically dont do
duplicate elimination unless the user
35.0
explicitly asks for it (by DISTINCT). 55.5
Why not?
age(S2)
sid sname rating age
28 yuppy 9 35.0
Selects rows that satisfy 58 rusty 10 35.0
selection condition.
No duplicates in result! rating 8(S2)
Why?
Schema of result identical to
schema of input relation.
What is Operator sname rating
composition?
Selection is distributive over yuppy 9
binary operators rusty 10
Selection is commutative
sname,rating( rating 8(S2))
All of these operations take sid sname rating age
two input relations, which 22 dustin 7 45.0
must be union-compatible: 31 lubber 8 55.5
Same number of fields.
58 rusty 10 35.0
`Corresponding fields have
44 guppy 5 35.0
the same type.
What is the schema of result?
28 yuppy 9 35.0
S1 S2

sid sname rating age sid sname rating age


31 lubber 8 55.5
22 dustin 7 45.0
58 rusty 10 35.0
S1 S2 S1 S2
Each row of S1 is paired with each row of R1.
Result schema has one field per field of S1 and R1, with
field names `inherited if possible.
Conflict: Both S1 and R1 have a field called sid.

(sid) sname rating age (sid) bid day


22 dustin 7 45.0 22 101 10/ 10/ 96
22 dustin 7 45.0 58 103 11/ 12/ 96
31 lubber 8 55.5 22 101 10/ 10/ 96
31 lubber 8 55.5 58 103 11/ 12/ 96
58 rusty 10 35.0 22 101 10/ 10/ 96
58 rusty 10 35.0 58 103 11/ 12/ 96
Joins: used to combine relations
The most general version of the join operation accepts a
join condition c and a pair of relation instances as
arguments and returns a relation instance.

The join condition is identical to a selection condition


in form.

Thus is defined to be a cross-product followed by a


selection.

Note that the condition c can (and typically does) refer


to attributes of both Rand S.

The reference to an attribute of a relation, say, R, can be


by position (of the form R.i) or by name (of the form
R.name).
Renaming
In the previous relation
operator:
S1 R1 field sid duplicated . To rename this
renaming operator is introduced .
The expression (R(F),E) takes any relational algebra expression E
and return the instance of relation R.
R contain same tuples as E but some fields are renamed.
The renaming list F , which is a list of terms having the form
oldname newname or
position newname

Ex: (C(1 sid1,5 sid 2),S1 R1)


Condition Join: R S ( R S)
c c
(sid) sname rating age (sid) bid day
22 dustin 7 45.0 58 103 11/ 12/ 96
31 lubber 8 55.5 58 103 11/ 12/ 96

Resultschema same as that of cross-product.


Fewer tuples than cross-product, might be able to
compute more efficiently
S1 R1
S1. sid R1. sid
Theta join combines tuples from different relations
provided they satisfy the theta condition. The join
condition is denoted by the symbol .
Notation : R1 R2
R1 and R2 are relations having attributes (A1,
A2, .., An) and (B1, B2,.. ,Bn) such that the
attributes dont have anything in common, that is
R1 R2 = .
Theta join can use all kinds of comparison
operators.
Equi-Join: A special case of condition join where the
condition c contains only equalities.

Result schema similar to cross-product, but only one copy


of fields for which equality is specified.
Student
Subjects
SID Name Std Class Subject
10 Math
101 Alex 10 10 English
11 Music
102 Maria 11 11 Sports

Student_Detail
STUDENT Student.Std = Subject.Class SUBJECT
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: Equijoin on all common fields.

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
HoD
CID Course Dept Dept Head
Databas CS Alex
CS01 CS
e
Mechani ME Maya
ME01 ME
cs
EE Mira
Electron
EE01 EE
ics

Courses HoD
Dept CID Course Head
CS CS01 Database Alex
ME ME01 Mechanics Maya
EE EE01 Electronics Mira
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.
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.
Right
Left
A B
A B
100 Alex
100 Database
102 Maya
101 Mechanics
102 Electronics 104 Mira

Courses HoD
A B C D
100 Database 100 Alex
101 Mechanics --- ---
102 Electronics 102 Maya
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.
Courses HoD
A B C D
100 Database 100 Alex
102 Electronics 102 Maya
--- --- 104 Mira
Full Outer Join: ( R S)

All the tuples from both participating


relations are included in the resulting
relation. If there are no matching tuples
for both relations, their respective
unmatched attributes are made NULL.

Courses HoD
A B C D
100 Database 100 Alex
101 Mechanics --- ---
102 Electronics 102 Maya
--- --- 104 Mira
SELECT * from class, cross JOIN class_info;

SELECT * from class, class_info where class.id =


class_info.id;

SELECT * from class NATURAL JOIN class_info;

SELECT * FROM class LEFT OUTER JOIN class_info


ON (class.id=class_info.id);

SELECT * FROM class RIGHT OUTER JOIN


class_info on (class.id=class_info.id);

SELECT * FROM class FULL OUTER JOIN class_info


on (class.id=class_info.id);
Given relational schema:
Sailors (sid, sname, rating, age)
Reservation (sid, bid, date)
Boats (bid, bname, color)

1) Find names of sailors whove reserved boat #103


2) Find names of sailors whove reserved a red boat
3) Find sailors whove reserved a red or a green boat
4) Find sailors whove reserved a red and a green boat
5) Find the names of sailors whove reserved all boats

You might also like