How Do Subselects Work? (Part 1 of 3)
A subselect or subquery is a nested select inside of another select.Sometimes you can’t answer one question until you answer another question. You might think, I need to get a DESCRIPTION of vehicles made by vendors from the STATE of ‘CA’. At first glance you think, I will first find all vendors from ‘CA’ and then I will use that information to get the vehicle DESCRIPTION. This would be a two step process.
Your second thought might be to do an INNER JOIN between these two tables. This would be a simpler one step process. The first example is the JOIN format and the second example is the WHERE format for a join.
SELECT I.DESCRIPTION SELECT I.DESCRIPTION
FROM INVENTORY_TBL I FROM INVENTORY_TBL I,
INNER JOIN VENDOR_TBL V or VENDOR_TBL V,
ON I.VNDR_ID = V.VNDR_ID WHERE I.VNDR_ID = V.VNDR_ID
WHERE V.STATE = ‘CA’; AND V.STATE = ‘CA’;
> Go on and you will see how to use a subselect.
If you have a VENDOR_TBL:
And an INVENTORY_TBL:
Result Table:


