The difference is how the query is processed. These two queries are functionally equivalent:
select min(internid), max(internid)
from v_intern
where trainingterm = 'Fall - September' and trainingyear = '2020'
group by trainingTerm, trainingYear;
select min(internid), max(internid)
from v_intern
group by trainingTerm, trainingYear
having trainingterm = 'Fall - September' and trainingyear = '2020';
However, the first is probably going to be more efficient. Why? If there are no indexes, the WHERE
filters the data before aggregation. Less data should improve performance. Under most circumstances, the HAVING
aggregates all the data and then does the filtering. However, some databases might recognize this situation and filter before the aggregation.
If there is an index, the first can use the index to get exactly the data needed for the aggregation.
There is a third version that is similar to the first . . . but subtly different:
select min(internid), max(internid)
from v_intern
where trainingterm = 'Fall - September' and trainingyear = '2020';
That is, the aggregation is not needed. This version always returns one row -- even when no rows match. In that case, the values would be NULL
.