Solution Practice Set 2
Get the solution to the exercise of viewing the information queried from a database.
We'll cover the following...
Solution Practice Set 2
The database relationship model is reprinted below for reference.
Connect to the terminal below by clicking in the widget. Once connected, the command line prompt will show up. Enter or copy and paste the command ./DataJek/Lessons/quiz.sh and wait for the MySQL prompt to start-up.
-- The lesson queries are reproduced below for convenient copy/paste into the terminal.-- Question # 1, Query 1SELECT Id, FirstName, SecondName, MovieIdFROM ActorsINNER JOIN CastON Id = ActorId;-- Question # 1, Query 2SELECT Id, COUNT(*)FROM ActorsINNER JOIN CastON Id = ActorIdGROUP BY Id;-- Question # 1, Query 3SELECT Id,COUNT(*) AS MovieCountFROM ActorsINNER JOIN CastON Id = ActorIdGROUP BY IdHAVING MovieCount > 1;-- Question # 1, Query 4SELECT CONCAT (FirstName, " ", SecondName)AS Actor_Names,Movie_CountFROM Actors aINNER JOIN (SELECT Id,COUNT(*) AS Movie_CountFROM ActorsINNER JOIN CastON Id = ActorIdGROUP BY IdHAVING Movie_Count > 1) AS tblON tbl.Id = a.Id;-- Question # 2, Query 1SELECT IdFROM MoviesWHERE Name="Mr & Mrs. Smith";-- Question # 2, Query 2SELECT ActorIdFROM CastWHERE MovieId IN (SELECT IdFROM MoviesWHERE Name="Mr & Mrs. Smith");-- Question # 2, Query 3SELECT CONCAT(FirstName, " ", SecondName)AS "Cast Of Mr. & Mrs. Smith"FROM ActorsWHERE Id IN ( SELECT ActorIdFROM CastWHERE MovieId IN (SELECT IdFROM MoviesWHERE Name="Mr & Mrs. Smith"));-- Question # 2, Query 4SELECT CONCAT(FirstName, " ", SecondName)AS "Cast Of Mr. & Mrs. Smith"FROM ActorsINNER JOIN (SELECT ActorIdFROM CastINNER JOIN MoviesON MovieId = IdWHERE Name="Mr & Mrs. Smith") AS tblON tbl.ActorId = Id;-- Question # 3, Query 1SELECT Name, ActorIdFROM MoviesINNER JOIN CastOn Id = MovieId;-- Question # 3, Query 2SELECT tbl.Name AS Movie_Name,CONCAT(FirstName, " ", SecondName) AS Actor_NameFROM ActorsINNER JOIN (SELECT Name, ActorIdFROM MoviesINNER JOIN CastOn Id = MovieId) AS tblON tbl.ActorId = IdORDER BY tbl.Name ASC;-- Question # 4, Query 1SELECT tbl.Name, COUNT(*)FROM ActorsINNER JOIN (SELECT Name, ActorId, MovieIdFROM MoviesINNER JOIN CastOn Id = MovieId) AS tblON tbl.ActorId = IdGROUP BY tbl.MovieId;-- Question # 4, Query 2SELECT MovieId, COUNT(*)FROM CastGROUP BY MovieId;-- Question # 4, Query 3SELECT Name AS Movie_Name,Actor_CountFROM MoviesINNER JOIN (SELECT MovieId, COUNT(*) AS Actor_CountFROM CastGROUP BY MovieId) AS tblON tbl.MovieID = Id;-- Question # 5, Query 1SELECT IdFROM ActorsWHERE FirstName = "Tom"AND SecondName = "Cruise";-- Question # 5, Query 2SELECT MovieIdFROM CastWHERE ActorId = (SELECT IdFROM ActorsWHERE FirstName = "Tom"AND SecondName = "Cruise");-- Question # 5, Query 3SELECT DISTINCT ProducerFROM MoviesWHERE Id IN (SELECT MovieIdFROM CastWHERE ActorId = (SELECT IdFROM ActorsWHERE FirstName = "Tom"AND SecondName = "Cruise"));-- Question # 5, Query 4SELECT DISTINCT ProducerFROM MoviesWHERE Id NOT IN (SELECT MovieIdFROM CastWHERE ActorId = (SELECT IdFROM ActorsWHERE FirstName = "Tom"AND SecondName = "Cruise"));-- Question # 5, Query 5SELECT DISTINCT ProducerFROM MoviesWHERE ProducerNOT IN (SELECT ProducerFROM MoviesWHERE Id IN (SELECT MovieIdFROM CastWHERE ActorId = (SELECT IdFROM ActorsWHERE FirstName = "Tom"AND SecondName = "Cruise")));
Question # 1
Write a query to display all those actors who have acted in 2 or more movies.
In this question we are required to print the names of the actors who have acted in two or more movies. The names of the actors are in the Actors table and the number of movies an actor has appeared in is in the Cast table. If we join the two tables, we can get the name of actor and the ID of the movie that the actor has starred in. Let’s see how that looks like:
SELECT Id, FirstName, SecondName, MovieId
FROM Actors
INNER JOIN Cast
ON Id = ActorId;
Each row contains a movie ID in which an actor has starred. We can GROUP BY the result of the above query by ID of each actor so that all the movies that an actor has acted in, fall into the same group. Next, we simply count the rows in each group. So far, we have the following:
SELECT Id, COUNT(*)
FROM Actors
INNER JOIN Cast
ON Id = ActorId
GROUP BY Id;
Note we have removed the columns in the SELECT clause since they don’t participate in the aggregation criteria. Now we’ll apply the restriction to only list those groups which have more than one row to fulfill the requirement to print names of only those actors who have acted in at least two movies.
SELECT Id,
COUNT(*) AS MovieCount
FROM Actors
INNER JOIN Cast
ON Id = ActorId
GROUP BY Id
HAVING MovieCount > 1;
The last piece is to print the actor’s name. The above query is printing actor Id and the count of movies the actor has been part of. We can join the result of the above query with the Actors table based on the common actor ID column the ...