Implementation of set operators, nested queries and join queries.
AIM
Consider the schema for MovieDatabase:
ACTOR (Act_id, Act_Name, Act_Gender)
DIRECTOR (Dirid, Dir _Name, Dir Phone)
MOVIES (Mov_id, Mov_Title, Mov_Year, Mov_Lang, Dir _id)
MOVIE_CAST (Act_id, Mov_id, Role)
RATING (Mov_id, Rev_Stars)
Write SQL queries to
List the titles of all movies directed by 'Hitchcock'.
Find the movie names where one or more actors acted in two or more movies.
List all actors who acted in a movie before 2000 and also in a movieafter 2015 (useJOlN operation).
Find the title of movies and number of stars for each movie that has at least one rating and find the highest number of stars that movie received. Sort the result by movie title.
Update rating of all movies directed by 'Steven Spielberg' to 5.
COMMANDS
Create tables
Create Actor table
CREATETABLE Actor ( Act_Id INTPRIMARYKEY, Act_Name VARCHAR2(25), Act_Gender CHAR(1));
Create Director table
CREATETABLE Director ( Dir_Id INTPRIMARYKEY, Dir_Name VARCHAR(25), Dir_Phone VARCHAR(15));
-- DirectorsINSERTINTO Director VALUES(1,'Alfred Hitchcock','111-1111');INSERTINTO Director VALUES(2,'Steven Spielberg','222-2222');INSERTINTO Director VALUES(3,'Christopher Nolan','333-3333');-- ActorsINSERTINTO Actor VALUES(1,'Leonardo DiCaprio','M');INSERTINTO Actor VALUES(2,'Joseph Gordon-Levitt','M');INSERTINTO Actor VALUES(3,'Meryl Streep','F');INSERTINTO Actor VALUES(4,'Tom Hanks','M');INSERTINTO Actor VALUES(5,'Natalie Portman','F');-- MoviesINSERTINTO Movies VALUES(1,'Psycho',1960,'English',1);INSERTINTO Movies VALUES(2,'Rear Window',1954,'English',1);INSERTINTO Movies VALUES(3,'Jurassic Park',1993,'English',2);INSERTINTO Movies VALUES(4,'The Post',2017,'English',2);INSERTINTO Movies VALUES(5,'Inception',2010,'English',3);INSERTINTO Movies VALUES(6,'Interstellar',2014,'English',3);-- Movie CastINSERTINTO Movie_Cast VALUES(1,5,'Cobb');INSERTINTO Movie_Cast VALUES(1,6,'Cooper');INSERTINTO Movie_Cast VALUES(2,5,'Arthur');INSERTINTO Movie_Cast VALUES(3,3,'Character A');INSERTINTO Movie_Cast VALUES(3,4,'Character B');INSERTINTO Movie_Cast VALUES(4,3,'Character C');INSERTINTO Movie_Cast VALUES(4,4,'Character D');INSERTINTO Movie_Cast VALUES(5,6,'Murph');-- RatingsINSERTINTO Rating VALUES(1,5);INSERTINTO Rating VALUES(1,4);INSERTINTO Rating VALUES(3,4);INSERTINTO Rating VALUES(5,5);INSERTINTO Rating VALUES(6,4);
Queries
List the titles of all movies directed by "Hitchcock"
SELECT Mov_Title
FROM Movies M
JOIN Director D ON M.Dir_Id = D.Dir_Id
WHERE D.Dir_Name ='Alfred Hitchcock';
Find the movie names where one or more actors acted in two or more movies
SELECTDISTINCT M.Mov_Title
FROM Movies M
JOIN Movie_Cast MC ON M.Mov_Id = MC.Mov_Id
WHERE MC.Act_Id IN(SELECT Act_Id
FROM Movie_Cast
GROUPBY Act_Id
HAVINGCOUNT(Mov_Id)>=2);
List all actors who acted in a movie before 2000 and also in a movie after 2015(use JOIN operation).
SELECTDISTINCT A.Act_Name
FROM Actor A
JOIN Movie_Cast MC1 ON A.Act_Id = MC1.Act_Id
JOIN Movies M1 ON MC1.Mov_Id = M1.Mov_Id
JOIN Movie_Cast MC2 ON A.Act_Id = MC2.Act_Id
JOIN Movies M2 ON MC2.Mov_Id = M2.Mov_Id
WHERE M1.Mov_Year <2000AND M2.Mov_Year >2015;
Find the title of movies and number of stars for each movie that has at least one rating and find the highest number of stars that movie received. Sort the result by movie title
SELECT M.Mov_Title,COUNT(R.Rev_Stars)AS Num_Ratings,MAX(R.Rev_Stars)AS Highest_Rating
FROM Movies M
JOIN Rating R ON M.Mov_Id = R.Mov_Id
GROUPBY M.Mov_Title
ORDERBY M.Mov_Title;
Update rating of all movies directed by "Steven Spielberg" to 5
UPDATE Rating
SET Rev_Stars =5WHERE Mov_Id IN(SELECT M.Mov_Id
FROM Movies M
JOIN Director D ON M.Dir_Id = D.Dir_Id
WHERE D.Dir_Name ='Steven Spielberg');