Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
551 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Written by Andrew Gould
In this tutorial
So far in this tutorial all of our subqueries have returned a single value which we have then compared against a field in a table. It's also possible for a subquery to return multiple values that can be compared against another column, and this part of the series shows you how to use this type of subquery.
It isn't difficult to make a subquery return multiple values; all you need is a select statement which returns multiple records. In the example below the subquery returns a list of birth dates from the table of actors in our database:
SELECT
DirectorName
,DirectorDOB
FROM
tblDirector
WHERE
DirectorDOB <
(SELECT
ActorDOB
FROM
tblActor)
Returning multiple values in a subquery is easy, but the above example will cause an error when we attempt to execute it:
Part of the error message you would see if you attempted to run the above query.
The reason for the error is that the operator by itself can't be used to compare multiple values. To make the above example work we need to add another keyword to the comparison.
One choice of extra keyword we could add to the above query is ALL. In the example shown below we are looking for directors whose date of birth is before that of all of the actors:
SELECT
DirectorName
,DirectorDOB
FROM
tblDirector
WHERE
DirectorDOB < ALL
(SELECT
ActorDOB
FROM
tblActor)
The result of this query is a list of directors who are older than every actor:
These three directors are older than all of the actors.
You'll probably have spotted that this query is the same as asking for directors whose date of birth is less than the minimum date of birth in the actor table. We could have achieved the same result using the MIN function and without using ALL.
The ANY and SOME keywords are interchangeable and allow you to check if the value of a field exceeds any one of the results returned by a subquery. The example below looks for directors whose date of birth is before that of any of the actors:
SELECT
DirectorName
,DirectorDOB
FROM
tblDirector
WHERE
DirectorDOB < ANY
(SELECT
ActorDOB
FROM
tblActor)
In plain English what we'll return is a list of directors who are older than the youngest actor:
As it turns out, all of the directors are older than at least one of the actors.
You can use the IN keyword to test if the value of a field matches an item in the results of a subquery. The example below shows a list of directors whose name also appears in the actor table:
SELECT
DirectorName
FROM
tblDirector
WHERE
DirectorName IN
(SELECT
ActorName
FROM
tblActor)
The results of the query are shown below:
Seven people in the database have been both an actor and a director.
You can also use NOT IN to find all of the values in a field which do not have a match in the results of a subquery.
The final part of this series looks at how to use correlated subqueries. Not for the faint-hearted!
You can learn more about this topic on the following Wise Owl courses:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.