ACC: How to Create and Use SubqueriesID: Q114678
|
Moderate: Requires basic macro, coding, and interoperability skills.
Microsoft Access supports the use of subqueries. A subquery is a select
query inside another select or action query that acts as a critera for a
field. A subquery can refer to a table that is part of the current query,
or to a table that is not part of the current query. Union and crosstab
queries are not supported as subqueries.
NOTE: A demonstration of the technique used in this article can be seen
in the sample file, Qrysmp97.exe. For information about how to obtain
this sample file, please see the following article in the Microsoft
Knowledge Base:
Q182568 ACC97: Microsoft Access 97 Sample Queries Available on
MSL
To create a subquery, enter a valid SQL SELECT statement in the Criteria
cell of the field whose value will be compared with the value selected in
the subquery. The field that the criterion is specified for and the field
that is being selected in the subquery must be of the same data type. You
can also have a subquery in the Field row of the query grid, although it is
not recommended.
A correlated subquery is one in which a column from a table specified in
the FROM clause of the main query is used in the WHERE clause of the
subquery, as demonstrated in the following example:
SELECT * FROM Table1 WHERE FirstName IN (SELECT FirstName FROM Table2
WHERE Table2.LastName=Table1.LastName);
<=(SELECT [UnitPrice] FROM [Products] WHERE [ProductName]= _
"Filo Mix")
For more information about creating subqueries, search the Help Index for "Subqueries," or ask the Microsoft Access 97 Office Assistant.
Additional query words: sub-select
Keywords : kbusage QryOthr
Version : 2.0 7.0 97
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: April 3, 1999