Reputation:
I am working on SQL and Relational Algebra these days. And I am stuck on the below questions. I am able to make a SQL for the below questions but somehow my Relational Algebra that I have made doesn't looks right.
Below are my tables-
Employee (
EmployeeId
, EmployeeName, EmployeeCountry)
Training (
TrainingCode
, TrainingName, TrainingType, TrainingInstructor)
Outcome
(
EmployeeId, TrainingCode
, Grade)
All the keys are specified with star *.
Below is the question and its SQL query as well which works fine-
Find an Id of the Employee who has taken every training.
SQL Qyery:
SELECT X.EmployeeID
FROM (SELECT EmployeeID, COUNT(*) AS NumClassesTaken
FROM OutCome GROUP BY EmployeeID )
AS X
JOIN (SELECT COUNT(*) AS ClassesAvailable
FROM Training)
AS Y
ON X.NumClassesTaken = Y.ClassesAvailable
I am not able to understand what will be the relational algebra for the above query? Can anyone help me with that?
Upvotes: 3
Views: 5956
Reputation: 58271
Relational algebra for:
Find an
Id
of theEmployee
who has taken everytraining
.
Actually you need division %
operator in relational algebra:
r ÷ s
is used when we wish to express queries with“all”
:Example:
- Which persons have a bank account at
ALL
the banks in the country?- Retrieve the name of employees who work on
ALL
the projects thatJon Smith
works on?
Read also this slid for division operator:
You also need query % operator for your query: "Employee who has taken all training".
First list off all Training codes:
Training (
TrainingCode
, TrainingName, TrainingType, TrainingInstructor)
Primary key is: TrainingCode
:
TC =
∏TrainingCode
(Training)
A pair of employeeID and trainingCode: a employee take the training.
ET =
∏EmployeeId, TrainingCode
(Outcome)
Apply % Division operation which gives you desired employee's codes with trainingCode then apply projection to filter out employee code only.
Result =
∏EmployeeId
(ET % TC)
"Fundamentals of Database Systems" is the book I always keep in my hand.
6.3.4 The DIVISION Operation
The DIVISION operation is defined for convenience for dealing with queries that involves
universal quantification
or theall
condition. Most RDBMS implementation with SQL as the primary query language do not directly implement division. SQL has round way of dealing with the type of query using EXISTS, CONTAINS and NOT EXISTS key words.The general DIVISION operation applied to two relations
T(Y) = R(Z) % S(X)
, whereX ⊆ Z
andY = Z - X
(and henceZ = X ∪ Y
); that isY
is the set of attributes ofR
that are not attributes ofS
e.g.X = {A}, Z = {A, B} then Y = {B}
,B
attribute is not present in relationS
.
T(Y)
the result of DIVISION is a relation includes a tuplet
if tupletR
appear in relationR
withtR[Y] = t
, and withtR[X] = tS
forevery
tuple inS
. This means that. for a tuplet
to appear in the resultT
of the DIVISION, the value oft
must be appear inR
in combination with every tuple inS
.
I would also like to add that the set of relational algebra operations {
σ
,
∏
,
⋈
,
Χ
,
-
}
namely Selection, Projection, Join, Cartesian Cross and Minus is a complete set; that is any of the other original relational algebra operation can be expressed as a sequence of operations from this set. Division operation %
can also be expressed in the form of ∏
, ⋈
, and -
operations as follows:
T1 <-- ∏Y(R)
T2 <-- ∏Y((S Χ T1) - R)
T3 <-- T1 - T2
To represent your question using basic relational algebraic operation just replace R by Outcome, S by Training and attribute set Y by EmployeeId.
I hope this help.
Upvotes: 4