You are on page 1of 3

Using the WHERE and HAVING Clauses in the Same Query (Visual

Database Tools)

Can I merge SQL's GROUP BY and WHERE functions?

The SELECT statement is combined with the GROUP BY clause. The GROUP BY clause
follows the WHERE clause in the query. If any, the GROUP BY clause is put before the
ORDER BY clause in the query.

In some cases, you might want to apply a condition to groups as a whole first before excluding
specific rows from them (using a WHERE clause) (using a HAVING clause).

Similar to a WHERE clause, a HAVING clause only applies to groups as a whole (i.e., to the
rows in the result set that represent groups), but a WHERE clause applies to specific rows. Both
a WHERE clause and a HAVING clause may be present in a query. If so, then

Individual rows in the tables or table-valued objects in the Diagram pane are the first to get the
WHERE clause's application. The WHERE clause's criteria are only satisfied by the rows that
are grouped

The rows in the result set are then subjected to the HAVING clause. The query output only
contains the groups that satisfy the HAVING requirements. Only columns that also appear in a
GROUP BY clause or an aggregate function are eligible to have a HAVING clause applied to
them.

Consider connecting the publishers and titles tables to produce a query that displays the average
book price for a particular group of publishers. You might only be interested in publishers from
the state of California if you only want to examine the average price for that group of publishers.
Even then, you should only look at the average cost if it is greater than $10.

Before computing average prices, you can construct the first criterion by using a WHERE clause,
which eliminates any publishers that are not in California. Given that the second condition is
predicated on the outcomes of grouping and summarizing the data, it needs a HAVING clause.
The final SQL statement may resemble this:

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;

In the Criteria pane of SQL Server Management Studio's Visual Database Tools, you may build
both HAVING and WHERE clauses. If you enter search criteria for a column, it is automatically
included in the HAVING clause. The condition can be changed to a WHERE clause, though.

The same column can be used in both a WHERE clause and a HAVING clause. To accomplish
this, you must add the column twice to the Criteria pane, after which you must specify one
instance as part of the HAVING clause and the other as part of the WHERE clause.

List the groupings that apply to your query. See Group Rows in Query Results for more
information. Add the column on which you wish to base the WHERE condition if it isn't already
there in the Criteria pane. Remove the data column from the Output column unless it is used in
the GROUP BY clause or an aggregate function.
Put the WHERE criteria in the Filter column. The condition is added to the SQL statement's
HAVING clause by the Query and View Designer.

Note; This procedure's example query joins the tables for titles and publishers.

A HAVING clause can be seen in the SQL statement at this point in the query:

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'

Choose Where from the group and summary options list in the Group by column. The Query and
View Designer moved the condition from the SQL statement's HAVING clause to the WHERE
clause.

You might also like