SlideShare a Scribd company logo
1 of 79
Download to read offline
Relational Query
Languages
Professor Navneet Goyal
Department of Computer Science & Information Systems
BITS, Pilani
© Prof. Navneet Goyal, BITS, Pilani
Relational Query
Languages
n A major strength of the relational
model: supports simple, powerful
querying of data.
n Queries can be written intuitively,
and the DBMS is responsible for
efficient evaluation.
n The key: precise semantics for
relational queries.
n Allows the optimizer to extensively re-
order operations, and still ensure that
the answer does not change.
© Prof. Navneet Goyal, BITS, Pilani
Relational Query
Languages
n Query languages: Allow manipulation
and retrieval of data from a database.
n Relational model supports simple,
powerful QLs:
n Strong formal foundation based on logic.
n Allows for much optimization.
n Query Languages != programming
languages!
n QLs not expected to be “Turing complete”.
n QLs not intended to be used for complex
calculations.
n QLs support easy, efficient access to large data
sets.
© Prof. Navneet Goyal, BITS, Pilani
Formal Relational
Query Languages
n Two mathematical Query
Languages form the basis for “real”
languages (e.g. SQL), and for
implementation:
n Relational Algebra: More operational,
very useful for representing execution
plans.
n Relational Calculus: Lets users
describe what they want, rather than
how to compute it. (Non-operational,
declarative.)
© Prof. Navneet Goyal, BITS, Pilani
The SQL Query
Language
n Developed by IBM’s System-R Project
in the 1970s
n Need for a standard since it is used by
many vendors
n Standards:
n SQL-86
n SQL-89 (minor revision)
n SQL-92 (major revision)
n SQL-99 (major extensions, current
standard)
n Find Out what is SQL-MM!!
© Prof. Navneet Goyal, BITS, Pilani
The SQL Query
Language
n SQL has been influenced by both
Relational Algebra (RA) & Relational
Calculus (RC)
n More so by RC, particularly Tuple
relational Calculus (TRC)
n The other variant of RC is Domain
Relational Calculus (DRC) which has
greatly influenced Query By Example
(QBE)
© Prof. Navneet Goyal, BITS, Pilani
The SQL Query
Language
n SQL consists of:
n DDL (Data Definition Language)
• Create conceptual schema
n DML (Data Manipulation Language)
• Relational operators
• Insert, Delete, Update
n VDL (View Definition Language)
• Specify user views & their mapping to the
conceptual schema (in most DBMSs, done by
DDL)
n SDL (Storage Definition Language)
• File organization
• Indexes
© Prof. Navneet Goyal, BITS, Pilani
The SQL Query
Language
n SDL is being removed from SQL
n DMLs
n High-level or nonprocedural (declarative)
• Can be entered at the SQL > or can be embedded in a
general purpose programming language
• Can specify & retrieve many records in a single DML
statement & are hence called Set-at-a-time DMLs
n Low-level or procedural
• Must be embedded in a general purpose
programming language
• Typically retrieves individual records or objects from
the DB & processes each separately
• Therefore it needs PL constructs like looping
• Record-at-a-time DMLs
© Prof. Navneet Goyal, BITS, Pilani
The SQL Query
Language
DMLs
n Whenever DML statements are embedded in a PL,
that language is called as the host language and
the DML is called the Data Sublanguage
n In object DBs, the host language & data
sublanguage form one integrated language – for
eg. C++ with some extensions to support
database functionality
n Some RDBMSs also provide integrated languages –
for eg. ORACLE’s PL/SQL.
© Prof. Navneet Goyal, BITS, Pilani
Operations on
Relations
n Restrict
n Project
n Join
n Divide
n Union
n Intersection
n Difference
n Product
Relational Operations
Set Operations
© Prof. Navneet Goyal, BITS, Pilani
Relational Algebra
n Collection of operations on relations
n 8 operators + Rename operator
n Codd’s 8 operators do not form a minimal
set
n Some of them are not primitive
n Join, Intersect, & Divide can be defined in
terms of other 5
n None of these 5 can be defined in terms of
the remaining 4
n MINIMAL SET
n Join, Intersect, & Divide are important &
should be supported directly
© Prof. Navneet Goyal, BITS, Pilani
Relational Algebra
n Some common DB requests can
not be performed with the RA
operations
n Additional operations are
developed
n Aggregate functions & additional
types of joins
© Prof. Navneet Goyal, BITS, Pilani
Relational Algebra
IMPORTANCE OF RA
n Provides a formal foundation for
relational model operations
n Plays a pivotal role in query
optimization in RDBMSs
n Some of its concepts are
implemented in SQL
n Expressive power of RA is used as a
metric of how powerful a relational
DB query language is!!
© Prof. Navneet Goyal, BITS, Pilani
Relational Completeness
n A relational DB query language is
said to be Relationally Complete, if
it can express all queries that we can
express in RA
n Relational DB query languages are
expected to be relationally complete
n Commercial query languages like
SQL, support features that allow us
to express some queries that are not
possible in RA
© Prof. Navneet Goyal, BITS, Pilani
Restrict & Project
© Prof. Navneet Goyal, BITS, Pilani
Union, Intersection &
Difference
© Prof. Navneet Goyal, BITS, Pilani
Union, Intersection &
Difference
Union Compatibility: r U s is valid if:
n Relations r & s have the same arity
n Domains of the ith attribute of r is the
same as the domain of the ith attribute of
s, ⍱ i.
Note that r & s can be either database
relations or derived relations
© Prof. Navneet Goyal, BITS, Pilani
Product & Divide
A
B
C
X
Y
A
A
B
B
C
C
X
Y
X
Y
X
Y
X
Z
A
PRODUCT
DIVIDE BY
A
A
A
B
C
X
Y
Z
X
Y
© Prof. Navneet Goyal, BITS, Pilani
Divide
DIVIDE
P1
P2
P4
S1
S1
S2
S2
S2
S2
S3
S4
S4
P1
P2
P3
P4
P1
P2
P2
P2
P4
S1
S2
S3
S4
Shipment
P2
P4
P2
S1
S4
S2
Parts
© Prof. Navneet Goyal, BITS, Pilani
Join
A1
A2
A3
B1
B1
B2
B1
B2
B3
C1
C2
C3
A1
A2
A3
B1
B1
B2
C1
C1
C2
NATURAL
JOIN
© Prof. Navneet Goyal, BITS, Pilani
Closure
n A relation is closed under
relational and set operators
n The result of these operators on
relation(s) is another relation
n Output from one operation can
become input to another
n Nested Expressions possible
© Prof. Navneet Goyal, BITS, Pilani
Example Schema
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.5
58 rusty 10 35.0
sid sname rating age
28 yuppy 9 35.0
31 lubber 8 55.5
44 guppy 5 35.0
58 rusty 10 35.0
sid bid day
22 101 10/10/96
58 103 11/12/96
R1 S1
S2
© Prof. Navneet Goyal, BITS, Pilani
Projection
sname rating
yuppy 9
lubber 8
guppy 5
rusty 10
πsname rating
S
,
( )
2
age
35.0
55.5
πage S
( )
2
n Deletes attributes that are not in
projection list.
n Schema of result contains exactly
the fields in the projection list,
with the same names that they
had in the (only) input relation.
n Projection operator has to
eliminate duplicates! (Why??)
n Note: real systems typically
don’t do duplicate elimination
unless the user explicitly asks
for it. (Why not?)
© Prof. Navneet Goyal, BITS, Pilani
Selection
σrating
S
>8
2
( )
sid sname rating age
28 yuppy 9 35.0
58 rusty 10 35.0
sname rating
yuppy 9
rusty 10
π σ
sname rating rating
S
,
( ( ))
>8
2
n Selects rows that
satisfy selection
condition.
n No duplicates in
result! (Why?)
n Schema of result
identical to schema of
(only) input relation.
n Result relation can be
the input for another
relational algebra
operation! (Operator
composition.)
© Prof. Navneet Goyal, BITS, Pilani
Union, Intersection,
Set-Difference
n All of these operations take
two input relations, which
must be union-compatible:
n Same number of fields
n `Corresponding’ fields
have the same
domain/data type
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.5
58 rusty 10 35.0
44 guppy 5 35.0
28 yuppy 9 35.0
sid sname rating age
31 lubber 8 55.5
58 rusty 10 35.0
S S
1 2
∪
S S
1 2
∩
sid sname rating age
22 dustin 7 45.0
S S
1 2
−
© Prof. Navneet Goyal, BITS, Pilani
Cross-Product
n Each row of S1 is paired with each row of R1.
n Result schema has one field per field of S1 and R1, with
field names `inherited’ if possible.
n Conflict: Both S1 and R1 have a field called sid.
ρ ( ( , ), )
C sid sid S R
1 1 5 2 1 1
→ → ×
(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
§ Renaming operator:
© Prof. Navneet Goyal, BITS, Pilani
Joins
n Condition Join:
n Result schema same as that of cross-
product.
n Fewer tuples than cross-product, might
be able to compute more efficiently
n Sometimes called a theta-join.
R c S c R S
!
" = ×
σ ( )
(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
S R
S sid R sid
1 1
1 1
!
"
. .
<
© Prof. Navneet Goyal, BITS, Pilani
Joins
n Equi-Join: A special case of condition join
where the condition c contains only equalities
n Result schema similar to cross-product, but
only one copy of fields for which equality is
specified.
n Natural Join: Equijoin on all common fields.
sid sname rating age bid day
22 dustin 7 45.0 101 10/10/96
58 rusty 10 35.0 103 11/12/96
S R
sid
1 1
!
"
© Prof. Navneet Goyal, BITS, Pilani
Examples of Division A/B
sno pno
s1 p1
s1 p2
s1 p3
s1 p4
s2 p1
s2 p2
s3 p2
s4 p2
s4 p4
pno
p2
pno
p2
p4
pno
p1
p2
p4
sno
s1
s2
s3
s4
sno
s1
s4
sno
s1
A
B1
B2
B3
A/B1 A/B2 A/B3
© Prof. Navneet Goyal, BITS, Pilani
Find names of sailors who’ve
reserved boat #103
n Solution 1: π σ
sname bid
serves Sailors
(( Re ) )
=103
!
"
n Solution 2: )
Re
,
1
(
103
serves
Temp
bid =
σ
ρ
)
1
,
2
( Sailors
Temp
Temp !
"
ρ
π sname Temp
( )
2
n Solution 3: ))
Re
(
103
( Sailors
serves
bid
sname !
"
=
σ
π
Query
Optimizer
© Prof. Navneet Goyal, BITS, Pilani
Find names of sailors who’ve
reserved a red boat
n Information about boat color only available
in Boats; so need an extra join:
π σ
sname color red
Boats serves Sailors
((
' '
) Re )
=
!
" !
"
v A more efficient solution:
)
)
Re
)
'
'
((
( Sailors
s
Boats
red
color
bid
sid
sname !
"
!
"
=
σ
π
π
π
A query optimizer can find this, given the first solution!
Query
Optimizer
© Prof. Navneet Goyal, BITS, Pilani
Find sailors who’ve reserved a
red or a green boat
n Can identify all red or green boats, then find
sailors who’ve reserved one of these boats:
ρ σ
( ,(
' ' ' '
))
Tempboats
color red color green
Boats
= ∨ =
π sname Tempboats serves Sailors
( Re )
!
" !
"
v Can also define Tempboats using union! (How?)
v What happens if is replaced by in this query?
∨ ∧
© Prof. Navneet Goyal, BITS, Pilani
Find sailors who’ve reserved a
red and a green boat
n Previous approach won’t work! Must
identify sailors who’ve reserved red boats,
sailors who’ve reserved green boats, then
find the intersection
ρ π σ
( , ((
' '
) Re ))
Tempred
sid color red
Boats serves
=
!
"
π sname Tempred Tempgreen Sailors
(( ) )
∩ !
"
))
Re
)
'
'
(
(
,
( serves
Boats
green
color
sid
Tempgreen !
"
=
σ
π
ρ
© Prof. Navneet Goyal, BITS, Pilani
Find the names of sailors
who’ve reserved all boats
n Uses division; schemas of the input
relations to / must be carefully chosen:
))
(
/
)
Re
,
(
,
( Boats
bid
serves
bid
sid
Tempsids π
π
ρ
π sname Tempsids Sailors
( )
!
"
v To find sailors who’ve reserved all ‘Interlake’ boats:
/ (
' '
)
π σ
bid bname Interlake
Boats
=
.....
© Prof. Navneet Goyal, BITS, Pilani
Summary
n The relational model has rigorously
defined query languages that are
simple and powerful.
n Relational algebra is more
operational; useful as internal
representation for query evaluation
plans.
n Several ways of expressing a given
query; a query optimizer should
choose the most efficient version.
© Prof. Navneet Goyal, BITS, Pilani
Summary
n Role of RA in query optimization
© Prof. Navneet Goyal, BITS, Pilani
Role of RA in QO
n Parsing and translation
n Translate the query into its internal form.
n Translation is similar to the work performed
by the parser of a compiler
n Parser checks syntax, verifies relations
n Parse tree representation
n This is then translated into RA expression
© Prof. Navneet Goyal, BITS, Pilani
Role of RA in QO
n Query Execution Plan
n In SQL, a query can be expressed is several ways
n Each SQL query can itself be translated into RA expression
in many ways
n An RA expression only partially tells you how to evaluate a
query
n Several ways to evaluate RA expression
n Annotate RA expression with instructions specifying how to
evaluate each operation
n Annotation may state the algorithm to be used for a specific
operation or the particular index to use
n Annotated RAE is called an evaluation primitive
n Sequence of primitive operations is a QEP
© Prof. Navneet Goyal, BITS, Pilani
Role of RA in QO
n Example
select balance
from account
where balance < 2500
n RAEs
n σbalance<2500(∏balance(account))
n ∏balance(σbalance<2500(account))
n E.g., we can use an index on balance to find
accounts with balance < 2500,
n or can perform complete relation scan and
discard accounts with balance ≥ 2500
© Prof. Navneet Goyal, BITS, Pilani
Role of RA in QO
Query Execution Plan
© Prof. Navneet Goyal, BITS, Pilani
Role of RA in QO
© Prof. Navneet Goyal, BITS, Pilani
Role of RA in QO
© Prof. Navneet Goyal, BITS, Pilani
Summary
n What we studied about RA is not used in
commercial systems!!!
n We assumed a SET model
n Commercial systems use a BAG model
n What are BAGS?
n Why BAGS?
© Prof. Navneet Goyal, BITS, Pilani
Relational Operations on
BAGS
n So far we discussed relational operations
on relations (sets of tuples)
n In commercial systems, a relation is
allowed to have duplicate tuples
n If a set is allowed to have multiple
occurrences of a member, it is called a
BAG or MULTISET
n Set – A relation without duplicate tuples
n Bag – A relation that may or may not have
duplicate tuples
© Prof. Navneet Goyal, BITS, Pilani
WHY BAGS?
n Allowing relations to be bags
can speed up operations on
relations
n For Example, allowing the
result to be a bag could speed
up union of two relations
n Duplicate elimination not
required
© Prof. Navneet Goyal, BITS, Pilani
WHY BAGS?
n Another Example:
Projection of relations
n Each projected tuple
must be compared with
other projected tuples
to make sure that each
projection tuple
appears only once
n If bags are accepted as
results, we simply need
to project each tuple &
add it to the result
A B C
1 2 5
3 4 6
1 2 7
1 2 8
A B
1 2
3 4
A B
1 2
3 4
1 2
1 2
© Prof. Navneet Goyal, BITS, Pilani
WHY BAGS?
If we are
projecting a
relation to take
an aggregate
(say AVG), we
could not use
the set model
A B C
1 2 5
3 4 6
1 2 7
1 2 8
A B
1 2
3 4
A B
1 2
3 4
1 2
1 2
AVG=2
AVG=1.5
© Prof. Navneet Goyal, BITS, Pilani
Union, Intersection, &
Difference of BAGS
UNION
n Add the no. of occurrences of each
tuple
n If R is a bag in which tuple t appears
n times, & S is a bag in which tuple t
appears m times, then in the bag
R∪S, t appears n+m times
n Either n or m or both could be zero
© Prof. Navneet Goyal, BITS, Pilani
Union, Intersection, &
Difference of BAGS
INTERSECTION
n If R is a bag in which tuple t
appears n times, & S is a bag in
which tuple t appears m times,
then in the bag R∩S, t appears
min(n,m) times
© Prof. Navneet Goyal, BITS, Pilani
Union, Intersection, &
Difference of BAGS
DIFFERENCE
n If R is a bag in which tuple t appears n
times, & S is a bag in which tuple t appears
m times, then in the bag R-S, tuple t
appears max(0, n-m) times
n If t appears in R more times than it
appears in S, then in R-S, t appears the no.
of times it appears in R, minus the no. of
times it appears in S
n If n=m, then t does not appear in R-S at all
n Occurrences of t in S each cancel one
occurrence in R
© Prof. Navneet Goyal, BITS, Pilani
Union, Intersection, &
Difference of BAGS
A B
1 2
3 4
1 2
1 2
A B
1 2
3 4
3 4
5 6
R S
Write down R∪S, R∩S, R-S, & S-R
© Prof. Navneet Goyal, BITS, Pilani
Union, Intersection, &
Difference of BAGS
2
1
4
3
2
1
4
3
6
5
2
1
4
3
2
1
B
A
4
3
2
1
B
A
R∪S R∩S R-S
2
1
2
1
B
A S-R
6
5
4
3
B
A
© Prof. Navneet Goyal, BITS, Pilani
Projection of BAGS
A B C
1 2 5
3 4 6
1 2 7
1 2 8
R
Write down πA, B (R)
A B
1 2
3 4
1 2
1 2
πA, B (R)
© Prof. Navneet Goyal, BITS, Pilani
Selection on BAGS
A B C
1 2 5
3 4 6
1 2 7
1 2 7
R
Write down σC>=6 (R)
A B C
3 4 6
1 2 7
1 2 7
σC>=6 (R)
© Prof. Navneet Goyal, BITS, Pilani
Product of BAGS
A B
1 2
1 2
R
Write down R x S
B C
2 3
4 5
4 5
S
A R.B S.B C
1 2 2 3
1 2 2 3
1 2 4 5
1 2 4 5
1 2 4 5
© Prof. Navneet Goyal, BITS, Pilani
Natural Join of BAGS
A B
1 2
1 2
B C
2 3
4 5
4 5
R
Write down R S
S
A B C
1 2 3
1 2 3
!
"
© Prof. Navneet Goyal, BITS, Pilani
Theta-Join of BAGS
A B
1 2
1 2
B C
2 3
4 5
4 5
R
Write down R S
S
A R.B S.B C
1 2 4 5
1 2 4 5
1 2 4 5
1 2 4 5
!
"
R.B < S.B
© Prof. Navneet Goyal, BITS, Pilani
Extended Operators of
RA
n Duplicate-Elimination Operator δ
n Aggregation Operators
• Sum
• Count
• Average
• Max
• Min
n Grouping Operator γ
n Sorting Operator τ
n Extended Projection
n Outerjoin Operator
© Prof. Navneet Goyal, BITS, Pilani
Duplicate-Elimination
Operator δ
n Converts a bag to a set
A B
1 2
3 4
1 2
1 2
R A B
1 2
3 4
δ(R)
© Prof. Navneet Goyal, BITS, Pilani
Aggregate Functions &
Operations
n Aggregation function takes a collection of values and
returns a single value as a result.
avg: average value
min: minimum value
max: maximum value
sum: sum of values
count: number of values
n Aggregate operation in relational algebra: General Form
E is any relational-algebra expression
n G1, G2 …, Gn is a list of attributes on which to group
(can be empty)
n Each Fi is an aggregate function
n Each Ai is an attribute name
© Prof. Navneet Goyal, BITS, Pilani
Aggregate Operation –
Example
Relation r: A B
α
α
β
β
α
β
β
β
C
7
7
3
10
g sum(c) (r) sum(C )
27
© Prof. Navneet Goyal, BITS, Pilani
Aggregate Operation –
Example
Relation account grouped by branch-name:
branch_name g sum(balance) (account)
branch_name account_number balance
Perryridge
Perryridge
Brighton
Brighton
Redwood
A-102
A-201
A-217
A-215
A-222
400
900
750
750
700
branch_name sum(balance)
Perryridge
Brighton
Redwood
1300
1500
700
© Prof. Navneet Goyal, BITS, Pilani
Aggregate Functions
(Cont.)
n Result of aggregation does not
have a name
n Can use rename operation to give it a
name
n For convenience, we permit renaming
as part of aggregate operation
branch_name g sum(balance)as sum_balance(account)
© Prof. Navneet Goyal, BITS, Pilani
Generalized Projection
n Extends the projection operation by allowing
arithmetic functions to be used in the projection
list.
n E is any relational-algebra expression
n Each of F1, F2, …, Fn are are arithmetic
expressions involving constants and attributes in
the schema of E.
n Given relation credit_info(customer_name, limit,
credit_balance), find how much more each person
can spend:
∏customer_name, limit – credit_balance (credit_info)
© Prof. Navneet Goyal, BITS, Pilani
Generalized Projection
Given relation credit_info(customer_name, limit,
credit_balance), find how much more each person can spend:
∏customer_name, (limit – credit_balance) as credit_available (credit_info)
1750
1500
700
400
Cust_name Credit_bal
Curry
Hayes
Jones
Smith
limit
2000
1500
6000
2000
Cust_name
Curry
Hayes
Jones
Smith
Credit_available
250
0
5300
1600
© Prof. Navneet Goyal, BITS, Pilani
Outer Join
n An extension of the join operation that
avoids loss of information.
n Computes the join and then adds tuples
form one relation that does not match
tuples in the other relation to the result of
the join.
n Uses null values:
n null signifies that the value is unknown or does
not exist
n All comparisons involving null are (roughly
speaking) false by definition.
• We shall study precise meaning of comparisons
with nulls later
© Prof. Navneet Goyal, BITS, Pilani
Outer Join – Example
Relation loan
Relation borrower
customer_name loan_number
Jones
Smith
Hayes
L-170
L-230
L-155
3000
4000
1700
loan_number amount
L-170
L-230
L-260
branch_name
Downtown
Redwood
Perryridge
© Prof. Navneet Goyal, BITS, Pilani
Outer Join – Example
Inner Join
loan Borrower
loan_number amount
L-170
L-230
3000
4000
customer_name
Jones
Smith
branch_name
Downtown
Redwood
Jones
Smith
null
loan_number amount
L-170
L-230
L-260
3000
4000
1700
customer_name
branch_name
Downtown
Redwood
Perryridge
Left Outer Join
loan Borrower
© Prof. Navneet Goyal, BITS, Pilani
Outer Join – Example
loan_number amount
L-170
L-230
L-155
3000
4000
null
customer_name
Jones
Smith
Hayes
branch_name
Downtown
Redwood
null
loan_number amount
L-170
L-230
L-260
L-155
3000
4000
1700
null
customer_name
Jones
Smith
null
Hayes
branch_name
Downtown
Redwood
Perryridge
null
■ Full Outer Join
loan borrower
■ Right Outer Join
loan borrower
© Prof. Navneet Goyal, BITS, Pilani
Outer Join
n Left join, or inner join, select rows
common to the participating tables to
a join
n Selecting elements in a table
regardless of whether they are
present in the second table
n OUTER JOIN is the solution
n In Oracle, we will place an "(+)" in
the WHERE clause on the other side
of the table for which we want to
include all the rows.
© Prof. Navneet Goyal, BITS, Pilani
Outer Join
store_name Sales Date
Los Angeles $1500 Jan-05-1999
San Diego $250 Jan-07-1999
Los Angeles $300 Jan-08-1999
Boston $700 Jan-08-1999
-­ We  want  to  find  out  the  sales  amount  for  all  of  the  stores
-­ If  we  do  a  regular  join,  we  will  not  be  able  to  get  what  we  want  because  we  
will          have  missed  "New  York,"  since  it  does  not  appear  in  the  
Store_Information table  
SELECT  A1.store_name,  SUM(A2.Sales)  SALES  
FROM  Geography  A1,  Store_Information  A2  
WHERE  A1.store_name  =  A2.store_name  (+)  
GROUP  BY  A1.store_name
Store_Information Geography
store_name    SALES
Boston                      $700
New  York                              
Los  Angeles    $1800
San  Diego            $250
region_name store_name
East Boston
East New York
West Los Angeles
West San Diego
© Prof. Navneet Goyal, BITS, Pilani
NVL Function
n In Oracle/PLSQL, the NVL function lets you substitutes a value when a null
value is encountered.
n NVL (string1, replace_with )
n string1 is the string to test for a null value. Replace_with is the value
returned if string1 is null.
n Example #1:
n select NVL (supplier_city, 'n/a')
from suppliers;
n The SQL statement above would return 'n/a' if the supplier_city field
contained a null value. Otherwise, it would return the supplier_city value.
n Example #2:
n select supplier_id,
NVL (supplier_desc, supplier_name)
from suppliers;
n This SQL statement would return the supplier_name field if the
supplier_desc contained a null value. Otherwise, it would return the
supplier_desc.
n Example #3:
n select NVL (commission, 0)
from sales;
n This SQL statement would return 0 if the commission field contained
a null value. Otherwise, it would return the commission field.
© Prof. Navneet Goyal, BITS, Pilani
Null Values
n It is possible for tuples to have a null value,
denoted by null, for some of their attributes
n null signifies an unknown value or that a value
does not exist or something that is NA
n The result of any arithmetic expression involving
null is null.
n Aggregate functions simply ignore null values
(as in SQL)
n For duplicate elimination and grouping, null is
treated like any other value, and two nulls are
assumed to be the same (as in SQL)
© Prof. Navneet Goyal, BITS, Pilani
Null Values
n Comparisons with null values return the special truth
value: unknown
n If false was used instead of unknown, then not (A < 5)
would not be equivalent to A >= 5
n Three-valued logic using the truth value unknown:
n OR: (unknown or true) = true,
(unknown or false) = unknown
(unknown or unknown) = unknown
n AND: (true and unknown) = unknown,
(false and unknown) = false,
(unknown and unknown) = unknown
n NOT: (not unknown) = unknown
n In SQL “P is unknown” evaluates to true if predicate P
evaluates to unknown
n Result of select predicate is treated as false if it
evaluates to unknown
© Prof. Navneet Goyal, BITS, Pilani
Problem Solving
n Consider the relational schemas:
Email (IDNO, email_id)
Student (IDNO, Name, Hostel, Room)
The EMAIL relation is maintained by IPC, where as the
Student relation is maintained by SWD. It is required
that the information in the two relations be combined
into a new relation with schema:
Student_email (IDNO, email_id, Name, Hostel, Room)
It is observed that there are some IDNOs in the
Student relation that are not in the Email relation.
Write a Relational Algebra expression to create the new
relation such that no IDNO from the Student relation is
left out.
ρ (Student_email, (student left outer join email))
© Prof. Navneet Goyal, BITS, Pilani
Problem Solving
n The student_master table has the schema:
Student_Master_2004 (SUID, IDNO, NAME)
where SUID is a unique identifier for a student
(for eg. f2004123). After completing one year,
students are either transferred to another
discipline/degree or offered a dual degree. The
following table contains the changed IDNO of
students:
Student_Master_NEW_2004 (SUID, IDNO, NAME)
You need to find out all cases of transfer and dual
degree.
Write a relational algebra expression that will
create & populate the following schema:
Dual_Transfer_2004 (SUID, OLD_IDNO,
NEW_IDNO, NAME)
© Prof. Navneet Goyal, BITS, Pilani
Relational Calculus
n Relational Algebra: More
operational, very useful for
representing execution plans.
n Relational Calculus: Lets users
describe what they want, rather
than how to compute it. (Non-
operational, declarative.)
Q & A
Thank You

More Related Content

What's hot

Dbms Notes Lecture 9 : Specialization, Generalization and Aggregation
Dbms Notes Lecture 9 : Specialization, Generalization and AggregationDbms Notes Lecture 9 : Specialization, Generalization and Aggregation
Dbms Notes Lecture 9 : Specialization, Generalization and AggregationBIT Durg
 
Dbms important questions and answers
Dbms important questions and answersDbms important questions and answers
Dbms important questions and answersLakshmiSarvani6
 
Introduction and architecture of expert system
Introduction  and architecture of expert systemIntroduction  and architecture of expert system
Introduction and architecture of expert systempremdeshmane
 
Syntax-Directed Translation into Three Address Code
Syntax-Directed Translation into Three Address CodeSyntax-Directed Translation into Three Address Code
Syntax-Directed Translation into Three Address Codesanchi29
 
Entity Relationship Diagram part9
Entity Relationship Diagram part9Entity Relationship Diagram part9
Entity Relationship Diagram part9DrMohammed Qassim
 
3D Graphics : Computer Graphics Fundamentals
3D Graphics : Computer Graphics Fundamentals3D Graphics : Computer Graphics Fundamentals
3D Graphics : Computer Graphics FundamentalsMuhammed Afsal Villan
 
Conceptual dependency
Conceptual dependencyConceptual dependency
Conceptual dependencyJismy .K.Jose
 
Hierarchical Object Oriented Design
Hierarchical Object Oriented DesignHierarchical Object Oriented Design
Hierarchical Object Oriented Designsahibsahib
 
2D Transformation
2D Transformation2D Transformation
2D TransformationShahDhruv21
 
Flow oriented modeling
Flow oriented modelingFlow oriented modeling
Flow oriented modelingramyaaswin
 
3.1 tuple relational_calculus
3.1 tuple relational_calculus3.1 tuple relational_calculus
3.1 tuple relational_calculusUtkarsh De
 
Character generation techniques
Character generation techniquesCharacter generation techniques
Character generation techniquesMani Kanth
 
Data Flow Diagram
Data Flow DiagramData Flow Diagram
Data Flow Diagramnethisip13
 
Heuristic Search Techniques {Artificial Intelligence}
Heuristic Search Techniques {Artificial Intelligence}Heuristic Search Techniques {Artificial Intelligence}
Heuristic Search Techniques {Artificial Intelligence}FellowBuddy.com
 
Presentation on Relational Schema (Database)
Presentation on Relational Schema (Database)Presentation on Relational Schema (Database)
Presentation on Relational Schema (Database)Salim Hosen
 
Entity Relationship design issues
Entity Relationship design issuesEntity Relationship design issues
Entity Relationship design issuesMegha Sharma
 

What's hot (20)

Dbms Notes Lecture 9 : Specialization, Generalization and Aggregation
Dbms Notes Lecture 9 : Specialization, Generalization and AggregationDbms Notes Lecture 9 : Specialization, Generalization and Aggregation
Dbms Notes Lecture 9 : Specialization, Generalization and Aggregation
 
Dbms important questions and answers
Dbms important questions and answersDbms important questions and answers
Dbms important questions and answers
 
Introduction and architecture of expert system
Introduction  and architecture of expert systemIntroduction  and architecture of expert system
Introduction and architecture of expert system
 
Syntax-Directed Translation into Three Address Code
Syntax-Directed Translation into Three Address CodeSyntax-Directed Translation into Three Address Code
Syntax-Directed Translation into Three Address Code
 
Entity Relationship Diagram part9
Entity Relationship Diagram part9Entity Relationship Diagram part9
Entity Relationship Diagram part9
 
3D Graphics : Computer Graphics Fundamentals
3D Graphics : Computer Graphics Fundamentals3D Graphics : Computer Graphics Fundamentals
3D Graphics : Computer Graphics Fundamentals
 
Conceptual dependency
Conceptual dependencyConceptual dependency
Conceptual dependency
 
Hierarchical Object Oriented Design
Hierarchical Object Oriented DesignHierarchical Object Oriented Design
Hierarchical Object Oriented Design
 
2D Transformation
2D Transformation2D Transformation
2D Transformation
 
Flow oriented modeling
Flow oriented modelingFlow oriented modeling
Flow oriented modeling
 
Rule Based System
Rule Based SystemRule Based System
Rule Based System
 
data mining
data miningdata mining
data mining
 
3.1 tuple relational_calculus
3.1 tuple relational_calculus3.1 tuple relational_calculus
3.1 tuple relational_calculus
 
Character generation techniques
Character generation techniquesCharacter generation techniques
Character generation techniques
 
Class based modeling
Class based modelingClass based modeling
Class based modeling
 
ER MODEL
ER MODELER MODEL
ER MODEL
 
Data Flow Diagram
Data Flow DiagramData Flow Diagram
Data Flow Diagram
 
Heuristic Search Techniques {Artificial Intelligence}
Heuristic Search Techniques {Artificial Intelligence}Heuristic Search Techniques {Artificial Intelligence}
Heuristic Search Techniques {Artificial Intelligence}
 
Presentation on Relational Schema (Database)
Presentation on Relational Schema (Database)Presentation on Relational Schema (Database)
Presentation on Relational Schema (Database)
 
Entity Relationship design issues
Entity Relationship design issuesEntity Relationship design issues
Entity Relationship design issues
 

Similar to Relational Query Languages 10_13.pdf

From class to architecture
From class to architectureFrom class to architecture
From class to architectureMarcin Hawraniak
 
Designing salesforce solutions for reuse - Josh Dennis
Designing salesforce solutions for reuse - Josh DennisDesigning salesforce solutions for reuse - Josh Dennis
Designing salesforce solutions for reuse - Josh DennisSakthivel Madesh
 
DBT ELT approach for Advanced Analytics.pptx
DBT ELT approach for Advanced Analytics.pptxDBT ELT approach for Advanced Analytics.pptx
DBT ELT approach for Advanced Analytics.pptxHong Ong
 
Domain driven design: a gentle introduction
Domain driven design:  a gentle introductionDomain driven design:  a gentle introduction
Domain driven design: a gentle introductionAsher Sterkin
 
Data Analytics with DBMS
Data Analytics with DBMSData Analytics with DBMS
Data Analytics with DBMSGLC Networks
 
Domain separation training
Domain separation trainingDomain separation training
Domain separation trainingbpatino15
 
Agile Methods and Data Warehousing (2016 update)
Agile Methods and Data Warehousing (2016 update)Agile Methods and Data Warehousing (2016 update)
Agile Methods and Data Warehousing (2016 update)Kent Graziano
 
Approaching ATDD/BDD
Approaching ATDD/BDDApproaching ATDD/BDD
Approaching ATDD/BDDDhaval Dalal
 
Carlo Bonamico, Sonia Pini - So you want to build your (Angular) Component Li...
Carlo Bonamico, Sonia Pini - So you want to build your (Angular) Component Li...Carlo Bonamico, Sonia Pini - So you want to build your (Angular) Component Li...
Carlo Bonamico, Sonia Pini - So you want to build your (Angular) Component Li...Codemotion
 
Build Your Own Angular Component Library
Build Your Own Angular Component LibraryBuild Your Own Angular Component Library
Build Your Own Angular Component LibraryCarlo Bonamico
 
How to Be More Productive With AI Development Assistance
How to Be More Productive With AI Development AssistanceHow to Be More Productive With AI Development Assistance
How to Be More Productive With AI Development AssistanceOutSystems
 
Model Driven Architectures
Model Driven ArchitecturesModel Driven Architectures
Model Driven ArchitecturesLalit Kale
 
Moumita_Sen_CV_EN
Moumita_Sen_CV_ENMoumita_Sen_CV_EN
Moumita_Sen_CV_ENMoumita Sen
 
.NET Fest 2019. Halil Ibrahim Kalkan. Implementing Domain Driven Design
.NET Fest 2019. Halil Ibrahim Kalkan. Implementing Domain Driven Design.NET Fest 2019. Halil Ibrahim Kalkan. Implementing Domain Driven Design
.NET Fest 2019. Halil Ibrahim Kalkan. Implementing Domain Driven DesignNETFest
 
MongoDB World 2019: Near Real-Time Analytical Data Hub with MongoDB
MongoDB World 2019: Near Real-Time Analytical Data Hub with MongoDBMongoDB World 2019: Near Real-Time Analytical Data Hub with MongoDB
MongoDB World 2019: Near Real-Time Analytical Data Hub with MongoDBMongoDB
 
Keeping code clean
Keeping code cleanKeeping code clean
Keeping code cleanBrett Child
 
Logical Data Fabric and Data Mesh – Driving Business Outcomes
Logical Data Fabric and Data Mesh – Driving Business OutcomesLogical Data Fabric and Data Mesh – Driving Business Outcomes
Logical Data Fabric and Data Mesh – Driving Business OutcomesDenodo
 

Similar to Relational Query Languages 10_13.pdf (20)

From class to architecture
From class to architectureFrom class to architecture
From class to architecture
 
Designing salesforce solutions for reuse - Josh Dennis
Designing salesforce solutions for reuse - Josh DennisDesigning salesforce solutions for reuse - Josh Dennis
Designing salesforce solutions for reuse - Josh Dennis
 
DBT ELT approach for Advanced Analytics.pptx
DBT ELT approach for Advanced Analytics.pptxDBT ELT approach for Advanced Analytics.pptx
DBT ELT approach for Advanced Analytics.pptx
 
Domain driven design: a gentle introduction
Domain driven design:  a gentle introductionDomain driven design:  a gentle introduction
Domain driven design: a gentle introduction
 
Data Analytics with DBMS
Data Analytics with DBMSData Analytics with DBMS
Data Analytics with DBMS
 
Domain separation training
Domain separation trainingDomain separation training
Domain separation training
 
Agile Methods and Data Warehousing (2016 update)
Agile Methods and Data Warehousing (2016 update)Agile Methods and Data Warehousing (2016 update)
Agile Methods and Data Warehousing (2016 update)
 
Resume
ResumeResume
Resume
 
Approaching ATDD/BDD
Approaching ATDD/BDDApproaching ATDD/BDD
Approaching ATDD/BDD
 
Carlo Bonamico, Sonia Pini - So you want to build your (Angular) Component Li...
Carlo Bonamico, Sonia Pini - So you want to build your (Angular) Component Li...Carlo Bonamico, Sonia Pini - So you want to build your (Angular) Component Li...
Carlo Bonamico, Sonia Pini - So you want to build your (Angular) Component Li...
 
Build Your Own Angular Component Library
Build Your Own Angular Component LibraryBuild Your Own Angular Component Library
Build Your Own Angular Component Library
 
How to Be More Productive With AI Development Assistance
How to Be More Productive With AI Development AssistanceHow to Be More Productive With AI Development Assistance
How to Be More Productive With AI Development Assistance
 
Model Driven Architectures
Model Driven ArchitecturesModel Driven Architectures
Model Driven Architectures
 
Moumita_Sen_CV_EN
Moumita_Sen_CV_ENMoumita_Sen_CV_EN
Moumita_Sen_CV_EN
 
Not your grandfathers BPM
Not your grandfathers BPMNot your grandfathers BPM
Not your grandfathers BPM
 
.NET Fest 2019. Halil Ibrahim Kalkan. Implementing Domain Driven Design
.NET Fest 2019. Halil Ibrahim Kalkan. Implementing Domain Driven Design.NET Fest 2019. Halil Ibrahim Kalkan. Implementing Domain Driven Design
.NET Fest 2019. Halil Ibrahim Kalkan. Implementing Domain Driven Design
 
HW04.pdf
HW04.pdfHW04.pdf
HW04.pdf
 
MongoDB World 2019: Near Real-Time Analytical Data Hub with MongoDB
MongoDB World 2019: Near Real-Time Analytical Data Hub with MongoDBMongoDB World 2019: Near Real-Time Analytical Data Hub with MongoDB
MongoDB World 2019: Near Real-Time Analytical Data Hub with MongoDB
 
Keeping code clean
Keeping code cleanKeeping code clean
Keeping code clean
 
Logical Data Fabric and Data Mesh – Driving Business Outcomes
Logical Data Fabric and Data Mesh – Driving Business OutcomesLogical Data Fabric and Data Mesh – Driving Business Outcomes
Logical Data Fabric and Data Mesh – Driving Business Outcomes
 

More from ShubhamJain981677

More from ShubhamJain981677 (6)

5 tv advertising.ppt
5 tv advertising.ppt5 tv advertising.ppt
5 tv advertising.ppt
 
9 CSR Final.pptx
9 CSR Final.pptx9 CSR Final.pptx
9 CSR Final.pptx
 
journalism.pptx
journalism.pptxjournalism.pptx
journalism.pptx
 
Chapter 9 Probability and Statistics.pdf
Chapter 9 Probability and Statistics.pdfChapter 9 Probability and Statistics.pdf
Chapter 9 Probability and Statistics.pdf
 
journalism.pptx.pdf
journalism.pptx.pdfjournalism.pptx.pdf
journalism.pptx.pdf
 
Hi I am Ram.pptx
Hi I am Ram.pptxHi I am Ram.pptx
Hi I am Ram.pptx
 

Recently uploaded

Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 

Recently uploaded (20)

Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 

Relational Query Languages 10_13.pdf

  • 1. Relational Query Languages Professor Navneet Goyal Department of Computer Science & Information Systems BITS, Pilani
  • 2. © Prof. Navneet Goyal, BITS, Pilani Relational Query Languages n A major strength of the relational model: supports simple, powerful querying of data. n Queries can be written intuitively, and the DBMS is responsible for efficient evaluation. n The key: precise semantics for relational queries. n Allows the optimizer to extensively re- order operations, and still ensure that the answer does not change.
  • 3. © Prof. Navneet Goyal, BITS, Pilani Relational Query Languages n Query languages: Allow manipulation and retrieval of data from a database. n Relational model supports simple, powerful QLs: n Strong formal foundation based on logic. n Allows for much optimization. n Query Languages != programming languages! n QLs not expected to be “Turing complete”. n QLs not intended to be used for complex calculations. n QLs support easy, efficient access to large data sets.
  • 4. © Prof. Navneet Goyal, BITS, Pilani Formal Relational Query Languages n Two mathematical Query Languages form the basis for “real” languages (e.g. SQL), and for implementation: n Relational Algebra: More operational, very useful for representing execution plans. n Relational Calculus: Lets users describe what they want, rather than how to compute it. (Non-operational, declarative.)
  • 5. © Prof. Navneet Goyal, BITS, Pilani The SQL Query Language n Developed by IBM’s System-R Project in the 1970s n Need for a standard since it is used by many vendors n Standards: n SQL-86 n SQL-89 (minor revision) n SQL-92 (major revision) n SQL-99 (major extensions, current standard) n Find Out what is SQL-MM!!
  • 6. © Prof. Navneet Goyal, BITS, Pilani The SQL Query Language n SQL has been influenced by both Relational Algebra (RA) & Relational Calculus (RC) n More so by RC, particularly Tuple relational Calculus (TRC) n The other variant of RC is Domain Relational Calculus (DRC) which has greatly influenced Query By Example (QBE)
  • 7. © Prof. Navneet Goyal, BITS, Pilani The SQL Query Language n SQL consists of: n DDL (Data Definition Language) • Create conceptual schema n DML (Data Manipulation Language) • Relational operators • Insert, Delete, Update n VDL (View Definition Language) • Specify user views & their mapping to the conceptual schema (in most DBMSs, done by DDL) n SDL (Storage Definition Language) • File organization • Indexes
  • 8. © Prof. Navneet Goyal, BITS, Pilani The SQL Query Language n SDL is being removed from SQL n DMLs n High-level or nonprocedural (declarative) • Can be entered at the SQL > or can be embedded in a general purpose programming language • Can specify & retrieve many records in a single DML statement & are hence called Set-at-a-time DMLs n Low-level or procedural • Must be embedded in a general purpose programming language • Typically retrieves individual records or objects from the DB & processes each separately • Therefore it needs PL constructs like looping • Record-at-a-time DMLs
  • 9. © Prof. Navneet Goyal, BITS, Pilani The SQL Query Language DMLs n Whenever DML statements are embedded in a PL, that language is called as the host language and the DML is called the Data Sublanguage n In object DBs, the host language & data sublanguage form one integrated language – for eg. C++ with some extensions to support database functionality n Some RDBMSs also provide integrated languages – for eg. ORACLE’s PL/SQL.
  • 10. © Prof. Navneet Goyal, BITS, Pilani Operations on Relations n Restrict n Project n Join n Divide n Union n Intersection n Difference n Product Relational Operations Set Operations
  • 11. © Prof. Navneet Goyal, BITS, Pilani Relational Algebra n Collection of operations on relations n 8 operators + Rename operator n Codd’s 8 operators do not form a minimal set n Some of them are not primitive n Join, Intersect, & Divide can be defined in terms of other 5 n None of these 5 can be defined in terms of the remaining 4 n MINIMAL SET n Join, Intersect, & Divide are important & should be supported directly
  • 12. © Prof. Navneet Goyal, BITS, Pilani Relational Algebra n Some common DB requests can not be performed with the RA operations n Additional operations are developed n Aggregate functions & additional types of joins
  • 13. © Prof. Navneet Goyal, BITS, Pilani Relational Algebra IMPORTANCE OF RA n Provides a formal foundation for relational model operations n Plays a pivotal role in query optimization in RDBMSs n Some of its concepts are implemented in SQL n Expressive power of RA is used as a metric of how powerful a relational DB query language is!!
  • 14. © Prof. Navneet Goyal, BITS, Pilani Relational Completeness n A relational DB query language is said to be Relationally Complete, if it can express all queries that we can express in RA n Relational DB query languages are expected to be relationally complete n Commercial query languages like SQL, support features that allow us to express some queries that are not possible in RA
  • 15. © Prof. Navneet Goyal, BITS, Pilani Restrict & Project
  • 16. © Prof. Navneet Goyal, BITS, Pilani Union, Intersection & Difference
  • 17. © Prof. Navneet Goyal, BITS, Pilani Union, Intersection & Difference Union Compatibility: r U s is valid if: n Relations r & s have the same arity n Domains of the ith attribute of r is the same as the domain of the ith attribute of s, ⍱ i. Note that r & s can be either database relations or derived relations
  • 18. © Prof. Navneet Goyal, BITS, Pilani Product & Divide A B C X Y A A B B C C X Y X Y X Y X Z A PRODUCT DIVIDE BY A A A B C X Y Z X Y
  • 19. © Prof. Navneet Goyal, BITS, Pilani Divide DIVIDE P1 P2 P4 S1 S1 S2 S2 S2 S2 S3 S4 S4 P1 P2 P3 P4 P1 P2 P2 P2 P4 S1 S2 S3 S4 Shipment P2 P4 P2 S1 S4 S2 Parts
  • 20. © Prof. Navneet Goyal, BITS, Pilani Join A1 A2 A3 B1 B1 B2 B1 B2 B3 C1 C2 C3 A1 A2 A3 B1 B1 B2 C1 C1 C2 NATURAL JOIN
  • 21. © Prof. Navneet Goyal, BITS, Pilani Closure n A relation is closed under relational and set operators n The result of these operators on relation(s) is another relation n Output from one operation can become input to another n Nested Expressions possible
  • 22. © Prof. Navneet Goyal, BITS, Pilani Example Schema sid sname rating age 22 dustin 7 45.0 31 lubber 8 55.5 58 rusty 10 35.0 sid sname rating age 28 yuppy 9 35.0 31 lubber 8 55.5 44 guppy 5 35.0 58 rusty 10 35.0 sid bid day 22 101 10/10/96 58 103 11/12/96 R1 S1 S2
  • 23. © Prof. Navneet Goyal, BITS, Pilani Projection sname rating yuppy 9 lubber 8 guppy 5 rusty 10 πsname rating S , ( ) 2 age 35.0 55.5 πage S ( ) 2 n Deletes attributes that are not in projection list. n Schema of result contains exactly the fields in the projection list, with the same names that they had in the (only) input relation. n Projection operator has to eliminate duplicates! (Why??) n Note: real systems typically don’t do duplicate elimination unless the user explicitly asks for it. (Why not?)
  • 24. © Prof. Navneet Goyal, BITS, Pilani Selection σrating S >8 2 ( ) sid sname rating age 28 yuppy 9 35.0 58 rusty 10 35.0 sname rating yuppy 9 rusty 10 π σ sname rating rating S , ( ( )) >8 2 n Selects rows that satisfy selection condition. n No duplicates in result! (Why?) n Schema of result identical to schema of (only) input relation. n Result relation can be the input for another relational algebra operation! (Operator composition.)
  • 25. © Prof. Navneet Goyal, BITS, Pilani Union, Intersection, Set-Difference n All of these operations take two input relations, which must be union-compatible: n Same number of fields n `Corresponding’ fields have the same domain/data type sid sname rating age 22 dustin 7 45.0 31 lubber 8 55.5 58 rusty 10 35.0 44 guppy 5 35.0 28 yuppy 9 35.0 sid sname rating age 31 lubber 8 55.5 58 rusty 10 35.0 S S 1 2 ∪ S S 1 2 ∩ sid sname rating age 22 dustin 7 45.0 S S 1 2 −
  • 26. © Prof. Navneet Goyal, BITS, Pilani Cross-Product n Each row of S1 is paired with each row of R1. n Result schema has one field per field of S1 and R1, with field names `inherited’ if possible. n Conflict: Both S1 and R1 have a field called sid. ρ ( ( , ), ) C sid sid S R 1 1 5 2 1 1 → → × (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 § Renaming operator:
  • 27. © Prof. Navneet Goyal, BITS, Pilani Joins n Condition Join: n Result schema same as that of cross- product. n Fewer tuples than cross-product, might be able to compute more efficiently n Sometimes called a theta-join. R c S c R S ! " = × σ ( ) (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 S R S sid R sid 1 1 1 1 ! " . . <
  • 28. © Prof. Navneet Goyal, BITS, Pilani Joins n Equi-Join: A special case of condition join where the condition c contains only equalities n Result schema similar to cross-product, but only one copy of fields for which equality is specified. n Natural Join: Equijoin on all common fields. sid sname rating age bid day 22 dustin 7 45.0 101 10/10/96 58 rusty 10 35.0 103 11/12/96 S R sid 1 1 ! "
  • 29. © Prof. Navneet Goyal, BITS, Pilani Examples of Division A/B sno pno s1 p1 s1 p2 s1 p3 s1 p4 s2 p1 s2 p2 s3 p2 s4 p2 s4 p4 pno p2 pno p2 p4 pno p1 p2 p4 sno s1 s2 s3 s4 sno s1 s4 sno s1 A B1 B2 B3 A/B1 A/B2 A/B3
  • 30. © Prof. Navneet Goyal, BITS, Pilani Find names of sailors who’ve reserved boat #103 n Solution 1: π σ sname bid serves Sailors (( Re ) ) =103 ! " n Solution 2: ) Re , 1 ( 103 serves Temp bid = σ ρ ) 1 , 2 ( Sailors Temp Temp ! " ρ π sname Temp ( ) 2 n Solution 3: )) Re ( 103 ( Sailors serves bid sname ! " = σ π Query Optimizer
  • 31. © Prof. Navneet Goyal, BITS, Pilani Find names of sailors who’ve reserved a red boat n Information about boat color only available in Boats; so need an extra join: π σ sname color red Boats serves Sailors (( ' ' ) Re ) = ! " ! " v A more efficient solution: ) ) Re ) ' ' (( ( Sailors s Boats red color bid sid sname ! " ! " = σ π π π A query optimizer can find this, given the first solution! Query Optimizer
  • 32. © Prof. Navneet Goyal, BITS, Pilani Find sailors who’ve reserved a red or a green boat n Can identify all red or green boats, then find sailors who’ve reserved one of these boats: ρ σ ( ,( ' ' ' ' )) Tempboats color red color green Boats = ∨ = π sname Tempboats serves Sailors ( Re ) ! " ! " v Can also define Tempboats using union! (How?) v What happens if is replaced by in this query? ∨ ∧
  • 33. © Prof. Navneet Goyal, BITS, Pilani Find sailors who’ve reserved a red and a green boat n Previous approach won’t work! Must identify sailors who’ve reserved red boats, sailors who’ve reserved green boats, then find the intersection ρ π σ ( , (( ' ' ) Re )) Tempred sid color red Boats serves = ! " π sname Tempred Tempgreen Sailors (( ) ) ∩ ! " )) Re ) ' ' ( ( , ( serves Boats green color sid Tempgreen ! " = σ π ρ
  • 34. © Prof. Navneet Goyal, BITS, Pilani Find the names of sailors who’ve reserved all boats n Uses division; schemas of the input relations to / must be carefully chosen: )) ( / ) Re , ( , ( Boats bid serves bid sid Tempsids π π ρ π sname Tempsids Sailors ( ) ! " v To find sailors who’ve reserved all ‘Interlake’ boats: / ( ' ' ) π σ bid bname Interlake Boats = .....
  • 35. © Prof. Navneet Goyal, BITS, Pilani Summary n The relational model has rigorously defined query languages that are simple and powerful. n Relational algebra is more operational; useful as internal representation for query evaluation plans. n Several ways of expressing a given query; a query optimizer should choose the most efficient version.
  • 36. © Prof. Navneet Goyal, BITS, Pilani Summary n Role of RA in query optimization
  • 37. © Prof. Navneet Goyal, BITS, Pilani Role of RA in QO n Parsing and translation n Translate the query into its internal form. n Translation is similar to the work performed by the parser of a compiler n Parser checks syntax, verifies relations n Parse tree representation n This is then translated into RA expression
  • 38. © Prof. Navneet Goyal, BITS, Pilani Role of RA in QO n Query Execution Plan n In SQL, a query can be expressed is several ways n Each SQL query can itself be translated into RA expression in many ways n An RA expression only partially tells you how to evaluate a query n Several ways to evaluate RA expression n Annotate RA expression with instructions specifying how to evaluate each operation n Annotation may state the algorithm to be used for a specific operation or the particular index to use n Annotated RAE is called an evaluation primitive n Sequence of primitive operations is a QEP
  • 39. © Prof. Navneet Goyal, BITS, Pilani Role of RA in QO n Example select balance from account where balance < 2500 n RAEs n σbalance<2500(∏balance(account)) n ∏balance(σbalance<2500(account)) n E.g., we can use an index on balance to find accounts with balance < 2500, n or can perform complete relation scan and discard accounts with balance ≥ 2500
  • 40. © Prof. Navneet Goyal, BITS, Pilani Role of RA in QO Query Execution Plan
  • 41. © Prof. Navneet Goyal, BITS, Pilani Role of RA in QO
  • 42. © Prof. Navneet Goyal, BITS, Pilani Role of RA in QO
  • 43. © Prof. Navneet Goyal, BITS, Pilani Summary n What we studied about RA is not used in commercial systems!!! n We assumed a SET model n Commercial systems use a BAG model n What are BAGS? n Why BAGS?
  • 44. © Prof. Navneet Goyal, BITS, Pilani Relational Operations on BAGS n So far we discussed relational operations on relations (sets of tuples) n In commercial systems, a relation is allowed to have duplicate tuples n If a set is allowed to have multiple occurrences of a member, it is called a BAG or MULTISET n Set – A relation without duplicate tuples n Bag – A relation that may or may not have duplicate tuples
  • 45. © Prof. Navneet Goyal, BITS, Pilani WHY BAGS? n Allowing relations to be bags can speed up operations on relations n For Example, allowing the result to be a bag could speed up union of two relations n Duplicate elimination not required
  • 46. © Prof. Navneet Goyal, BITS, Pilani WHY BAGS? n Another Example: Projection of relations n Each projected tuple must be compared with other projected tuples to make sure that each projection tuple appears only once n If bags are accepted as results, we simply need to project each tuple & add it to the result A B C 1 2 5 3 4 6 1 2 7 1 2 8 A B 1 2 3 4 A B 1 2 3 4 1 2 1 2
  • 47. © Prof. Navneet Goyal, BITS, Pilani WHY BAGS? If we are projecting a relation to take an aggregate (say AVG), we could not use the set model A B C 1 2 5 3 4 6 1 2 7 1 2 8 A B 1 2 3 4 A B 1 2 3 4 1 2 1 2 AVG=2 AVG=1.5
  • 48. © Prof. Navneet Goyal, BITS, Pilani Union, Intersection, & Difference of BAGS UNION n Add the no. of occurrences of each tuple n If R is a bag in which tuple t appears n times, & S is a bag in which tuple t appears m times, then in the bag R∪S, t appears n+m times n Either n or m or both could be zero
  • 49. © Prof. Navneet Goyal, BITS, Pilani Union, Intersection, & Difference of BAGS INTERSECTION n If R is a bag in which tuple t appears n times, & S is a bag in which tuple t appears m times, then in the bag R∩S, t appears min(n,m) times
  • 50. © Prof. Navneet Goyal, BITS, Pilani Union, Intersection, & Difference of BAGS DIFFERENCE n If R is a bag in which tuple t appears n times, & S is a bag in which tuple t appears m times, then in the bag R-S, tuple t appears max(0, n-m) times n If t appears in R more times than it appears in S, then in R-S, t appears the no. of times it appears in R, minus the no. of times it appears in S n If n=m, then t does not appear in R-S at all n Occurrences of t in S each cancel one occurrence in R
  • 51. © Prof. Navneet Goyal, BITS, Pilani Union, Intersection, & Difference of BAGS A B 1 2 3 4 1 2 1 2 A B 1 2 3 4 3 4 5 6 R S Write down R∪S, R∩S, R-S, & S-R
  • 52. © Prof. Navneet Goyal, BITS, Pilani Union, Intersection, & Difference of BAGS 2 1 4 3 2 1 4 3 6 5 2 1 4 3 2 1 B A 4 3 2 1 B A R∪S R∩S R-S 2 1 2 1 B A S-R 6 5 4 3 B A
  • 53. © Prof. Navneet Goyal, BITS, Pilani Projection of BAGS A B C 1 2 5 3 4 6 1 2 7 1 2 8 R Write down πA, B (R) A B 1 2 3 4 1 2 1 2 πA, B (R)
  • 54. © Prof. Navneet Goyal, BITS, Pilani Selection on BAGS A B C 1 2 5 3 4 6 1 2 7 1 2 7 R Write down σC>=6 (R) A B C 3 4 6 1 2 7 1 2 7 σC>=6 (R)
  • 55. © Prof. Navneet Goyal, BITS, Pilani Product of BAGS A B 1 2 1 2 R Write down R x S B C 2 3 4 5 4 5 S A R.B S.B C 1 2 2 3 1 2 2 3 1 2 4 5 1 2 4 5 1 2 4 5
  • 56. © Prof. Navneet Goyal, BITS, Pilani Natural Join of BAGS A B 1 2 1 2 B C 2 3 4 5 4 5 R Write down R S S A B C 1 2 3 1 2 3 ! "
  • 57. © Prof. Navneet Goyal, BITS, Pilani Theta-Join of BAGS A B 1 2 1 2 B C 2 3 4 5 4 5 R Write down R S S A R.B S.B C 1 2 4 5 1 2 4 5 1 2 4 5 1 2 4 5 ! " R.B < S.B
  • 58. © Prof. Navneet Goyal, BITS, Pilani Extended Operators of RA n Duplicate-Elimination Operator δ n Aggregation Operators • Sum • Count • Average • Max • Min n Grouping Operator γ n Sorting Operator τ n Extended Projection n Outerjoin Operator
  • 59. © Prof. Navneet Goyal, BITS, Pilani Duplicate-Elimination Operator δ n Converts a bag to a set A B 1 2 3 4 1 2 1 2 R A B 1 2 3 4 δ(R)
  • 60. © Prof. Navneet Goyal, BITS, Pilani Aggregate Functions & Operations n Aggregation function takes a collection of values and returns a single value as a result. avg: average value min: minimum value max: maximum value sum: sum of values count: number of values n Aggregate operation in relational algebra: General Form E is any relational-algebra expression n G1, G2 …, Gn is a list of attributes on which to group (can be empty) n Each Fi is an aggregate function n Each Ai is an attribute name
  • 61. © Prof. Navneet Goyal, BITS, Pilani Aggregate Operation – Example Relation r: A B α α β β α β β β C 7 7 3 10 g sum(c) (r) sum(C ) 27
  • 62. © Prof. Navneet Goyal, BITS, Pilani Aggregate Operation – Example Relation account grouped by branch-name: branch_name g sum(balance) (account) branch_name account_number balance Perryridge Perryridge Brighton Brighton Redwood A-102 A-201 A-217 A-215 A-222 400 900 750 750 700 branch_name sum(balance) Perryridge Brighton Redwood 1300 1500 700
  • 63. © Prof. Navneet Goyal, BITS, Pilani Aggregate Functions (Cont.) n Result of aggregation does not have a name n Can use rename operation to give it a name n For convenience, we permit renaming as part of aggregate operation branch_name g sum(balance)as sum_balance(account)
  • 64. © Prof. Navneet Goyal, BITS, Pilani Generalized Projection n Extends the projection operation by allowing arithmetic functions to be used in the projection list. n E is any relational-algebra expression n Each of F1, F2, …, Fn are are arithmetic expressions involving constants and attributes in the schema of E. n Given relation credit_info(customer_name, limit, credit_balance), find how much more each person can spend: ∏customer_name, limit – credit_balance (credit_info)
  • 65. © Prof. Navneet Goyal, BITS, Pilani Generalized Projection Given relation credit_info(customer_name, limit, credit_balance), find how much more each person can spend: ∏customer_name, (limit – credit_balance) as credit_available (credit_info) 1750 1500 700 400 Cust_name Credit_bal Curry Hayes Jones Smith limit 2000 1500 6000 2000 Cust_name Curry Hayes Jones Smith Credit_available 250 0 5300 1600
  • 66. © Prof. Navneet Goyal, BITS, Pilani Outer Join n An extension of the join operation that avoids loss of information. n Computes the join and then adds tuples form one relation that does not match tuples in the other relation to the result of the join. n Uses null values: n null signifies that the value is unknown or does not exist n All comparisons involving null are (roughly speaking) false by definition. • We shall study precise meaning of comparisons with nulls later
  • 67. © Prof. Navneet Goyal, BITS, Pilani Outer Join – Example Relation loan Relation borrower customer_name loan_number Jones Smith Hayes L-170 L-230 L-155 3000 4000 1700 loan_number amount L-170 L-230 L-260 branch_name Downtown Redwood Perryridge
  • 68. © Prof. Navneet Goyal, BITS, Pilani Outer Join – Example Inner Join loan Borrower loan_number amount L-170 L-230 3000 4000 customer_name Jones Smith branch_name Downtown Redwood Jones Smith null loan_number amount L-170 L-230 L-260 3000 4000 1700 customer_name branch_name Downtown Redwood Perryridge Left Outer Join loan Borrower
  • 69. © Prof. Navneet Goyal, BITS, Pilani Outer Join – Example loan_number amount L-170 L-230 L-155 3000 4000 null customer_name Jones Smith Hayes branch_name Downtown Redwood null loan_number amount L-170 L-230 L-260 L-155 3000 4000 1700 null customer_name Jones Smith null Hayes branch_name Downtown Redwood Perryridge null ■ Full Outer Join loan borrower ■ Right Outer Join loan borrower
  • 70. © Prof. Navneet Goyal, BITS, Pilani Outer Join n Left join, or inner join, select rows common to the participating tables to a join n Selecting elements in a table regardless of whether they are present in the second table n OUTER JOIN is the solution n In Oracle, we will place an "(+)" in the WHERE clause on the other side of the table for which we want to include all the rows.
  • 71. © Prof. Navneet Goyal, BITS, Pilani Outer Join store_name Sales Date Los Angeles $1500 Jan-05-1999 San Diego $250 Jan-07-1999 Los Angeles $300 Jan-08-1999 Boston $700 Jan-08-1999 -­ We  want  to  find  out  the  sales  amount  for  all  of  the  stores -­ If  we  do  a  regular  join,  we  will  not  be  able  to  get  what  we  want  because  we   will          have  missed  "New  York,"  since  it  does  not  appear  in  the   Store_Information table   SELECT  A1.store_name,  SUM(A2.Sales)  SALES   FROM  Geography  A1,  Store_Information  A2   WHERE  A1.store_name  =  A2.store_name  (+)   GROUP  BY  A1.store_name Store_Information Geography store_name    SALES Boston                      $700 New  York                               Los  Angeles    $1800 San  Diego            $250 region_name store_name East Boston East New York West Los Angeles West San Diego
  • 72. © Prof. Navneet Goyal, BITS, Pilani NVL Function n In Oracle/PLSQL, the NVL function lets you substitutes a value when a null value is encountered. n NVL (string1, replace_with ) n string1 is the string to test for a null value. Replace_with is the value returned if string1 is null. n Example #1: n select NVL (supplier_city, 'n/a') from suppliers; n The SQL statement above would return 'n/a' if the supplier_city field contained a null value. Otherwise, it would return the supplier_city value. n Example #2: n select supplier_id, NVL (supplier_desc, supplier_name) from suppliers; n This SQL statement would return the supplier_name field if the supplier_desc contained a null value. Otherwise, it would return the supplier_desc. n Example #3: n select NVL (commission, 0) from sales; n This SQL statement would return 0 if the commission field contained a null value. Otherwise, it would return the commission field.
  • 73. © Prof. Navneet Goyal, BITS, Pilani Null Values n It is possible for tuples to have a null value, denoted by null, for some of their attributes n null signifies an unknown value or that a value does not exist or something that is NA n The result of any arithmetic expression involving null is null. n Aggregate functions simply ignore null values (as in SQL) n For duplicate elimination and grouping, null is treated like any other value, and two nulls are assumed to be the same (as in SQL)
  • 74. © Prof. Navneet Goyal, BITS, Pilani Null Values n Comparisons with null values return the special truth value: unknown n If false was used instead of unknown, then not (A < 5) would not be equivalent to A >= 5 n Three-valued logic using the truth value unknown: n OR: (unknown or true) = true, (unknown or false) = unknown (unknown or unknown) = unknown n AND: (true and unknown) = unknown, (false and unknown) = false, (unknown and unknown) = unknown n NOT: (not unknown) = unknown n In SQL “P is unknown” evaluates to true if predicate P evaluates to unknown n Result of select predicate is treated as false if it evaluates to unknown
  • 75. © Prof. Navneet Goyal, BITS, Pilani Problem Solving n Consider the relational schemas: Email (IDNO, email_id) Student (IDNO, Name, Hostel, Room) The EMAIL relation is maintained by IPC, where as the Student relation is maintained by SWD. It is required that the information in the two relations be combined into a new relation with schema: Student_email (IDNO, email_id, Name, Hostel, Room) It is observed that there are some IDNOs in the Student relation that are not in the Email relation. Write a Relational Algebra expression to create the new relation such that no IDNO from the Student relation is left out. ρ (Student_email, (student left outer join email))
  • 76. © Prof. Navneet Goyal, BITS, Pilani Problem Solving n The student_master table has the schema: Student_Master_2004 (SUID, IDNO, NAME) where SUID is a unique identifier for a student (for eg. f2004123). After completing one year, students are either transferred to another discipline/degree or offered a dual degree. The following table contains the changed IDNO of students: Student_Master_NEW_2004 (SUID, IDNO, NAME) You need to find out all cases of transfer and dual degree. Write a relational algebra expression that will create & populate the following schema: Dual_Transfer_2004 (SUID, OLD_IDNO, NEW_IDNO, NAME)
  • 77. © Prof. Navneet Goyal, BITS, Pilani Relational Calculus n Relational Algebra: More operational, very useful for representing execution plans. n Relational Calculus: Lets users describe what they want, rather than how to compute it. (Non- operational, declarative.)
  • 78. Q & A