I have an assignment that I am having trouble with, since this is for my studies I would appreciate it if you didn't give me the answer straight up, but instead gave me clues (I don't want to cheat).
I have three tables: Movies, Stars and ActsIn. I want to retrieve movies By a certain actor (in this case, Cameron Diaz) but I only want to display her LATEST movie. So basically there are two conditions, A name condition, and a year released condition.
I am very close to completing it, but I am stuck on a vital part. If I apply the conditions separately they work, but I need to have it all in the one statement. This is what i have so far.
SELECT M.Title, M.Yearreleased
FROM Movies M NATURAL JOIN Stars S NATURAL JOIN ActsIn A
WHERE Yearreleased = (SELECT MAX(Yearreleased) FROM Movies)
This will display the movies released in the latest year. If i swap the WHERE condition of this statement with:
WHERE S.Familyname = 'Diaz'
I will get all the movies Cameron Diaz acted in.
I have tried the following code combinations that I thought would work:
SELECT M.Title, M.Yearreleased
FROM Movies M NATURAL JOIN Stars S NATURAL JOIN ActsIn A
WHERE Yearreleased = (SELECT MAX(Yearreleased) FROM Movies) AND S.Familyname = 'Diaz'
My results from this query was literally an empty table.
All i guess i need, is for you guys to give me clues as to how i can join these two conditions into the one statement. THANK YOU! Let me know if you need sample data or a Fiddle
UPDATE
I think I may have solved it, although I did it by complete accident, maybe you could explain to me why it works? The query is:
SELECT M.Title, M.Yearreleased
FROM Movies M
NATURAL JOIN Stars S
NATURAL JOIN ActsIn A
WHERE Yearreleased = (SELECT MAX(Yearreleased)
FROM Movies M
NATURAL JOIN Stars S
NATURAL JOIN ActsIn A
WHERE S.Familyname = 'Diaz')
AND S.Familyname = 'Diaz'
This outputs the latest movie released by Cameron Diaz.