I have tables with data on actors, directors and movies separately but i need to find a way to input a director and output a list of all the actors that have worked with that director and all of the films those actors have been in. As this could be a very long list, they also need to limit the results in some sensible way. I have the SQL code to give out list of directors and the actors they have worked with and the movies the actor has been in with the director but we need all the movies the actors have worked in including the ones without that director?
Movie Table
Movie_ID Title Director Release Date
1 A John 2015
Actor Table
Actor_ID Actor_Name
if that's the structure that you have, I think you need a foreign key in the MovieTable to be able to relate the actors with the movies they have worked in. or a diferent table where you have the ID of actors and movies they have worked in, in that way you should do something like this:
SELECT actor_name, title, director
FROM MovieTable m
inner join actorTable a
on m.movieID = a.actor_name
WHERE m.director = "yourImput"
LIMIT (0,10) --limit that only displays 10 rows
Mmmm.. I'm not sure of the data structure partially posted in comments, but aren't you looking for something like this ?
SELECT Title FROM Movie m, Acts a, Actors ac
WHERE m.Movie_ID = a.Movie_ID
AND a.Actor_ID = ac.Actor_ID
AND m.Director='the name you are looking for';
Related
I'm trying to learn PostgreSQL with the imdb database and I can't seem to figure out how to list the directors who have not directed a movie since a particular year.
I have three tables to work with
Table movie with mov_id, mov_title, mov_year
Table director with dir_id, dir_name
Table movie_direction with dir_id and movie_id
I tried the code below to list all the directors who have not directed a movie since 1988, and this isn't working. Can someone guide me to the right direction on how to achieve this?
from movie_direction
join director d using (dir_id)
join movie m using (mov_id)
where m.mov_year < 1988
Use aggregation:
select d.dir_name
from movie_direction md join
director d
using (dir_id) join
movie m
using (mov_id)
group by dir_id, d.dir_name
having max(m.mov_year) < 1988;
Note that this aggregates by dir_id, just in case two directors have the same name.
I have two tables movie with moive id, movie title, director of the movie, and rating with rating id, movie id, and rating.
The question is to select director's name together with the title(s) of the movie(s) they directed that received the highest rating among all of their movies, and the value of that rating.
I am trying to understand the following solution
select distinct director, title, stars
from (movie join rating using (mid)) m
where stars in (select max(stars)
from rating join movie using (mid)
where m.director = director)
I am in particular confused with the last subquery
select max(stars)
from rating join movie using (mid)
where m.director = director
from all I know, '=' can only be followed by a fixed value, but here it seems to suggest 'looping' through all distinct directors. Which table is the latter director referring to? And how does the looping concept work in sql?
Although this code works to find the highest rated of all movies, it does not do so for each distinct director and it's not the simplest solution, which I will include below. However, the answers to your questions are:
1) The second director (from the where clause in the subquery) is referring to the table created in that subquery while the other m.director is using the m alias of the first table created in the main query.
2) This isn't really a loop here, in the traditional sense of the word. Basically what the above query is saying is: 'Give me the distinct director name, movie name, and rating from the table created by joining rating to movie, where the number of stars is the largest number of stars pulled from this subquery.' Loops for SQL server use the WHILE keyword but they are pretty rare in SQL since there are other functions (or clauses) that can fulfill the same purpose without the need for iteration.
The query posted in your comment only returns a single line with the data for the highest rated movie of all movies in the database not the highest rated movie for each director. The following is a simpler way of writing the query which gives the highest rating achieved for all movies for each director:
SELECT director, title, MAX(stars)
FROM movie
JOIN rating
ON movie.title = rating.movieID
GROUP BY director
I am practicing for the database exam I have shortly but I can't figure out how to make this query.
I copy paste the excercise as it is:
Given 2 tables
FILM(Title, Director, Year, Genre)
InCastOf(Actor, Film) Where InCastOf(film) is constrained to Film(Title)
Write a query to find all couples of actors who have played EXACTLY in the same movies.
What I got so far is:
SELECT InCastOf.Actor AS A1, T.Actor AS A2, InCastOf.Film FROM InCastOf
JOIN (SELECT Actor, Film FROM InCastOf) AS T ON InCastOf.Film = T.Film
WHERE InCastOf.Actor != T.Actor
Which returns me for each film a pair of actors who played in it. I could use group by etc to nail down the query, but the word "Exactly" is causing me some trouble because I need to make sure those actors only worked on the same movies and nothing else. At least that is what I understand from the word "Exactly".
Thanks for the help!
Thanks to the hint from #Martin Smith I wrote the query as following:
SELECT T1.Actor AS A1, T2.Actor AS A2, T1.films FROM (
SELECT Actor, GROUP_CONCAT(Film) AS films
FROM InCastOf
GROUP BY Actor) AS T1
JOIN (
SELECT Actor, GROUP_CONCAT(Film) AS films
FROM InCastOf
GROUP BY Actor) AS T2
ON T1.films = T2.films
WHERE T1.Actor > T2.Actor
The a table of movies with attribute of directors and actors. The actor is a varray type of actor_type contain 5 actor. i want to retrieve the movie that actor is both director and actor.
i tried
select actors, title, director
from movie
where actors = director
it telling me inconsistent datatype
You didn’t provide the sample data so my understanding from your description is:
The director and actor attribute are in the movie table and the actor is displayed as Varray, which being called actor_type. Also, the actor Varray contains 5 actors in each movie.
The way to query Varray is different, you can use TABLE expression in FROM clause.
This website might help you
Database Object-Relational Developer's Guide:
https://docs.oracle.com/database/121/ADOBJ/adobjcol.htm#ADOBJ00204
Then, you have to loop through the actor list and use the EXISTS or IN operator to check if the director is inside the actor list instead of using actors=director
You didn't specify what datatype the director column is. You can do a convert on the join actor = convert(director).
But I highly recommend you don't do this.
The problem with joining on strings is it is very inconsistent and if this is your only option to join actor to director, you need to restructure your tables.
I advise you have a Person table which contains both actors and directors.
You then need a table for movies and have the director and actor as foreign keys.
If the movie can have more than one actor, for instance, you then need a link table between person and movie tables (Same with the director if you have more than one director)
Hope this helps.
So, I'm doing a movie database and I got one table for actors and one table for movie titles.
In the Movies table I got Three columns for Actors. Actor_1, Actor_2, Actor_3. In these fields, I only write numbers which corresponds to a row in the Actors table.
Each actor have these columns:
Actor_ID, Firstname, Surname
Now if I do this query:
SELECT movie.titel, firstname + ' ' + surname AS name
FROM Movie
INNER JOIN Actors ON movie.actor_1=actor.actor_id
Then I get almost what I want. I get the movie titles but only one actor per movie. I don't know how I am supposed to do for the movies where I got two or three actors.
I had to translate to code so it would be more understandable. The code in its original shape works so don't mind if it's not 100% here. Just would appreciate some pointers on how I could do.
You need to change your table design to include a junction table. So you will have
Movies
ID
Etc
Actors
ID
Etc
MoviesActors
MovieID
ActorID
Etc
Your query might be:
SELECT m.MovieTitle, a.ActorName
FROM Actors a
INNER JOIN (Movies
INNER JOIN MoviesActors ma
ON m.ID = ma.MovieID)
ON a.ID = ma.ActorID
Relational database design
Junction tables