Search⌘ K

Nested Column Queries

Explore how to construct nested column queries in SQL. Learn to use subqueries with operators ANY, IN, and ALL to filter and join tables, enabling complex data retrieval like identifying actors with specific digital assets or net worth conditions.

We'll cover the following...

Nested Column Queries

In the previous lesson we examined nested queries that returned a single value. In this lesson we’ll see nested queries that return values belonging to the same column.

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/31lesson.sh and wait for the MySQL prompt to start-up.

MySQL
-- The lesson queries are reproduced below for convenient copy/paste into the terminal.
-- Query 1
SELECT * FROM Actors
INNER JOIN DigitalAssets ON ActorId=Id
WHERE AssetType = ANY (SELECT DISTINCT AssetType
FROM DigitalAssets
WHERE AssetType != 'Website');
-- Query 2
SELECT * FROM Actors
INNER JOIN DigitalAssets ON ActorId=Id
WHERE AssetType != 'Website';
-- Query 3
SELECT FirstName, SecondName
FROM Actors
WHERE Id = ANY (SELECT ActorId
FROM DigitalAssets
WHERE AssetType = 'Facebook');
-- Query 4
SELECT FirstName, SecondName
FROM Actors
WHERE Id IN (SELECT ActorId
FROM DigitalAssets
WHERE AssetType = 'Facebook');
-- Query 5
SELECT FirstName, SecondName
FROM Actors
WHERE NetworthInMillions > ALL (SELECT NetworthInMillions
FROM Actors
WHERE FirstName LIKE "j%");
Terminal 1
Terminal
Loading...
  1. We’ll use a slightly contrived example this time. Imagine we want to list all the social media accounts for all the actors, except for their personal websites. From our database schema we know that the table DigitalAssets has a column AssetType, which is ...