T-SQL Tutorial

Difference between Where and Having


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:

IDTITLESTUDENT_ID
1SQL3
2T-SQL1
3MSSQL5
4PHP1
5CSS2




Where

select Title, Student_id
from Library
where Student_id=1;

Results

TitleStudent_id
T-SQL1
PHP1

Having

select Student_id, count(*) Nr
from Library
group by Student_id
having count(Student_id)>1 ;

Results

Student_idNr
12