...

/

Solution Practice Set 4

Solution Practice Set 4

Get the solution to the exercise of viewing the information queried from a database.

Solution Practice Set 4

The database relationship model is reprinted below for reference.

widget

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.

Press + to interact
-- The lesson queries are reproduced below for convenient copy/paste into the terminal.
-- Question # 1, Query 1
SELECT Producer
FROM Movies
GROUP BY Producer;
-- Question # 1, Query 2
SELECT Producer
FROM Movies
GROUP BY Producer
HAVING COUNT(Producer) > 1;
-- Question # 1, Query 3
SELECT Producer AS Producer_Name, AVG(CollectionInMillions) AS Average_Collection_In_Millions
FROM Movies
GROUP BY Producer
HAVING COUNT(Producer) > 1;
-- Question # 2, Query 1
SELECT CONCAT (FirstName, " ", SecondName) AS Actors, MovieId, Producer
FROM Actors JOIN Cast
ON Actors.Id = Cast.ActorId
JOIN Movies
ON Cast.MovieId = Movies.Id;
-- Question # 2, Query 2
SELECT CONCAT (FirstName, " ", SecondName) AS Actors, MovieId, Producer
FROM Actors JOIN Cast
ON Actors.Id = Cast.ActorId
JOIN Movies
ON Cast.MovieId = Movies.Id
AND Producer <> 'Ryan Seacrest';
-- Question # 2, Query 3
SELECT DISTINCT(CONCAT (FirstName, " ", SecondName)) AS Actors_Who_Have_Not_Worked_with_Ryan_Seacrest
FROM Actors JOIN Cast
ON Actors.Id = Cast.ActorId
JOIN Movies
ON Cast.MovieId = Movies.Id
AND Producer <> 'Ryan Seacrest';
-- Question # 2, Query 4
SELECT c.ActorID, c.MovieId, m.Producer
FROM Cast c, Movies m
WHERE c.MovieId = m.Id;
-- Question # 2, Query 5
SELECT c.ActorID, c.MovieId, m.Producer
FROM Cast c, Movies m
WHERE c.MovieId = m.Id
AND m.Producer <> 'Ryan Seacrest';
-- Question # 2, Query 6
SELECT DISTINCT(CONCAT (a.FirstName, " ", a.SecondName)) AS Actors_Who_Have_Not_Worked_with_Ryan_Seacrest
FROM Cast c, Movies m, Actors a
WHERE c.MovieId = m.Id
AND m.Producer <> 'Ryan Seacrest'
AND c.ActorId = a.Id;
-- Question # 3, Query 1
SELECT ActorId, AssetType, LastUpdatedOn
FROM DigitalAssets
ORDER BY ActorId ASC,
LastUpdatedOn DESC;
-- Question # 3, Query 2
SELECT ActorId, MAX(LastUpdatedOn)
FROM DigitalAssets
GROUP BY ActorId;
-- Question # 3, Query 3
SELECT ActorId, AssetType, LastUpdatedOn
FROM DigitalAssets
WHERE (ActorId, LastUpdatedOn) IN
(SELECT ActorId, MAX(LastUpdatedOn)
FROM DigitalAssets
GROUP BY ActorID);
-- Question # 3, Query 4
CREATE 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 5
INSERT INTO DigitalActivityTrack (Actor_Id, Digital_Asset, Last_Updated_At)
SELECT ActorId, AssetType, LastUpdatedOn FROM DigitalAssets
WHERE (ActorId, LastUpdatedOn) In
(SELECT ActorId, MAX(LastUpdatedOn) FROM DigitalAssets
GROUP BY ActorID)
ORDER BY LastUpdatedOn DESC;
-- Question # 3, Query 6
SELECT CONCAT(a.FirstName, " ", a.SecondName) AS Actor_Name, Digital_Asset, Last_Updated_At
FROM Actors a, DigitalActivityTrack
WHERE a.Id = Actor_Id;
-- Question # 4, Query 1
SELECT CONCAT (FirstName, " ", SecondName) AS Actor_Name, NetWorthInMillions AS 3rd_Lowest_Net_Worth_In_Millions
From Actors a1
WHERE 2 = (SELECT COUNT(DISTINCT (NetWorthInMillions))
FROM Actors a2
WHERE a2. NetWorthInMillions < a1. NetWorthInMillions);
-- Question # 5, Query 1
SELECT ActorID, COUNT(ActorId)
FROM DigitalAssets
GROUP BY ActorId;
-- Question # 5, Query 2
SELECT ActorID, GROUP_CONCAT(AssetType)
FROM DigitalAssets
GROUP BY ActorId;
-- Question # 5, Query 3
SELECT CONCAT (FirstName, " ", SecondName) AS Actor_Name,
GROUP_CONCAT(AssetType) AS Digital_Assets
FROM Actors INNER JOIN DigitalAssets
ON Actors.Id = DigitalAssets.ActorId
GROUP BY Id;
-- Question # 5, Query 4
SELECT CONCAT (FirstName, " ", SecondName) AS Actor_Name,
GROUP_CONCAT(AssetType) AS Digital_Assets
FROM Actors LEFT JOIN DigitalAssets
ON Actors.Id = DigitalAssets.ActorId
GROUP BY Id;
Terminal 1
Terminal
Loading...

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 ...

Access this course and 1400+ top-rated courses and projects.