80 Solution Practice Set 3
80 Solution Practice Set 3
-- 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
Question # 1
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 satisfy the join
criteria. In this case, we want to include all the actors that don't have their
ID present in the DigitalAssets table, so we need to place the Actors table
on the right of the RIGHT JOIN clause.
The result set of the above query sets NULL for columns from
DigitalAssets table for those rows from the Actors table that don't have a
corresponding entry in the DigitalAssets table. We can predicate on the
column URL being NULL to identify those actors who don't have a social
media presence.
Can you rewrite the previous query without a join and using EXISTS
operator?
We can grab all the actor IDs that exist in the DigitalAssets table and then
select those actors from the Actors table whose ID doesn't appear in the
result set of the subquery.
Question # 4
Write a query to print the name of the fifth highest grossing movie at
the box office.
It's trivial to print the list of Movies sorted by how much they made at the
box office in descending fashion as follows:
SELECT Name,
CollectionInMillions AS Collection_In_Millions
FROM Movies
ORDER BY CollectionInMillions DESC
LIMIT 1 OFFSET 4;
Alternative syntax would be:
SELECT Name,
CollectionInMillions AS Collection_In_Millions
FROM Movies
ORDER BY CollectionInMillions DESC
LIMIT 4, 1;
Question # 5
1. First, we'll retrieve the latest times for all the actors when they made
updates to their online accounts along with their IDs.
SELECT LastUpdatedOn, Id
FROM Actors
INNER JOIN DigitalAssets
ON ActorId = Id;
2. Next, we'll join the result of the previous step with the Cast table
based on the actor ID. The result of this step will be another derived
table whose each row will consist of a movie ID, an actor that was
part of the movie, and time of their latest online activity.
SELECT *
FROM Cast
ON tbl.Id = ActorId;
3. In the third step we can join the derived table of the second step with
the Movies table based on the movie ID. The derived table resulting
from this joining will have the movie name, movie release date, an
actor participating in that movie, and the latest time of that actor's
online activity. By now we have all the necessary columns we need to
compare.
SELECT *
FROM Movies AS m
FROM Cast
ON tbl2.MovieId = m.Id;
4. In the fourth step we'll add a WHERE clause and set the
LastUpdatedColumn to be between the plus/minus 5 days from the
date of the movie release. Also, remember that two actors cast in the
same movie could have posted about their upcoming movie, but we
want to print the name of the movie only once, therefore, we also
add the DISTINCT clause.
AS Actors_Posting_Online_Within_Five_Days_Of_Movie_Release
FROM Movies AS m
FROM Cast
ON tbl2.MovieId = m.Id