Nested Row Queries
This lesson discusses nested queries that return a set of rows.
We'll cover the following...
Nested Row Queries
In this lesson we’ll study nested queries that return rows, allowing the outer query to match on multiple different column values. Furthermore, so far, we have used nested queries only with the WHERE clause, but now we’ll also use them with the FROM clause.
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/32lesson.sh and wait for the MySQL prompt to start-up.
-- The lesson queries are reproduced below for convenient copy/paste into the terminal.-- Query 1SELECT FirstNameFROM ActorsINNER JOIN DigitalAssetsON Id=ActorIdAND MONTH(DoB) = MONTH(LastUpdatedOn)AND DAY(DoB) = DAY(LastUpdatedOn);-- Query 2SELECT FirstNameFROM ActorsWHERE (Id, MONTH(DoB), DAY(DoB))IN ( SELECT ActorId, MONTH(LastUpdatedOn), DAY(LastUpdatedOn)FROM DigitalAssets);--Query 3SELECT ActorId, AssetType, LastUpdatedOn FROM DigitalAssets;-- Query 4SELECT FirstName, AssetType, LastUpdatedOnFROM ActorsINNER JOIN (SELECT ActorId, AssetType, LastUpdatedOnFROM DigitalAssets) AS tblON ActorId = Id;-- Query 5SELECT FirstName, AssetType, LastUpdatedOnFROM ActorsINNER JOIN (SELECT ActorId, AssetType, LastUpdatedOnFROM DigitalAssets) AS tblON ActorId = IdWHERE FirstName = "Kim";-- Query 6SELECT FirstName, AssetType, LastUpdatedOnFROM ActorsINNER JOIN (SELECT ActorId, AssetType, LastUpdatedOnFROM DigitalAssets) AS tblON ActorId = IdWHERE FirstName = "Kim"ORDER BY LastUpdatedOn DESC LIMIT 1;
-
Let’s say we want to find the list of all the actors whose latest update to any of their online accounts was on the day of their birthday. The date of birth for each actor is in the Actors table and the LastUpdatedOn column is in the DigitalAssets table. We can extract the birthday month and day using the MONTH() and DAY() ...