March 2nd, 2009

How Do Subselects Work? (Part 2 of 3)

Posted by admin in N. Subselects

You need to get a DESCRIPTION of vehicles made by vendors from the STATE of ‘CA’.If you have a VENDOR_TBL:

 

And an INVENTORY_TBL:

Result Table:

Now, instead of an INNER JOIN (first example), you can do a subselect (second example) to get the same results.

          SELECT I.DESCRIPTION                               SELECT DESCRIPTION
             FROM INVENTORY_TBL I,                            FROM INVENTORY_TBL
                       VENDOR_TBL      V              or        WHERE VNDR_ID =
          WHERE I.VNDR_ID = V.VNDR_ID                        (SELECT VNDR_ID
               AND V.STATE = ‘CA’;                                          FROM VENDOR_TBL
                                                                                       WHERE STATE = ‘CA’);

> All RDBMS systems process the inner query first. Then they use that data to run the outer query for the result.

> Remember that subselects can be nested within other subselects, so a subselect (inner query) can be an outer query for another nested subselect.

Comments are closed.

Sorry, the comment form is closed at this time.