Using HAVING and WHERE Clauses in the Same Query
In some instances, you might want to exclude individual rows from groups (using a WHERE clause) before applying a condition to groups as a whole (using a HAVING clause).
A HAVING clause is like a WHERE clause, but applies only to groups as a whole (that is, to the rows in the
- The WHERE clause is applied first to the individual rows in the tables or table-structured objects in the diagram pane.. Only the rows that meet the conditions in the WHERE clause are grouped.
- The HAVING clause is then applied to the rows in the result set that are produced by grouping. Only the groups that meet the HAVING conditions appear in the query output. You can apply a HAVING clause only to columns that also appear in the GROUP BY clause or in an
aggregate function .
For example, imagine that you are joining the titles
and publishers
tables to create a query showing the average book price for a set of publishers. You want to see the average price for only a specific set of publishers — perhaps only the publishers in the state of California. And even then, you want to see the average price only if it is over $10.00.
You can establish the first condition by including a WHERE clause, which discards any publishers that are not in California, before calculating average prices. The second condition requires a HAVING clause, because the condition is based on the results of grouping and summarizing the data. The resulting
SELECT titles.pub_id, AVG(titles.price)
FROM titles INNER JOIN publishers
ON titles.pub_id = publishers.pub_id
WHERE publishers.state = 'CA'
GROUP BY titles.pub_id
HAVING AVG(price) > 10
You can create both HAVING and WHERE clauses in the Grid pane of the Query Designer. By default, if you specify a
You can create a WHERE clause and HAVING clause involving the same column. To do so, you must add the column twice to the Grid pane, then specify one instance as part of the HAVING clause and the other instance as part of the WHERE clause.
To specify a WHERE condition in an aggregate query
- Specify the groups for your query. For details, see Grouping Rows in Query Results.
- If it is not already in the Grid pane, add the column on which you want to base the WHERE condition.
- Clear the Output column unless the data column is part of the GROUP BY clause or included in an aggregate function.
- In the Criteria column, specify the WHERE condition. The Query Designer adds the condition to the HAVING clause of the SQL statement.
Note The query shown in the example for this procedure joins two tables,
titles
andpublishers
.At this point in the query, the SQL statement contains a HAVING clause:
SELECT titles.pub_id, AVG(titles.price) FROM titles INNER JOIN publishers ON titles.pub_id = publishers.pub_id GROUP BY titles.pub_id HAVING publishers.state = 'CA'
- In the Group By column, select Where from the list of group and summary options. The Query Designer removes the condition from the HAVING clause in the SQL statement and adds it to the WHERE clause.
The SQL statement changes to include a WHERE clause instead:
SELECT titles.pub_id, AVG(titles.price) FROM titles INNER JOIN publishers ON titles.pub_id = publishers.pub_id WHERE publishers.state = 'CA' GROUP BY titles.pub_id
See Also
Grouping Rows in Query Results | Specifying Conditions for Groups | Summary and Grouping Behavior in the Query Designer