here are the relevant tables
movie(id, title, relYear, category, runTime, director,
studioName, description, rating)
actor(aID, fName, surname, gender)
stars(movieID, actorID)
movGenre(movieID, genre)
I'm just trying to return movies where 'Michael Fassbender' stars using a subquery ... 'id' in movie refers to the the movie id not the actor id if that was the case I could do
SELECT title, category
FROM movie
WHERE 'id' =(SELECT 'aID'
FROM actor
WHERE fName='Michael' and surname='Fassbender')
Is there a way to do it without using any joins?
You can utilize Correlated Subquery with Exists()
SELECT m.title, m.category
FROM movie AS m
WHERE EXISTS (SELECT 1
FROM stars AS s
JOIN actor AS a ON a.aID = s.actorID
WHERE a.fName = 'Michael' AND
a.surname = 'Fassbender' AND
s.movieID = m.id)
Above solution still utilize JOIN
in some way (inside the subquery). A solution utilizing no Join at all can be:
SELECT title, category
FROM movie
WHERE id IN (SELECT movieID
FROM stars
WHERE actorID IN (SELECT aID
FROM actor
WHERE fName = 'Michael' AND
surname = 'Fassbender'))
PS Unless this is some trick assignment/HW, I would seriously recommend against using this query in a real-life application.
SELECT DISTINCT m.title, m.category
FROM movie
INNER JOIN stars s
ON s.movieID = m.id
INNER JOIN actors a
ON s.actorID = a.aID
AND a.fName='Michael'
AND a.surname='Fassbender'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.