Subqueries

Team-Fly    

Macromedia® DreamWeaver® MX Unleashed
By Matthew Pizzi, Zak Ruvalcaba
Table of Contents
Chapter 27.  SQL Primer


Sometimes it may not be possible to retrieve the results that you need from a simple Select statement. At times, you might need to create a Select statement and compare the results to that of another statement. In that case, you would want to use subqueries. A subquery is a query that is nested inside another query. There are two types of subqueries that you can use:

  • The In Operator

  • The Embedded Select Statement

The In Operator

The In operator is used in a Select statement primarily to specify a list of values to be used with a primary query. A classic example is if you wanted to find all your customers who lived in California. You could write a Select statement using the In operator to accomplish that:

 Select * From Customers Where CustomerState In ("Ca")  

This statement would effectively return all the customers who live in the state of California, assuming you had a field for state.

The Embedded Select Statement

An Embedded Select statement is used when you want to perform a query within the where clause of a primary query. Suppose you wanted to see a list of customers who have completed orders for the week.

 Select * From Customers Where CustomerID In  (Select Distinct CustomerID From Orders) 

    Team-Fly    
    Top


    Macromedia Dreamweaver MX Unleashed
    Macromedia Dreamweaver MX 2004 Unleashed
    ISBN: 0672326310
    EAN: 2147483647
    Year: 2002
    Pages: 321

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net