Subquery Selections
Quite frequently you want to submit a further select on the result of a select. There are several ways to do that, one way being to nest a select statement inside another. A nested select statement is called a subquery.
Here is an example of a subquery:
SELECT Person.name FROM PersonWHERE Person.lives_in IN ('England', 'USA')AND Person.name NOT IN ( SELECT ListensTo.person_name FROM ListensTo WHERE ListensTo.music_group_name = 'Beatles' );
The simplest way to understand subqueries is to look at them piece by piece, starting from the innermost nested one. In this case, the nested select statement is:
( SELECT ListensTo.name FROM ListensTo WHERE ListensTo.music_group_name = 'Beatles' );
A moment's ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access