Database Toolbox |
|
Subquery
A subquery restricts query results by using values found in other tables. For example, use a subquery if you want to know sales volumes for the building blocks product, but the sales volumes and product descriptions are not in the same table. The subquery uses a field common to both tables to relate the tables.
In Advanced query options, click Where; the Where Clauses dialog box appears. In the Where Clauses dialog box, click Subquery; the Subquery dialog box appears.
- From Tables, select the table that contains the association you need for the query results.
- From Fields, select the field that is common to both tables.
- In the Subquery Where Clauses area, specify the associations between the fields that are in different tables, and click Apply.
- The subquery appears in the Current subquery Where clauses area and in the SQL Subquery Statement area.
- You can make changes to subquery clauses.
- To edit a subquery, select the clause from the Current subquery Where clauses area and click Edit. Make changes to the subquery fields and click Apply.
- To delete a subquery, select the clause from the Current subquery Where clauses area and click Delete. Use Ctrl+click or Shift+click to select multiple clauses to delete.
- Click OK to close the Subquery dialog box, which updates the Where Clauses dialog box with the subquery you created in the Subquery dialog box. Or click Cancel to close the Subquery dialog box without updating the Where Clauses dialog box.
- Click OK to close the Where Clauses dialog box.
- The SQL statement in the Visual Query Builder dialog box includes the subquery.
Subquery Where Clauses
In the Subquery Where clauses area of the Subquery dialog box, specify the associations between fields that are in different tables.
- From Fields, select the entry whose value you want to specify.
- Specify the condition for the selected field. For more information, see Condition as described for where clauses.
- Select an operator to add another condition. For more information, see Operator as described for where clauses.
- To create the clause, click Apply.
- The subquery appears in the Current subquery Where clauses area and in the SQL subquery statement area.
Example Using Subquery
This example retrieves sales volumes for the product whose description is Building Blocks. The salesVolume
table has sales volumes and a stock number field, but not a product description field. Another table, productTable
, has the product description and the stock number, but not the sales volumes. The subquery looks at productTable
to get the stock number for the product whose description is Building Blocks. The main query gets the s
ales volume values for that stock number from the salesVolume
table.
In the Subquery dialog box:
- From Tables, select
productTable
.
- From Fields, select
stockNumber
.
- This begins creating the subquery to retrieve
stockNumber
from productTable
; it appears in the SQL subquery statement area.
- Specify the condition that instructs the query to select the products whose
productDescription
is Building Blocks
.
- In Subquery Where clauses, select
productDescription
from Fields.
- For Condition, select Relation.
- From the drop-down list to the right of Relation, select
=
.
- In the field to the right of the drop-down list, type
'Building Blocks'
(include the single quotation marks).
- Click Apply.
- Click OK.
- The Subquery dialog box closes.
- In the Where Clauses dialog box, click Apply.
- This updates the Current clauses area, using the subquery criteria specified in steps 1 through 5.
- In the Where Clauses dialog box, click OK.
- This closes the Where Clauses dialog box and updates the SQL statement in the Visual Query Builder dialog box.
- In the Visual Query Builder dialog box:
- Create the main query that selects sales volumes from the
salesVolume
table.
- Assign the MATLAB workspace variable and click Execute to run the
query.
The results show sales volumes for the item whose product description is Building Blocks
, although the product description itself is not included in the results.
You can select multiple tables in the Visual Query Builder dialog box and create a Where clause to perform the same query. That method also allows you to include the product description in the results. See Example Creating Queries for Results from Multiple Tables for more information.
See Also
| Current Clauses | | Group By | |
© 1994-2005 The MathWorks, Inc.