You are on page 1of 7

336

- Division
By Riham ELSAADANY



Ques;on: select sid of the students that are


enrolled in all courses.
In order to solve this ques;on, lets rst reason about it as we would do
without a computer, then well try to formulate our reasoning into Rela;onal
Algebra (RA). So our reasoning will be like:
1.

2.

Well look up all course names and cid in Courses table:


336,550,660,770.
Here, cid alone will get us those info (gets all 4 of them).
Well look up which students (sid) are associated with
every instance of cid in Enrollement table:
101, 102 each is associated with all of 336,550,660,770.

Enrollement
cid

sid

semester

336

101

S2015

336

102

S2015

550

101

S2012

550

102

S2013

Dpt

550

103

S2015

336 DB

CS

660

101

S2015

550 OS

CS

660

102

S2015

660 Network CS

770

101

S2015

770 Security

770

102

S2015

Cources
cid

cname

CS

Ques;on: select sid of the students that are


enrolled in all courses (Division in RA).
Now lets translate our reasoning into (RA):
1. To get all cid values: we project on cid colomn:

cid Courses.

2. To get the students (sid) that are associated with


all instances of cid: we project on both colomns in Enrollement table:

cid,sid Enrollement.

3. Then we divide both projec;ons: the division will simplify the values of
the colomns, to get rid of the sid colomn (common eld):

cid,sid Enrollement/cid Courses =sid Enrollement

The result will get us only the sid colomn but with the exact values that
are enrolled in all courses: 101, 102.

Division Concept

Keywords: ALL, Every.


We need a subset of column X (sid) from table E (Enrollement) thats related to the
en;re instances (all) of column Y (cid) from table C (Courses).
We start backwards, start where the keyword All is located:
1.

Get the en;re (all) Y colomn from table C, so we project on that Y column.


2.

yC
Get X (the sid we want) from table E, but to relate the 2 tables C and E: we need to get the
common eld Y (cid) , which will happen to be the foreign key in E table, along with X (sid) :
so project on X,Y.

3.

x,yE

Divide 2./1., so divide both projec;ons of XY/Y = X (Y is simplied, so it doesnt appear in the
result, and X values that have associa;on with all Y is what we end up with).

x,yE/ yC

So Division involves dividing 2 projec;ons, to simplify the common colomn (the


en;re colomn following the keyword all or every, and ending up with the
instances of the result having associa;on with all instances of the commun
colomn. Lets implement what we learnt so far in a real life ques;on, as in exams.
4

Ques;on as in real life situa;ons: select the names of


the students that are enrolled in all courses.
Students
sid sname login

Enrollement
gpa

101 Mike

M44

3.9

102 Suzan

Sxx9

3.8

103 Andy

Addd

3.7

cid

sid

semester

336

101

S2015

336

102

S2015

550

101

S2012

550

102

S2013

Courses
cid cname

Dpt

550

103

S2015

336 DB

CS

660

101

S2015

550 OS

CS

660

102

S2015

660 Network CS

770

101

S2015

770 Security

770

102

S2015

CS

Ques;on: select the names of the students that


are enrolled in all courses.
So now we want the names of those students, not just their
sid:
1. So work backwards as we did to perform the division, so
were done with associa;ng the Courses to the Enrollement
tables: this will end us up with the sid of the students that are
enrolled in all courses: 101, 102. (as we did so far)
2. Then well try to associate those sids in the result to the
student table sids, in order to check the names of the
students related to those sids.
3. So what were trying to do is associate some results in the
form of (1 colomn having 2 values: 101. 102) to a table. In
order to do that, well add those results into another table;
lets call it: Tmp.
4. Associate the table Tmp to Students table.

Final Answer
So well select all the results from the division and put them into Tmp
table: the selec;on here is equivalent to select * without a
condi;on, which gets everything from the result (the result has only
one colomn containing 2 values: 101, 102).

x,yE / yC) )

(Tmp, (

Then well associate that Tmp to Student table: so we join both tables
(remember: join will omit the repe;;on of common colomns, that will
get us all columns from the Students table, corresponding to sid 101
and 102). But we only need sname for those resul;ng students: so we
project on that colomn:

sname (Tmp Students)

Both formulas (wriken on separate lines) represent the answer.


7

You might also like