Check if the sub-query includes all of the stated values - sql

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

Related

Cant understand why my approach isnt right when iam adding a condition with "and" to look some names in the database

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"
)

Asking a condition based on two cases in the same column. sqlite3

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')

How I can use SQL to create a new table using WHERE/AND conditions?

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

how can I use two characters that are in the same column to limit my SELECT in SQL

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"

SQL: Wrong output

I have written a query but I get wrong results.
These are the tables:
|actor|-->|role|-->|movie|->|movie_has_genre|-->|genre|
In each table I have (table: fields):
actor: actor_id
role: actor_id, movie_id
movie:movie_id
movie_has_genre: movie_id, genre_id
genre: genre_id
I'm looking to find the actors' ids that have played in all kinds of movies.
My code is:
SELECT a.actor_id
FROM actor a
WHERE NOT EXISTS(
SELECT *
FROM genre g
WHERE NOT EXISTS(
SELECT *
FROM movie m
WHERE m.movie_id in(
SELECT m1.movie_id
FROM movie m1, role r, movie_has_genre mg
WHERE a.actor_id=r.actor_id AND r.movie_id=m1.movie_id
AND m1.movie_id=mg.movie_id AND mg.genre_id=g.genre_id))
);
Can you help?
It's pseudo code but you can get the idea
select a.actor_id, count(mg.genre_id) as countkinds
from actor a
join role r on r.actor_id = a.actor_id
join movie_has_genre mg on mg.movie_id =r.movie_id
group by a.actor_id
having count(mg.genre_id) = (select count(*) from genre)
just join actors to genre_ids, count it and return only all played in all genres.
note you don't need to join movies table since you can join role and movie_has_genre for the same movie directly by the id