ra example

advertisement
Relational Algebra
1
Query Languages
 Language in which user requests information from
the database.
 Categories of languages


procedural
non-procedural
 “Pure” languages:



Relational Algebra
Tuple Relational Calculus
Domain Relational Calculus
 Pure languages form underlying basis of query
languages that people use.
2
Relational Algebra
Procedural language
Four basic operators




select
project
union
Intersection
The operators take one or more
relations as inputs and give a new
relation as a result.
Select Operation
 Notation:  p(r)
 p is called the selection predicate
 Defined as:
p(r) = {t | t  r and p(t)}
Where p is a formula in propositional calculus
consisting of terms connected by :  (and), 
(or),  (not)
Each term is one of:
<attribute> op
<attribute> or
<constant>
where op is one of: =, , >, . <. 
 Example of selection:
 branch-name=“Perryridge” (account)
Select Operation – Example
Account
Select Operation – Example
The result is the relation:
Accountnumber
A-101
Branchname
perryridge
balance
400
6
Select Operation – Example
 Balance >“700” (account)
Accountnumber
A-201
Branchname
Brighton
balance
A-217
Brighton
750
900
7
Project Operation
Notation:
A1, A2, …, Ak (r)
where A1, A2 are attribute names and
r is a relation name.
The result is defined as the relation
of k columns obtained by erasing the
columns that are not listed
Duplicate rows removed from result,
since relations are sets
Project Operation
E.g. To eliminate the branch-name
attribute of account
account-number, balance (account)
The result relation is:
Project Operation
Account-number
Balance
A-101
500
A102
400
A-201
900
A-215
700
A-217
750
A-222
750
A-305
350
10
Union Operation
Notation: r  s
Defined as:
r  s = {t | t  r or t  s}
For r  s to be valid.
1. r, s must have the same arity (same
number of attributes)
2. The attribute domains must be
compatible (e.g., 2nd column of r deals
with the same type of values as does the
2nd column of s)
Union Operation
E.g. to find all customers with either an
account or a loan
customer-name (depositor)  customer-name (borrower)
Accou
nt-no.
A-101
A-201
A-217
A-222
A-305
borrower
depositor
Customer
-name
Ali
Mahmood
Ahmid
Linda
Rana
Customer- Loanname
no.
Ali
L-11
Kasim
L-11
Ahmid
L-25
Linda
L-11
Rana
L-34
Union Operation
 customer-name (depositor)
Customername
Ali
Mahmood
Ahmid
Linda
Rana
….
customer-name (borrower)
Customer
-name
Ali
Kasim
Ahmid
Linda
Rana
…..
Union Operation
 The result relation is:
Customername
Ali
Mahmood
Ahmid
Linda
Rana
Kasim
Intersection Operation
Notation: r  s
Defined as:
r  s ={ t | t  r and t  s }
Assume:
 r, s have the same arity
 attributes of r and s are compatible
Note: r  s = r - (r - s)
Intersection Operation
 E.g. to find all customers with an account and a
loan
customer-name (depositor)  customer-name (borrower)
Accou
nt-no.
A-101
A-201
A-217
A-222
A-305
borrower
depositor
Customer
-name
Ali
Mahmood
Ahmid
Linda
Rana
Customer- Loanname
no.
Ali
L-11
Kasim
L-11
Ahmid
L-25
Linda
L-11
Rana
L-34
Intersection Operation
 customer-name (depositor)
Customername
Ali
Mahmood
Ahmid
Linda
Rana
….
customer-name (borrower)
Customer
-name
Ali
Kasim
Ahmid
Linda
Rana
…..
Intersection Operation
The result relation is:
Customer-name
Ali
Ahmid
Linda
Rana
Relational Algebra
Four basic operators




set difference
Cartesian product
rename
assignment
The operators take one or more
relations as inputs and give a new
relation as a result.
Set Difference Operation
Notation r – s
Defined as:
r – s = {t | t  r and t  s}
Set differences must be taken
between compatible relations.
 r and s must have the same arity.
 attribute domains of r and s must be compatible.
Set Difference Operation –
Example
For example, find all customers who have an account
and haven't a loan.
 customer-name (depositor) - customer-name (borrower)
Accou
nt-no.
A-101
A-201
A-217
A-222
A-305
borrower
depositor
Customer
-name
Ali
Mahmood
Ahmid
Linda
Rana
Customer- Loanname
no.
Ali
L-11
Kasim
L-11
Ahmid
L-25
Linda
L-11
21
Rana
L-34
Set Difference Operation –
Example
 customer-name (depositor)
Customername
Ali
Mahmood
Ahmid
Linda
Rana
….
customer-name (borrower)
Customer
-name
Ali
Kasim
Ahmid
Linda
Rana
…..
Set Difference Operation –
Example
 The result relation is:
Customername
Mahmood
Set Difference Operation –
Example
For example, find all customers who have
a loan, but they haven't an account.
 customer-name (borrower) - customer-name (depositor)
depositor
borrower
Customer- Loanname
no.
Ali
L-11
Kasim
L-11
Ahmid
L-25
Linda
L-11
Rana
L-34
Customer- Accountname
no.
Ali
A-101
Mahmood A-201
Ahmid
A-217
Linda
A-222
Rana
A-305
24
Set Difference Operation –
Example
 customer-name (borrower)
Customer
-name
Ali
Kasim
Ahmid
Linda
Rana
…..
customer-name (depositor)
Customername
Ali
Mahmood
Ahmid
Linda
Rana
….
Set Difference Operation –
Example
 The result relation is:
Customername
Kasim
Cartesian-Product Operation
Notation r x s
Defined as:
r x s = {t q | t  r and q  s}
Assume that attributes of r(R) and
s(S) are disjoint. (That is,
R  S = ).
If attributes of r(R) and s(S) are not
disjoint, then renaming must be
used.
Cartesian-Product Operation
Relations r and s :
r
A
B
a1 1
a2 2
s
C
D
E
c1
d1
1
c2
d2
2
c3
d3
3
c4
d4
2
Cartesian-Product Operation
The relation r x s is:
A
a1
a1
a1
a1
a2
a2
a2
a2
B
1
1
1
1
2
2
2
2
C
c1
c2
c3
c4
c1
c2
c3
c4
D
d1
d2
d3
d4
d1
d2
d3
d4
E
1
2
3
2
1
2
3
2
29
Composition of Operations
Can build expressions using multiple
operations
Example: B=E(r x s)
A
B
C
D
E
a1
a2
a2
1
2
2
c1
c2
c4
d1
d2
d4
1
2
2
Rename Operation
 Allows us to name, and therefore to refer to, the results
of relational-algebra expressions.
 Allows us to refer to a relation by more than one name.
Example:
 x (E)
returns the expression E under the name X
If a relational-algebra expression E has arity n, then
x (A1, A2, …, An) (E)
returns the result of expression E under the name X, and
with the attributes renamed to A1, A2, …., An.
Rename Operation
R d (account)
d
Assignment Operation
The assignment operation ()
provides a convenient way to express
complex queries.
 Write query as a sequential program consisting of
• a series of assignments.
• followed by an expression whose value is displayed as a
result of the query.
 Assignment must always be made to a temporary relation
variable.
Assignment Operation
 Example: Write
 customer-name (borrower)-customer-name (depositor)
temp1  customer-name (borrower)
temp2  customer-name (depositor)
result = temp1 – temp2
 The result to the right of the  is assigned to the
relation variable on the left of the .
 May use variable in subsequent expressions.
Relational Algebra
A basic expression in the
relational algebra consists of
either one of the following:
 A relation in the database
 A constant relation
Relational Algebra
Let E1 and E2 be relational-algebra
expressions; the following are
relational-algebra expressions:
 E1  E2
 E1  E2
 E1 - E2
 E1 x E2
 p (E1), P is a predicate on attributes in E1
 s(E1), S is a list consisting of some of the attributes in E1
  x (E1), x is the new name for the result of E1
 The assignment operation ()
Relational Schema for the
Banking Enterprise
 branch (branch-name, branch-city, assets)
 customer (customer-name, customer-street,
customer-only)
 account (account-number, branch-name,
balance)
 loan (loan-number, branch-name, amount)
 depositor (customer-name, account-number)
 borrower (customer-name, loan-number)
Example Queries
Find all loans of over $1200.
amount > 1200 (loan)
Find the loan number for each loan of an amount greater than
$1200.
loan-number (amount > 1200 (loan))
Example Queries
 Find the names of all customers who have a loan, an
account, or both, from the bank.
customer-name (borrower)  customer-name (depositor)
Find the names of all customers who have a loan
and an account at bank.
customer-name (borrower)  customer-name (depositor)
Example Queries
 Find the names of all customers who have a loan at the
Perryridge branch.
customer-name (branch-name=“Perryridge”
(borrower.loan-number = loan.loan-number(borrower x loan)))
Find the names of all customers who have a loan at the
Perryridge branch but do not have an account at any
branch of the bank.

customer-name (branch-name = “Perryridge”
(borrower.loan-number = loan.loan-number(borrower x loan))) –
customer-name(depositor)
Example Queries
 Find the names of all customers who have a loan at the
Perryridge branch.
Query 1
customer-name(branch-name = “Perryridge” (
borrower.loan-number = loan.loan-number(borrower x loan)))
 Query 2
customer-name(loan.loan-number = borrower.loan-number(
(branch-name = “Perryridge”(loan)) x borrower))
Example Queries
Find the largest account balance
 Rename account relation as d
 The query is:
balance(account) - account.balance
(account.balance < d.balance (account x d (account)))
Relational Algebra
Two basic operators
 Division
 Natural-Join Operation
The operators take two or more
relations as inputs and give a new
relation as a result.
Division Operation
rs
 Suited to queries that include the phrase “for
all”.
 Let r and s be relations on schemas R and S
respectively where
 R = (A1, …, Am, B1, …, Bn)
 S = (B1, …, Bn)
The result of r  s is a relation on schema
R – S = (A1, …, Am)
r  s = { t | t   R-S(r)   u  s ( tu  r ) }
Division Operation – Example
Relations r, s:
r  s:
A


A
B
B











1
2
3
1
1
1
3
4
6
1
2
1
r
2
s
Another Division Example
Relations r, s:
A
B
C
D
E
D
E








a
a
a
a
a
a
a
a








a
a
b
a
b
a
b
b
1
1
1
1
3
1
1
1
a
b
1
1
r
r  s:
A
B
C


a
a


s
Natural-Join Operation

Notation: r
s
Let r and s be relations on schemas R and
S respectively.
Then, r
s is a relation on schema R 
S obtained as follows:
 Consider each pair of tuples tr from r and ts from s.
 If tr and ts have the same value on each of the attributes in R  S, add
a tuple t to the result, where
• t has the same value as tr on r
• t has the same value as ts on s
Natural-Join Operation

Notation: r
s
Example:
R = (A, B, C, D)
S = (E, B, D)
 Result schema = (A, B, C, D, E)
 r
s is defined as:
r.A, r.B, r.C, r.D, s.E (r.B = s.B  r.D = s.D (r x s))
Natural Join Operation –
Example
 Relations r, s:
A
B
C
D
B
D
E





1
2
4
1
2





a
a
b
a
b
1
3
1
2
3
a
a
a
b
b





r
r
s
s
A
B
C
D
E





1
1
1
1
2





a
a
a
a
b





Outer Join
An extension of the join operation that
avoids loss of information.
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.
Uses null values:
 null signifies that the value is unknown or does not exist
 All comparisons involving null are (roughly speaking) false by
definition.
• Will study precise meaning of comparisons with nulls later
Outer Join – Example
 Relation loan
loan-number
branch-name
L-170
L-230
L-260
Downtown
Redwood
Perryridge
amount
3000
4000
1700
 Relation borrower
customer-name loan-number
Jones
Smith
Hayes
L-170
L-230
L-155
Outer Join – Example
Inner Join
loan
Borrower
loan-number
L-170
L-230
branch-name
Downtown
Redwood
amount
customer-name
3000
4000
Jones
Smith
amount
customer-name
 Left Outer Join
loan
Borrower
loan-number
L-170
L-230
L-260
branch-name
Downtown
Redwood
Perryridge
3000
4000
1700
Jones
Smith
null
Outer Join – Example
 Right Outer Join
loan
loan-number
L-170
L-230
L-155

borrower
branch-name
Downtown
Redwood
null
amount
3000
4000
null
customer-name
Jones
Smith
Hayes
Full Outer Join
Loan
borrower
loan-number
L-170
L-230
L-260
L-155
branch-name
Downtown
Redwood
Perryridge
null
amount
3000
4000
1700
null
customer-name
Jones
Smith
null
Hayes
Example Queries (1)
 Find all customers who have an account from at least
the “Downtown” and the Uptown” branches.
Query 1
CN(BN=“Downtown”(depositor
account)) 
CN(BN=“Uptown”(depositor
account))
where CN denotes customer-name and BN denotes
branch-name.
Example Queries (2)
 Find all customers who have an account from at least
the “Downtown” and the Uptown” branches.
Query 2
customer-name, branch-name (depositor account)
 temp(branch-name) ({(“Downtown”), (“Uptown”)})
Example Queries (3)
 Find all customers who have an account at all
branches located in Brooklyn city.
customer-name, branch-name (depositor account)
 branch-name (branch-city = “Brooklyn” (branch))
Relational Algebra
Four basic operators


Generalized Projection
Aggregate Functions
The operators take one or more
relations as inputs and give a new
relation as a result.
Generalized Projection
Extends the projection operation by
allowing arithmetic functions to be used
in the projection list.
 F1, F2, …, Fn(E)
E is any relational-algebra expression
Each of F1, F2, …, Fn are are arithmetic
expressions involving constants and
attributes in the schema of E.
Generalized Projection
Given relation credit-info(customer-
name, limit, credit-balance).
find how much more each person can
spend:
customer-name, limit – credit-balance (credit-
info)
Generalized Projection
Credit-info
customername
Limit
creditbalance
Ali
1500
380
Ahmed
2000
1400
Rana
1000
500
Kasim
3500
1400
Generalized Projection
The result relation is:
customername
Ali
Limitcreditbalance
1220
Ahmed
600
Rana
500
Kasim
2100
Aggregate Functions and
Operations
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
Aggregate operation in relational
algebra
Aggregate Functions and
Operations
G1, G2, …, Gn




g F1( A1), F2( A2),…, Fn( An) (E)
E is any relational-algebra expression
G1, G2 …, Gn is a list of attributes on which to group (can be empty)
Each Fi is an aggregate function
Each Ai is an attribute name
Aggregate Operation –
Example
 Relation r:
g sum(c) (r)
A
B
C








7
sum-C
27
7
3
10
Aggregate Operation –
Example
account
branch-name account-number
Perryridge
Perryridge
Brighton
Brighton
Redwood
branch-name
g
balance
A-102
A-201
A-217
A-215
A-222
sum(balance)
400
900
750
750
700
(account)
branch-name
Perryridge
Brighton
Redwood
balance
1300
1500
700
Aggregate Functions (Cont.)
Result of aggregation does not have a
name
branch-name g sum(balance) as sum-balance (account)
 Can use rename operation to give it a name
 For convenience, we permit renaming as part of aggregate
operation
Branch-name
Sum-balance
Perryridge
1300
Brighton
1500
Redwood
700
Aggregate Functions (Cont.)
 A type of request that cannot be expressed in the basic relational
algebra is to specify mathematical aggregate functions on
collections of values from the database.
 Examples of such functions include retrieving the average or total
salary of all employees or the total number of employee tuples.
These functions are used in simple statistical queries that
summarize information from the database tuples.
 Common functions applied to collections of numeric values include
SUM, AVERAGE, MAXIMUM, and MINIMUM. The COUNT function
is used for counting tuples or values.
Aggregate Functions (Cont.)
Relational Algebra
A basic expression in the
relational algebra consists of
either one of the following:
 A relation in the database
 A constant relation
Relational Instances for the
Purchasing System
The Supplier relation:
S-number
S-name
S-city
S100
Ahmed
Amman
S200
Ali
Jarash
S300
Kasim
Irbid
S400
Jasim
Aquaba
S500
Rana
Amman
The Part relation:
P-number P-name Color
Price
P-city
P1
TV
Silver
300
Amman
P2
Camera
Black
100
Jarash
P3
Video
Black
200
Amman
P4
PC
Silver
400
Irbid
P5
Printer
Red
100
Irbid
P6
Scanner
silver
150
Jarash
71
The shipment relation:
S-number
S100
S100
S100
P-number
P1
P2
P3
Quantity
100
150
200
S100
S100
S100
P4
P5
P6
160
50
70
S200
S200
P1
P2
200
150
S300
P2
400
S400
S400
S500
P2
P4
P4
150
80
100
72
Example Queries
 Q1- Find The cities for all suppliers .
 S-city (Supplier)
Example Queries
 Q1- Find The cities for all suppliers .
 S-city (Supplier)
The result relation
S-city
Amman
Jarash
Irbid
Aquaba
Example Queries
Q2- Find city for Ahmed.
Example Queries
Q2- Find city for Ahmed.
Temp1   S-name = ‘Ahmed’ (Supplier)
Example Queries
Q2- Find city for Ahmed.
Temp1   S-name = ‘Ahmed’ (Supplier)
S-number
Temp1
S100
S-name
Ahmed
S-city
Amman
Example Queries
Q2- Find city for Ahmed.
Temp1   S-name = ‘Ahmed’ (Supplier)
S-number
Temp1
S100
Result   S-city (Temp1)
Result
S-city
Amman
S-name
Ahmed
S-city
Amman
Example Queries
 Q3- Find Supplier number and name for suppliers in
Amman.
Example Queries
 Q3- Find Supplier number and name for suppliers in
Amman.
Temp1   S-city= ‘Amman’ (Supplier)
Example Queries
 Q3- Find Supplier number and name for suppliers in
Amman.
Temp1   S-city= ‘Amman’ (Supplier)
Temp1
S-number S-name S-city
S100
Ahmed
Amman
S500
Rana
Amman
Example Queries
 Q3- Find Supplier number and name for suppliers in
Amman.
Temp1   S-city= ‘Amman’ (Supplier)
Temp1
S-number S-name S-city
S100
Ahmed
Amman
S500
Rana
Amman
Result   S-number, S-name (Temp1)
Example Queries
 Q3- Find Supplier number and name for suppliers in
Amman.
Temp1   S-city= ‘Amman’ (Supplier)
Temp1
S-number S-name S-city
S100
Ahmed
Amman
S500
Rana
Amman
Result   S-number, S-name (Temp1)
Result
S-number
S-name
S100
Ahmed
S500
Rana
Example Queries
 Q4- For each part supplied, find the part names and
the names of all cities storing these parts.
Example Queries
 Q4- For each part supplied, find the part names and
the names of all cities storing these parts.
Temp1   P-number ( Shipment )
Example Queries
 Q4- For each part supplied, find the part names and
the names of all cities storing these parts.
Temp1   P-number ( Shipment )
Temp1
P-number
P1
P2
P3
P4
P5
P6
Example Queries
 Q4- For each part supplied, find the part names and
the names of all cities storing these parts.
Temp1   P-number ( Shipment )
Temp2  Part
Temp1
Example Queries
 Q4- For each part supplied, find the part names and
the names of all cities storing these parts.
Temp1   P-number ( Shipment )
Temp2  Part
Temp1
Temp2
P-number P-name Color
P1
TV
Silver
Price
300
P-city
Amman
P2
Camera
Black
100
Jarash
P3
Video
Black
200
Amman
P4
PC
Silver
400
Irbid
P5
Printer
Red
100
Irbid
P6
Scanner silver
150
Jarash
Example Queries
 Q4- For each part supplied, find the part names and
the names of all cities storing these parts.
Temp1   P-number ( Shipment )
Temp2  Part
Temp1
Result   P-name, p-city(Temp2)
P-name
Result
P-city
TV
Amman
Camera
Jarash
Video
Amman
PC
Irbid
Printer
Irbid
Scanner
Jarash
Example Queries
 Q5- For each part supplied, find the part numbers and
names of all cities supplying the parts.
Example Queries
 Q5- For each part supplied, find the part numbers and
names of all cities supplying the parts.
Supplier
Temp1  Shipment
Example Queries
 Q5- For each part supplied, find the part numbers and
names of all cities supplying the parts.
Supplier
Temp1  Shipment
S-number P-number Quantity S-name S-city
Temp1
S100
P1
100
Ahmed
Amman
S100
P2
150
Ahmed
Amman
S100
P3
200
Ahmed
Amman
S100
P4
160
Ahmed
Amman
S100
P5
50
Ahmed
Amman
S100
P6
70
Ahmed
Amman
S200
P1
200
Ali
Jarash
S200
P2
150
Ali
Jarash
S300
P2
400
Kasim
Irbid
S400
P2
150
Jasim
Aquaba
S400
P4
80
Jasim
Aquaba
S500
P4
100
Rana
Amman
 Q5- For each part supplied, find the part numbers and
names of all cities supplying the parts.
Example Queries
Temp1  Shipment
Supplier
Result   P-number, S-city ( Temp1 )
Result
P-number S-city
P1
Amman
P2
Amman
P3
Amman
P4
Amman
P5
Amman
P6
Amman
P1
Jarash
P2
Jarash
P2
Irbid
P2
Aquaba
P4
Aquaba
P4
Amman
Example Queries
 Q6- Get supplier numbers for suppliers who supply
part p2
Example Queries
 Q6- Get supplier numbers for suppliers who supply
part p2
Temp1   p-number = ‘p2’ (Shipment)
Example Queries
 Q6- Get supplier numbers for suppliers who supply
part p2
Temp1   p-number = ‘p2’ (Shipment)
Temp1
S-number
P-number
Quantity
S100
S200
P2
P2
150
150
S300
S400
P2
P2
400
150
Example Queries
 Q6- Get supplier numbers for suppliers who supply
part p2
Temp1   P-number = ‘p2’ (Shipment)
Result   S-number ( Temp1 )
Result
S-number
S100
S200
S300
S400
Relational Algebra
A basic expression in the
relational algebra consists of
either one of the following:
 A relation in the database
 A constant relation
Relational Instances for the
Purchasing System
The Supplier relation:
S-number
S-name
S-city
S100
Ahmed
Amman
S200
Ali
Jarash
S300
Kasim
Irbid
S400
Jasim
Aquaba
S500
Rana
Amman
The Part relation:
P-number P-name Color
Price
P-city
P1
TV
Silver
300
Amman
P2
Camera
Black
100
Jarash
P3
Video
Black
200
Amman
P4
PC
Silver
400
Irbid
P5
Printer
Red
100
Irbid
P6
Scanner
silver
150
Jarash
100
The shipment relation:
S-number
S100
S100
S100
P-number
P1
P2
P3
Quantity
100
150
200
S100
S100
S100
P4
P5
P6
160
50
70
S200
S200
P1
P2
200
150
S300
P2
400
S400
S400
S500
P2
P4
P4
150
80
100
101
Example Queries
 Q1- Get supplier names for suppliers who supplying at
least one silver part.
Example Queries
 Q1- Get supplier names for suppliers who supplying at
least one silver part.
Temp1   P-color = ‘silver’ (Part)
Example Queries
 Q1- Get supplier names for suppliers who supplying at
least one silver part.
Temp1   P-color = ‘silver’ (Part)
Temp1
P-number
P-name Color
Price P-city
P1
TV
Silver
300
Amman
P4
PC
Silver
400
Irbid
P6
Scanner silver
150
Jarash
Example Queries
 Q1- Get supplier names for suppliers who supplying at
least one silver part.
Temp1   P-color = ‘silver’ (Part)
Temp2  Shipment
Temp1
Example Queries
 Q1- Get supplier names for suppliers who supplying at
least one silver part.
Temp1   P-color = ‘silver’ (Part)
Temp2  Shipment
Temp1
Temp2
S-number
P-number
Quantity
P-name
Color
Price
P-city
S100
P1
100
TV
Silver
300
Amman
S100
P4
160
PC
Silver
400
Irbid
S100
P6
70
Scanner
silver
150
Jarash
S200
P1
200
TV
Silver
300
Amman
S400
P4
80
PC
Silver
400
Irbid
S500
P4
100
PC
Silver
400
Irbid
Example Queries
 Q1- Get supplier names for suppliers who supplying at
least one silver part.
Temp1   P-color = ‘silver’ (Part)
Temp2  Shipment
Temp1
Temp3   S-number (Temp2)
S-number
Temp3
S100
S200
S400
S500
Example Queries
 Q1- Get supplier names for suppliers who supplying at
least one silver part.
Temp1   P-color = ‘silver’ (Part)
Temp2  Shipment
Temp1
Temp3   S-number (Temp2)
Temp4  Supplier
Temp4
Temp3
S-number
S-name
S-city
S100
Ahmed
Amman
S200
Ali
Jarash
S400
Jasim
Aquaba
S500
Rana
Amman
Example Queries
 Q1- Get supplier names for suppliers who supplying at
least one silver part.
Temp1   P-color = ‘silver’ (Part)
Temp2  Shipment
Temp1
Temp3   S-number (Temp2)
Temp4  Supplier
Temp3
Result   S-name (Temp4)
S-name
Ahmed
Result
Ali
Jasim
Rana
Example Queries
 Q2- Get supplier names and cities who supplying parts
stored in Irbid.
Example Queries
 Q2- Get supplier names and cities who supplying parts
stored in Irbid.
Temp1   P-city = ‘Irbid’ (Part)
Example Queries
 Q2- Get supplier names and cities who supplying parts
stored in Irbid.
Temp1   P-city = ‘Irbid’ (Part)
Temp1
P-number P-name
Color
Price
P-city
P4
PC
Silver
400
Irbid
P5
Printer
Red
100
Irbid
Example Queries
 Q2- Get supplier names and cities who supplying parts
stored in Irbid.
Temp1   P-city = ‘Irbid’ (Part)
Temp2  Shipment
Temp1
Temp2
S-number P-number
Quantity P-name
Color Price
P-city
S100
P4
160
PC
Silver
400
Irbid
S100
P5
50
Printer
Red
100
Irbid
S400
P4
80
PC
Silver
400
Irbid
S500
P4
100
PC
Silver
400
Irbid
Example Queries
 Q2- Get supplier names and cities who supplying parts
stored in Irbid.
Temp1   P-city = ‘Irbid’ (Part)
Temp2  Shipment
Temp1
Temp3   S-number (Temp2)
S-number
Temp3
S100
S400
S500
Example Queries
 Q2- Get supplier names and cities who supplying parts
stored in Irbid.
Temp1   P-city = ‘Irbid’ (Part)
Temp2  Shipment
Temp1
Temp3   S-number (Temp2)
Temp4  Supplier
Temp3
Example Queries
 Q2- Get supplier names and cities who supplying parts
stored in Irbid.
Temp1   P-city = ‘Irbid’ (Part)
Temp2  Shipment
Temp1
Temp3   S-number (Temp2)
Temp4  Supplier
Temp4
Temp3
S-number
S-name
S-city
S100
Ahmed
Amman
S400
Jasim
Aquaba
S500
Rana
Amman
Example Queries
 Q2- Get supplier names and cities who supplying parts
stored in Irbid.
Temp1   P-city = ‘Irbid’ (Part)
Temp2  Shipment
Temp1
Temp3   S-number (Temp2)
Temp4  Supplier
Temp3
Result   S-name , S-city (Temp4)
Example Queries
 Q2- Get supplier names and cities who supplying parts
stored in Irbid.
Temp1   P-city = ‘Irbid’ (Part)
Temp2  Shipment
Temp1
Temp3   S-number (Temp2)
Temp4  Supplier
Temp3
Result   S-name , S-city (Temp4)
Result
S-name
S-city
Ahmed
Amman
Jasim
Aquaba
Rana
Amman
Example Queries
 Q3- Find supplier names for suppliers who supply all
parts.
Example Queries
 Q3- Find supplier names for suppliers who supply all
parts.
Temp1   P-number (Part)
Example Queries
 Q3- Find supplier names for suppliers who supply all
parts.
Temp1   P-number (Part)
P-number
P1
P2
Temp1
P3
P4
P5
P6
Example Queries
 Q3- Find supplier names for suppliers who supply all
parts.
Temp1   P-number (Part)
Temp2   S-number , P-number (Shipment)
Example Queries
 Q3- Find supplier names for suppliers who supply all
parts.
Temp1   P-number (Part)
Temp2   S-number , P-number (Shipment)
Temp2
S-number
P-number
S100
P1
S100
P2
S100
P3
S100
P4
S100
P5
S100
P6
S200
P1
S200
P2
S300
P2
S400
P2
S400
P4
S500
P4
Example Queries
 Q3- Find supplier names for suppliers who supply all
parts.
Temp1   P-number (Part)
Temp2   S-number , P-number (Shipment)
Temp3  Temp2  Temp1
Example Queries
 Q3- Find supplier names for suppliers who supply all
parts.
Temp1   P-number (Part)
Temp2   S-number , P-number (Shipment)
Temp3  Temp2  Temp1
S-number
Temp3
S100
Example Queries
 Q3- Find supplier names for suppliers who supply all
parts.
Temp1   P-number (Part)
Temp2   S-number , P-number (Shipment)
Temp3  Temp2  Temp1
Temp3
Temp4  Supplier
Example Queries
 Q3- Find supplier names for suppliers who supply all
parts.
Temp1   P-number (Part)
Temp2   S-number , P-number (Shipment)
Temp3  Temp2  Temp1
Temp3
Temp4  Supplier
Temp4
S-number
S-name
S-city
S100
Ahmed
Amman
Example Queries
 Q3- Find supplier names for suppliers who supply all
parts.
Temp1   P-number (Part)
Temp2   S-number , P-number (Shipment)
Temp3  Temp2  Temp1
Temp3
Temp4  Supplier
Result   S-name (Temp4)
Example Queries
 Q3- Find supplier names for suppliers who supply all
parts.
Temp1   P-number (Part)
Temp2   S-number , P-number (Shipment)
Temp3  Temp2  Temp1
Temp3
Temp4  Supplier
Result   S-name (Temp4)
S-name
Result
Ahmed
Example Queries
 Q4- How many parts they have?
Example Queries
 Q4- How many parts they have?
g count (P-number) (Part)
P-number
6
Example Queries
 Q5- Find the total quantities supplied by each supplier.
Example Queries
 Q6- Find the total quantities supplied by each supplier.
S-number g sum (Quantity) (Shipment)
Example Queries
 Q6- Find the total quantities supplied by each supplier.
S-number g sum (Quantity) (Shipment)
S-number
Quantity
S100
730
S200
350
S300
400
S400
250
S500
100
Example Queries
 Q6- Find the total quantities supplied by each supplier.
S-number g sum (Quantity) as ( sum-quantity) (Shipment)
S-number
Sum-Quantity
S100
730
S200
350
S300
400
S400
250
S500
100
END
136
Download