Solution Practice Set 3
Get the solution to the exercise of viewing the information queried from a database.
We'll cover the following...
Solution Practice Set 3
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 AVG(BudgetInMillions)FROM Movies;-- Question # 1, Query 2SELECT NameFROM MoviesWHERE BudgetInMillions > (SELECT AVG(BudgetInMillions)FROM Movies);-- Question # 2, Query 1SELECT * FROM DigitalAssetsRIGHT JOIN ActorsON Id = ActorId;-- Question # 2, Query 2SELECT CONCAT(FirstName, " ", SecondName)AS Actors_With_No_Online_PresenceFROM DigitalAssetsRIGHT JOIN ActorsON Id = ActorIdWHERE URL IS NULL;-- Question # 3, Query 1SELECT CONCAT(FirstName, " ", SecondName)FROM ActorsWHERE NOT EXISTS (SELECT ActorIdFROM DigitalAssetsWHERE ActorId = Id);-- Question # 4, Query 1SELECT Name, CollectionInMillionsFROM MoviesORDER BY CollectionInMillions DESC;-- Question # 4, Query 2SELECT Name,CollectionInMillions AS Collection_In_MillionsFROM MoviesORDER BY CollectionInMillions DESCLIMIT 1 OFFSET 4;-- Question # 4, Query 3SELECT Name,CollectionInMillions AS Collection_In_MillionsFROM MoviesORDER BY CollectionInMillions DESCLIMIT 4, 1;-- Question # 5, Query 1SELECT LastUpdatedOn, IdFROM ActorsINNER JOIN DigitalAssetsON ActorId = Id;-- Question # 5, Query 2SELECT *FROM CastINNER JOIN (SELECT LastUpdatedOn, IdFROM ActorsINNER JOIN DigitalAssetsON ActorId = Id) AS tblON tbl.Id = ActorId;-- Question # 5, Query 3SELECT *FROM Movies AS mINNER JOIN (SELECT *FROM CastINNER JOIN (SELECT LastUpdatedOn, IdFROM ActorsINNER JOIN DigitalAssetsON ActorId = Id) AS tbl1ON tbl1.Id = ActorId) AS tbl2ON tbl2.MovieId = m.Id;-- Question # 5, Query 4SELECT DISTINCT NameAS Actors_Posting_Online_Within_Five_Days_Of_Movie_ReleaseFROM Movies AS mINNER JOIN (SELECT *FROM CastINNER JOIN (SELECT LastUpdatedOn, IdFROM ActorsINNER JOIN DigitalAssetsON ActorId = Id) AS tbl1ON tbl1.Id = ActorId) AS tbl2ON tbl2.MovieId = m.IdWHERE ADDDATE(ReleaseDate, INTERVAL -5 Day) <= LastUpdatedOnAND ADDDATE(ReleaseDate, INTERVAL +5 Day) >= LastUpdatedOn;
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 ...