What is the difference between WHERE and HAVING?
The examples use the students table from our MySQL tutorial: id, name, city, marks, course_id.
| Point | WHERE | HAVING |
|---|---|---|
| Filters | Rows | Groups |
| Runs | Before GROUP BY | After GROUP BY |
| Aggregate functions | Not allowed | Allowed |
SELECT city, COUNT(*) AS students
FROM students
WHERE marks > 50
GROUP BY city
HAVING COUNT(*) > 1;
Tutorials Class - Output Window
| city | students |
|---|---|
| Delhi | 2 |
First WHERE removes Neha (45). Then the rows are grouped by city. Only Delhi has more than one student left. See GROUP BY and HAVING.
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |