Provided by Allen Browne, March 2007. Updated February 2009.
Discovering subqueries is one of those "Eureka!" moments. A new landscape opens in front of you, and you can do really useful things such as:
Read a value from the previous or next record in a table.
Select just the TOP (or most recent) 5 scores per client.
Choose the people who have not paid/ordered/enrolled in a period.
Express a value as a percentage of the total.
Avoid inflated totals where a record is repeated (due to multiple related records.)
Filter or calculate values from other tables that are not even in the query.
A subquery is a SELECT query statement inside another query.
As you drag fields and type expressions in query design, Access writes a sentence describing what you asked for. The statement is in SQL (see'quell) - Structured Query Language - the most common relational database language, also used by MySQL, SQL Server, Oracle, DB2, FoxPro, dBase, and others.
If SQL is a foreign language, you can mock up a query like the subquery you need, switch it to SQL View, copy, and paste into SQL View in your main query. There will be some tidying up to do, but that's the simplest way to create a subquery.
The best way to grasp subqueries is to look at examples of how to use them.
A sales rep. wants to hound customers who have not placed any orders in the last 90 days:
The main query selects two fields (ID and Company) from the Customers table. It is limited by the WHERE clause, which contains the subquery.
The subquery (everything inside the brackets) selects Order ID from the Orders table, limited by two criteria: it has to be the same customer as the one being considered in the main query, and the Order Date has to be in the last 90 days.
When the main query runs, Access examines each record in the Customers table. To decide whether to include the customer, it runs the subquery. The subquery finds any orders for that customer in the period. If it finds any, the customer is excluded by the NOT EXISTS.
The subquery goes in brackets, without a semicolon of its own.
The Orders table is not even in the main query. Subqueries are ideal for querying about data in other tables.
The subquery does not have the Customers table in its FROM clause, yet it can refer to values in the main query.
Subqueries are useful for answering questions about what data exists or does not exist in a related table.
Periodically, they read the meter at your house, and send a bill for the number of units used since the previous reading. The previous reading is a different record in the same table. How can they query that?
A subquery can read another record in the same table, like this:
The main query here contains 4 fields: the primary key, the reading date, the meter value at that date, and a fourth field that is the value returned from the subquery.
The subquery returns just one meter reading (TOP 1.) The WHERE clause limits it to the same address, and a previous date. The ORDER BY clause sorts by descending date, so the most recent record will be the first one.
Since there are two copies of the same table, you must alias one of them. The example uses Dupe for the duplicate table, but any name will do.
If the main query displays the result, the subquery must return a single value only. You get this error if it returns multiple values:
At most one record can be returned by this subquery.
Even though we asked for TOP 1, Access will return multiple records if there is a tie, e.g. if there were two meter readings on the same date. Include the primary key in the ORDER BY clause to ensure it can decide which one to return if there are equal values.
The main query will be read-only (not editable.) That is always the case when the subquery shows a value in the main query (i.e. when the subquery is in the SELECT clause of the main query.)
You want the three most recent orders for each client. Use a subquery to select the 3 top orders per client, and use it to limit which orders are selected in the main query:
Since we have two copies of the same table, we need the alias.
Like EXISTS in the first example above, there is no problem with the subquery returning multiple records. The main query does not have to show any value from the subquery.
Adding the primary key field to the ORDER BY clause differentiates between tied values.
A Totals query easily gives you a total for the current month, but to get a year-to-date total or a total from the same month last year means another calculation from the same table but for a different period. A subquery is ideal for this purpose.
The subquery uses the same tables, so aliases them as A (for Orders) and OD (for Order Details.)
The date criteria are designed so you can easily modify them for financial years rather than calendar years.
Even with several thousand records in Order Details, the query runs instantaneously.
The Unmatched Query Wizard (first dialog when you create a new query) can help you identify records in one table that have no records in another. But if you try to delete the unmatched records, Access may respond with, Could not delete from specified tables.
An alternative approach is to use a subquery to identify the records in the related table that have no match in the main table. This example deletes any records in tblInvoice that have no matching record in the tblInvoiceDetail table:
This example uses a subquery to de-duplicate a table. "Duplicate" is defined as records that have the same values in Surname and FirstName. We keep the one that has the lowest primary key value (field ID.)
Nulls don't match each other, so if you want to treat pairs of Nulls as duplicates, use this approach:
Instead of creating a query into another query, you can summarize data with a subquery.
This example works with Northwind, to show how many distinct clients bought each product:
Since subqueries can look up tables that are not in the main query, they are very useful for filtering forms and reports.
A Filter or WhereCondition is just a WHERE clause. A WHERE clause can contain a subquery. So, you can use a subquery to filter a form or report. You now have a way to filter a form or report on fields in tables that are not even in the RecordSource!
In our first example above, the main query used only the Customers table, and the subquery filtered it to those who had no orders in the last 90 days. You could filter the Customers form in exactly the same way:
'Create a subquery as a filter string. strWhere = "NOT EXISTS (SELECT Orders.OrderID FROM Orders " & _ "WHERE (Orders.CustomerID = Customers.CustomerID) AND (Orders.OrderDate > Date() - 90))" 'Apply the string as the filter of the form that has only the Customers table. Forms!Customers.Filter = strWhere Forms!Cusomters.FilterOn = True 'Or, use the string to filter a report that has only the Customers table. DoCmd.OpenReport "Customers", acViewPreview, , strWhere
This technique opens the door for writing incredibly powerful searches. Add subqueries to the basic techniques explained in the Search form article, and you can offer a search where the user can select criteria based on any related table in the whole database.
The screenshot below is to whet your appetite for how you can use subqueries. The form is unbound, with each tab collecting criteria that will be applied against related tables. The final RESULTS tab offers to launch several reports which don't even have those tables. It does this by dynamically generating a huge WhereCondition string that consists of several subqueries. The reports are filtered by the subqueries in the string.
These articles also illustrate the use of subqueries:
Now you know how powerful subqueries are, you will also want to know their limitations. Surviving subqueries gives insight to help you trouble-shoot them.
|Home||Index of tips||Top|