Slideshow:
|
SELECT DISTINCT m1.movieTitle, m1.movieYear FROM StarsIn m1 WHERE m1.movieYear - 40 ≤ (SELECT avg(s.birthdate) FROM StarsIn m2, MovieStar s WHERE m2.StarName = s.name AND m2.movieTitle = m1.movieTitle AND m2.movieYear = m1.movieYear) |
The sub-query is correlated because we connot execute the sub-query without the outer query:
SELECT avg(s.birthdate) FROM StarsIn m2, MovieStar s WHERE m2.StarName = s.name AND m2.movieTitle = m1.movieTitle AND m2.movieYear = m1.movieYear |
That is because the values of the attributes m1.movieTitle and m1.movieYear are undefined without the relation StarsIn (m1) in the outer query
|
These "match-making" attributes must be propagated out so they are available (= accessible) by the outer query !!!!
Comments:
|
MovieStar ( name, addr, gender, birthdate ) StarsIn ( movieTitle, movieYear, starName ) |
|
SELECT DISTINCT m1.movieTitle, m1.movieYear FROM StarsIn m1 WHERE m1.movieYear - 40 ≤ (SELECT avg(s.birthdate) FROM StarsIn m2, MovieStar s WHERE m2.StarName = s.name AND m2.movieTitle = m1.movieTitle AND m2.movieYear = m1.movieYear) |
Note:
|
Notice that:
|
|
Employee: ssn fname lname dno salary --------- ------ -------- ----------- --------- 888665555 James Borg 1 55000.00 999887777 Alicia Zelaya 4 25000.00 987654321 Jennif Wallace 4 43000.00 987987987 Ahmad Jabbar 4 25000.00 123456789 John Smith 5 30000.00 333445555 Frankl Wong 5 40000.00 666884444 Ramesh Narayan 5 38000.00 453453453 Joyce English 5 25000.00 |
|
SELECT fname, lname FROM employee a WHERE salary = (SELECT max(salary) FROM employee b WHERE b.dno = a.dno) |
Note:
|
|
|