Solution Practice Set 1
Get the solution to the basic exercise of viewing the information from a database.
We'll cover the following...
Solution Practice Set 1
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 NameFROM MoviesORDER BY CollectionInMillions DESCLIMIT 3;-- Question # 2, Query 1SELECT * FROM Movies aINNER JOIN Movies b;-- Question # 2, Query 2SELECT concat(a.FirstName," ",b.SecondName)FROM Actors aINNER JOIN Actors bON a.SecondName = b.SecondNameWHERE a.ID != b.ID;-- Question # 2, Query 3SELECT DISTINCT concat(a.FirstName," ",b.SecondName)AS Actors_With_Shared_SecondNamesFROM Actors aINNER JOIN Actors bON a.SecondName = b.SecondNameWHERE a.Id != b.Id;-- Question # 3, Query 1SELECT b.SecondNameFROM Actors aINNER JOIN Actors bON a.SecondName = b.SecondNameWHERE a.Id != b.IdGROUP BY b.SecondName;-- Question # 3, Query 2SELECT a.SecondName,COUNT(DISTINCT a.FirstName)FROM Actors aINNER JOIN Actors bON a.SecondName = b.SecondNameWHERE a.Id != b.Idgroup by a.SecondName;-- Question # 3, Query 3SELECT a.SecondName AS Actors_With_Shared_SecondNames,COUNT(DISTINCT a.Id) AS CountFROM Actors aINNER JOIN Actors bON a.SecondName = b.SecondNameWHERE a.Id != b.Idgroup by a.SecondName;-- Question # 4, Query 1SELECT DISTINCT CONCAT(FirstName, " ", SecondName) AS Actors_Acted_In_Atleast_1_MoviesFROM ActorsINNER JOIN CastON Id = ActorId;-- Question # 5, Query 1SELECT Id, CONCAT(FirstName, " ", SecondName) AS Actors_With_No_MoviesFROM ActorsWHERE Id NOT IN (SELECT IdFROM ActorsINNER JOIN CastON Id = ActorId);-- Question # 5, Query 2SELECT *FROM ActorsLEFT JOIN CastON Id = ActorId;-- Question # 5, Query 3SELECT CONCAT(FirstName, " ", SecondName) AS Actors_With_No_MoviesFROM ActorsLEFT JOIN CastON Id = ActorIdWHERE MovieId IS NULL;
Question # 1
Write a query that prints the top three movies by box office collection?
This asks for us to print the top three movies and should hint towards sorting. The sort key should be the column CollectionInMillions. However, remember that by default, ORDER BY sorts in ascending order so we’ll need to sort in descending order. The last piece to the puzzle is to apply the LIMIT clause so that we only retrieve the top three rows.
SELECT Name
FROM Movies
ORDER BY CollectionInMillions DESC
LIMIT 3;
Question # 2
Can you write a query to determine if any two actors share the same second name?
The information we want to extract is contained within the Actors table. However, we need a way to compare the second name of the first actor with all the other actors in the table except with itself. How can you make the comparison? The answer is using a self join.
Whenever you hear yourself thinking in terms of picking up a row from a table and comparing it to another row from the same table or another table, you are looking for a join. Without further ado, we’ll perform ...