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?
Relational algebra for:
Actually you need division
%
operator in relational algebra: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:
Primary key is:
TrainingCode
:A pair of employeeID and trainingCode: a employee take the training.
Apply % Division operation which gives you desired employee's codes with trainingCode then apply projection to filter out employee code only.
"Fundamentals of Database Systems" is the book I always keep in my hand.
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: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.