DISCLAIMER : Never Learned Relational Algebra but it looks interesting
From the schema given and your question, this is what the SQL should be:
SELECT
emp_mgr.person_name
FROM
manages emp_mgr
INNER JOIN employee emp ON emp_mgr.person_name = emp.person_name
INNER JOIN employee mgr ON emp_mgr.manager_name = mgr.person_name
WHERE
emp.street = mgr.street AND
emp.city = mgr.city
;
Here is another query that only uses JOINs, no WHERE clause:
SELECT
emp.person_name
FROM
(SELECT A.person_name,B.street,B.city FROM manages A
INNER JOIN employee B ON A.person_name = B.person_name) emp
NATURAL JOIN
(SELECT A.manager_name,B.street,B.city FROM manages A
INNER JOIN employee B ON A.manager_name = B.person_name) mgr
;
The first query gets all employees who are managed and their managers in the form of a Cartesian Product. Then, it looks for a common street and city.
The second query collects personnel records (name,street,city) of employees and their managers and performs a NATURAL JOIN between the employess and their managers using (street,city).
If you can transalate both queries back to Relational Algebra, I think you will have what you are looking for. I believe the second may be of better help.
Your sql query is wrong:
- Assume there is a student s1 who has passed one exam
after '2000-01-01' and none before.
Your query results in {s1} - {} = {s1}. This will be a false positive.
- Assume there is a student s1 that passed three exams
after '2000-01-01' and one exam before.
Your query results in {s1} - {s1} = {}. This will be a false negative.
I'm having a bit of trouble reading your algebra expressions, but the first one looks ok. It should not matter that you do the selection (topic = "motorcycle") after the join instead of joining on the selection.
The second one can't be right. Assume there's a newspaper that published both an article on motorcycle and an article on something else. Your expression will pick the article on something else and therefore return that newspaper (incorrectly).
Best Answer
This line represents a cartesian product of 3 tables:
After creating a cartesian product you filter out rows that match the predicate.
If you look closer at this query it is equivalent to:
Maybe you should analyze the second query.