Solution Practice Set 4
Get the solution to the exercise of viewing the information queried from a database.
We'll cover the following...
Solution Practice Set 4
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 ProducerFROM MoviesGROUP BY Producer;-- Question # 1, Query 2SELECT ProducerFROM MoviesGROUP BY ProducerHAVING COUNT(Producer) > 1;-- Question # 1, Query 3SELECT Producer AS Producer_Name, AVG(CollectionInMillions) AS Average_Collection_In_MillionsFROM MoviesGROUP BY ProducerHAVING COUNT(Producer) > 1;-- Question # 2, Query 1SELECT CONCAT (FirstName, " ", SecondName) AS Actors, MovieId, ProducerFROM Actors JOIN CastON Actors.Id = Cast.ActorIdJOIN MoviesON Cast.MovieId = Movies.Id;-- Question # 2, Query 2SELECT CONCAT (FirstName, " ", SecondName) AS Actors, MovieId, ProducerFROM Actors JOIN CastON Actors.Id = Cast.ActorIdJOIN MoviesON Cast.MovieId = Movies.IdAND Producer <> 'Ryan Seacrest';-- Question # 2, Query 3SELECT DISTINCT(CONCAT (FirstName, " ", SecondName)) AS Actors_Who_Have_Not_Worked_with_Ryan_SeacrestFROM Actors JOIN CastON Actors.Id = Cast.ActorIdJOIN MoviesON Cast.MovieId = Movies.IdAND Producer <> 'Ryan Seacrest';-- Question # 2, Query 4SELECT c.ActorID, c.MovieId, m.ProducerFROM Cast c, Movies mWHERE c.MovieId = m.Id;-- Question # 2, Query 5SELECT c.ActorID, c.MovieId, m.ProducerFROM Cast c, Movies mWHERE c.MovieId = m.IdAND m.Producer <> 'Ryan Seacrest';-- Question # 2, Query 6SELECT DISTINCT(CONCAT (a.FirstName, " ", a.SecondName)) AS Actors_Who_Have_Not_Worked_with_Ryan_SeacrestFROM Cast c, Movies m, Actors aWHERE c.MovieId = m.IdAND m.Producer <> 'Ryan Seacrest'AND c.ActorId = a.Id;-- Question # 3, Query 1SELECT ActorId, AssetType, LastUpdatedOnFROM DigitalAssetsORDER BY ActorId ASC,LastUpdatedOn DESC;-- Question # 3, Query 2SELECT ActorId, MAX(LastUpdatedOn)FROM DigitalAssetsGROUP BY ActorId;-- Question # 3, Query 3SELECT ActorId, AssetType, LastUpdatedOnFROM DigitalAssetsWHERE (ActorId, LastUpdatedOn) IN(SELECT ActorId, MAX(LastUpdatedOn)FROM DigitalAssetsGROUP BY ActorID);-- Question # 3, Query 4CREATE TABLE DigitalActivityTrack (Id INT AUTO_INCREMENT NOT NULL PRIMARY KEY,Actor_Id INT NOT NULL,Digital_Asset VARCHAR(20) NOT NULL,Last_Updated_At DATETIME Not NULL DEFAULT NOW());-- Question # 3, Query 5INSERT INTO DigitalActivityTrack (Actor_Id, Digital_Asset, Last_Updated_At)SELECT ActorId, AssetType, LastUpdatedOn FROM DigitalAssetsWHERE (ActorId, LastUpdatedOn) In(SELECT ActorId, MAX(LastUpdatedOn) FROM DigitalAssetsGROUP BY ActorID)ORDER BY LastUpdatedOn DESC;-- Question # 3, Query 6SELECT CONCAT(a.FirstName, " ", a.SecondName) AS Actor_Name, Digital_Asset, Last_Updated_AtFROM Actors a, DigitalActivityTrackWHERE a.Id = Actor_Id;-- Question # 4, Query 1SELECT CONCAT (FirstName, " ", SecondName) AS Actor_Name, NetWorthInMillions AS 3rd_Lowest_Net_Worth_In_MillionsFrom Actors a1WHERE 2 = (SELECT COUNT(DISTINCT (NetWorthInMillions))FROM Actors a2WHERE a2. NetWorthInMillions < a1. NetWorthInMillions);-- Question # 5, Query 1SELECT ActorID, COUNT(ActorId)FROM DigitalAssetsGROUP BY ActorId;-- Question # 5, Query 2SELECT ActorID, GROUP_CONCAT(AssetType)FROM DigitalAssetsGROUP BY ActorId;-- Question # 5, Query 3SELECT CONCAT (FirstName, " ", SecondName) AS Actor_Name,GROUP_CONCAT(AssetType) AS Digital_AssetsFROM Actors INNER JOIN DigitalAssetsON Actors.Id = DigitalAssets.ActorIdGROUP BY Id;-- Question # 5, Query 4SELECT CONCAT (FirstName, " ", SecondName) AS Actor_Name,GROUP_CONCAT(AssetType) AS Digital_AssetsFROM Actors LEFT JOIN DigitalAssetsON Actors.Id = DigitalAssets.ActorIdGROUP BY Id;
Question # 1
Write a query to display the average collection in millions of producers who have produced more than one movie.
To answer this question, we need to shortlist only those producers whose name appears more than once in the Movies table. This is hinting towards grouping the results by producers:
SELECT Producer
FROM Movies
GROUP BY Producer;
Since we are only interested in producers who have produced more than one movie, we will add a restriction on the number of times a producer’s name appears in the Movies table. The HAVING clause used with the GROUP BY clause will give us the desired result.
SELECT Producer
FROM Movies
GROUP BY Producer
HAVING COUNT(Producer) > 1;
We are now left with two produces who have multiple movies. The last step is to find the average collection in millions of the films by these two producers:
SELECT Producer AS Producer_Name, AVG(CollectionInMillions) AS Average_Collection_In_Millions
FROM Movies
GROUP BY Producer
HAVING COUNT(Producer) > 1;
Question # 2
Find all those actors who have not worked with producer Ryan Seacrest.
Approach 1: Joining three tables in a single SQL query.
The information on actors, movies and producers is scattered in three tables; Actors, Cast and Movies. We need to join the tables together to find the answer. Joining three tables in a single SQL query can be a tricky concept. The first table is related to the second table and the second table is related to the third table. In our case the Actors table is related to the Cast table and the Cast table is related to the Movies table. We want the names of the actors from the Actors table and information ...