GROUP BY and the WHERE Clause

Accessing and Changing Relational Data

Accessing and Changing Relational Data

GROUP BY and the WHERE Clause

You can use a WHERE clause in a query containing a GROUP BY clause. Rows not meeting the conditions in the WHERE clause are eliminated before any grouping is done. For example:

USE pubs
SELECT type, AVG(price)
FROM titles
WHERE advance > $5000
GROUP BY type

Here is the result set:

type                                    
------------ -------------------------- 
business     2.99                       
mod_cook     2.99                       
popular_comp 21.48                      
psychology   14.30                      
trad_cook    17.97                      

(5 row(s) affected)

Only rows with advances greater than $5,000 are included in the groups shown in the query results.

See Also

SELECT

WHERE