...

/

Solution Practice Set 3

Solution Practice Set 3

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

Solution Practice Set 3

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 AVG(BudgetInMillions)
FROM Movies;
-- Question # 1, Query 2
SELECT Name
FROM Movies
WHERE BudgetInMillions > (SELECT AVG(BudgetInMillions)
FROM Movies);
-- Question # 2, Query 1
SELECT * FROM DigitalAssets
RIGHT JOIN Actors
ON Id = ActorId;
-- Question # 2, Query 2
SELECT CONCAT(FirstName, " ", SecondName)
AS Actors_With_No_Online_Presence
FROM DigitalAssets
RIGHT JOIN Actors
ON Id = ActorId
WHERE URL IS NULL;
-- Question # 3, Query 1
SELECT CONCAT(FirstName, " ", SecondName)
FROM Actors
WHERE NOT EXISTS (SELECT ActorId
FROM DigitalAssets
WHERE ActorId = Id);
-- Question # 4, Query 1
SELECT Name, CollectionInMillions
FROM Movies
ORDER BY CollectionInMillions DESC;
-- Question # 4, Query 2
SELECT Name,
CollectionInMillions AS Collection_In_Millions
FROM Movies
ORDER BY CollectionInMillions DESC
LIMIT 1 OFFSET 4;
-- Question # 4, Query 3
SELECT Name,
CollectionInMillions AS Collection_In_Millions
FROM Movies
ORDER BY CollectionInMillions DESC
LIMIT 4, 1;
-- Question # 5, Query 1
SELECT LastUpdatedOn, Id
FROM Actors
INNER JOIN DigitalAssets
ON ActorId = Id;
-- Question # 5, Query 2
SELECT *
FROM Cast
INNER JOIN (SELECT LastUpdatedOn, Id
FROM Actors
INNER JOIN DigitalAssets
ON ActorId = Id) AS tbl
ON tbl.Id = ActorId;
-- Question # 5, Query 3
SELECT *
FROM Movies AS m
INNER JOIN (SELECT *
FROM Cast
INNER JOIN (SELECT LastUpdatedOn, Id
FROM Actors
INNER JOIN DigitalAssets
ON ActorId = Id) AS tbl1
ON tbl1.Id = ActorId) AS tbl2
ON tbl2.MovieId = m.Id;
-- Question # 5, Query 4
SELECT DISTINCT Name
AS Actors_Posting_Online_Within_Five_Days_Of_Movie_Release
FROM Movies AS m
INNER JOIN (SELECT *
FROM Cast
INNER JOIN (SELECT LastUpdatedOn, Id
FROM Actors
INNER JOIN DigitalAssets
ON ActorId = Id) AS tbl1
ON tbl1.Id = ActorId) AS tbl2
ON tbl2.MovieId = m.Id
WHERE ADDDATE(ReleaseDate, INTERVAL -5 Day) <= LastUpdatedOn
AND ADDDATE(ReleaseDate, INTERVAL +5 Day) >= LastUpdatedOn;
Terminal 1
Terminal
Loading...

Question # 1

Write a query to display all those movie titles whose budget is greater than the average budget of all the movies.

This question also requires flexing MySQL’s aggregation capabilities. First we’ll write a query to calculate the average budget for all the films as follows:

SELECT AVG(BudgetInMillions) 
FROM Movies;

Now, we can plug the above query as a sub-query and list all the movies whose budget was greater than the average budget across all movies.

SELECT Name 
FROM Movies 
WHERE BudgetInMillions > (SELECT AVG(BudgetInMillions) 
                          FROM Movies);

Question # 2

Find all those actors who don’t have any digital media presence using a right join statement.

The Actors table has the ID column which is the same as the ActorID column of the DigitalAssets table. In a right join, the table on the right side of the join has all the rows included which don’t ...

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