Where vs Having
Where and Having are used as search conditions in a select statement or in a query.
Where clause is the search condition for the rows returned by the select statement.
Having clause is the search condition for a group or an aggregate.
Having clause is usually used with a GROUP BY clause.
Library table:
| ID | TITLE | STUDENT_ID |
|---|---|---|
| 1 | SQL | 3 |
| 2 | T-SQL | 1 |
| 3 | MSSQL | 5 |
| 4 | PHP | 1 |
| 5 | CSS | 2 |
Where
select Title, Student_id
from Library
where Student_id=1;
Results
| Title | Student_id |
|---|---|
| T-SQL | 1 |
| PHP | 1 |
Having
select Student_id, count(*) Nr
from Library
group by Student_id
having count(Student_id)>1 ;
Results
| Student_id | Nr |
|---|---|
| 1 | 2 |