I am trying to insert into a new table all the names of the movies where helena bonhab carter and johnny depp starred in. I am selecting information firstfrom a table called movies which has a title and an id. and then from a table called stars which has a movie_id and a person_id. and lastly I am selecting from a table called people which has a id and name columns. I think my problem is in the union line but i don't know why.
CREATE TABLE jh( title TEXT)
INSERT INTO jh(title)
SELECT movies.title
FROM movies
WHERE movies.id in ( SELECT stars.movie_id FROM stars WHERE stars.person_id in(SELECT
people.id FROM people WHERE people.name = ' Johnny Depp' UNION SELECT people.id FROM people
WHERE people.name = 'Helena Bonham Carter'))
You can join the tables, filter for the actors you want, group by movie and set the condition the HAVING clause:
SELECT m.title
FROM movies m
INNER JOIN stars s ON s.movie_id = m.id
INNER JOIN people p ON p.id = s.person_id
WHERE p.name IN ('Johnny Depp', 'Helena Bonham Carter')
GROUP BY m.id, m.title
HAVING COUNT(*) = 2 -- the number of the actors
I really suggest you don't do query like that. for example, you could select all names in one single WHERE condition. And be ready to use JOIN if you have several tables need to be connected.
SELECT M.Title
FROM Movies M INNER JOIN Stars S on M.id = S.Movie_Id
INNER JOIN People P on S.PersonID = P.PersonID
WHERE P.Name in ('Johnny Depp','Helena Bonham Carter')
Related
so i get some data from IMDB for Movies and i wanted to find the movies that both Jonny Depp and Helena Bonham Carter starred in them
i tried this code
SELECT title
FROM movies
JOIN stars ON stars.movie_id = movies.id
JOIN people ON people.id = stars.person_id
**WHERE name = "Johnny Depp" AND name = "Helena Bonham Carter")**
the last part seems to not work this way and i have to write it like this
SELECT title
FROM movies
JOIN stars ON stars.movie_id = movies.id
JOIN people ON people.id = stars.person_id
WHERE name = "Johnny Depp" AND movie_id IN
(SELECT movie_id FROM people JOIN stars on stars.person_id = people.id WHERE name = "Helena Bonham Carter")
Why using the first aproach i dont get the results?
The column name cannot be two different values at the same time, which is what your predicate is testing.
Instead something like the following (untested) should work, we filter on the name being in the set of two names and count to make sure only movies with two unique names are returned:
select m.title
from movies m
join stars s on s.movie_id = m.id
join people p on p.id = s.person_id
where p.name in('Johnny Depp', 'Helena Bonham Carter')
group by m.title
having Count(distinct p.name) = 2;
It cannot be Johnny and Helena at the same time. You could use your IN approach or EXISTS:
SELECT title
FROM movies m
WHERE EXISTS
(
SELECT 1 FROM stars s
WHERE s.movie_id = m.movie_id
AND s.name = "Johnny Depp"
)
AND EXISTS
(
SELECT 1 FROM stars s
WHERE s.movie_id = m.movie_id
AND s.name = "Helena Bonham Carter"
)
I'm trying to find movie_ids for movies that both johnny Depp and Helena Bonham Carted played in. I'm using SQLite3 as software and the famous IMDB dataset.
The result I got: ids for movies that either of them played
The Desired result: ids for movies that both of them played in
select movie_id from stars where person_id in
(select id from people where name in
("Johnny Depp", "Helena Bonham Carter"))
);
SELECT s.movie_id
FROM stars s
INNER JOIN people p ON p.id = s.person_id
WHERE p.name in ('Johnny Depp', 'Helena Bonham Carter')
GROUP BY s.movie_id
HAVING COUNT(DISTINCT p.id) = 2
I need to create a new table with the title of the movies starred by Johnny Depp AND Helena Bonham Caster.
I have these tables:
Movies: (ID, title and year)
Stars: (Movie_ID, Person_ID)
People: (ID, Name, and Birth year)
CREATE TABLE johnny_helena (title TEXT);
INSERT INTO johnny_helena (title)
SELECT title
FROM movies
WHERE id IN (SELECT movie_id FROM stars WHERE person_id IN (SELECT id FROM people WHERE name = 'Johnny Depp') AND person_id IN (SELECT id FROM people WHERE name = 'Helena Bonham Caster'))
When I execute this code I receive one Table with 0 rows but should be 6.
I'm new em SQL and I believe that I'm making a foolish mistake. So, how I can fix this error?
Thanks.
One option uses joins and aggregation:
INSERT INTO johnny_helena (title)
SELECT m.title
FROM movies m
INNER JOIN stars s ON s.movie_id = m.id
INNER JOIN people p ON p.id = s.person_id
WHERE p.name in ('Johnny Depp', 'Helena Bonham Caster')
GROUP BY m.id, m.title
HAVING COUNT(*) = 2
i need to output all the movies that both "Johnny Depp" and "Helena Bonham Carter" starred. I think my problem is that I dont no what to putt inside of the WHERE
SELECT title
FROM movies JOIN
stars
ON movies.id = stars.movie_id JOIN
people ON stars.person_id = people.id
WHERE
this is what I got so far...
I would recommend moving the where clause into a query from one of the tables as it will be more efficient. Suppose your people table has a field called name then you could do a subquery like so:
SELECT stars.movie_id, people.name
FROM stars
JOIN (SELECT id, name FROM people WHERE name="Johnny Depp") AS people
ON people.id = stars.people_id
This gives all Johnny Depp movies, a similar query gives all Helena Bonham Carter movies. Now you can intersect the two queries:
SELECT stars.movie_id, people.name
FROM stars
JOIN (SELECT id, name FROM people WHERE name="Johnny Depp") AS people
ON people.id = stars.people_id
WHERE movie_id IN (SELECT stars.movie_id, people.name
FROM stars
JOIN (SELECT id, name FROM people WHERE name="Helena Bonham Carter") AS people
ON people.id = stars.people_id)
This gives all movies starring both. Now intersect this with your original query
SELECT
title
FROM movies
JOIN stars ON stars.movie_id = movies.id
JOIN people ON people.id = stars.person_id
WHERE starred_name IN ('Johnny Depp', 'Helena Bonham Carter')
-- i used "starred_name", becuase you never specified the name of the field.
my final cod was:
SELECT title
FROM people
JOIN stars ON people.id = stars.person_id
JOIN movies ON stars.movie_id = movies.id
WHERE movie_id IN (SELECT movie_id
FROM stars
JOIN people ON stars.person_id = people.id
WHERE name = "Helena Bonham Carter") and name = "Johnny Depp"
What is wrong with this statement? It results in no output.
select title from movies where id in
(select movie_id from stars where person_id in
(select id from people where name="Johnny Depp" intersect
select id from people where name="Helena Bonham Carter"));
This statement:
select id from people where name="Johnny Depp"
intersect
select id from people where name="Helena Bonham Carter"
returns the common results of the 2 queries, but since id is the unique primary key of the table people there is no case that it will return anything.
Simpler: if the 1st query returns 10 and the 2nd returns 20 the intersection of 10 and 20 does not exist.
You can get the result that you want with joins:
select distinct m.title
from movies m
inner join stars s1 on s1.movie_id = m.id
inner join stars s2 on s2.movie_id = m.id
inner join people p1 on p1.id = s1.person_id and p1.name = 'Johnny Depp'
inner join people p2 on p2.id = s2.person_id and p2.name = 'Helena Bonham Carter'