- WHERE clause specifies the criteria which individual records must meet to be selected by a query. It can be used without the GROUP BY clause. HAVING clause cannot be used without the GROUP BY clause.
- WHERE clause selects rows before grouping. HAVING clause selects rows after grouping.
- WHERE clause cannot contain aggregate functions. HAVING clause can contain aggregate functions, such as COUNT() or AVG() or SUM(), or MAX() or MIN().
- Having is just an additional filter to Where clause.
- Where clause applies to the individual rows whereas 'Having' clause is used to test some condition on the group (usually aggregate methods) rather than on individual rows.
- WHERE clause is used for comparing values in the base table whereas HAVING clause can be used for filtering the results of aggregate functions in the result set of the query.
SELECT * FROM tablename WHERE ID > 100
Here's a simple example of a HAVING clause that returns the count of workers with the same last names --
SELECT WorkerLastName, COUNT(WorkerLastName) AS WorkerCount FROM tblWorker
GROUP BY WorkerLastName HAVING COUNT(WorkerLastName) > 1
No comments:
Post a Comment