SQL Interview: 50 Using WHERE vs HAVING

SQL Interview: 50 Using WHERE vs HAVING

This is a post in the SQL Interview series. These aren’t trick or gotcha questions, they’re just questions designed to scope out a candidate’s knowledge around SQL Server and Azure SQL Database.

Section: Developer Level: Intro

Question:

You need to filter rows being returned by a query.

When should you use a HAVING clause rather than a WHERE clause?

Answer:

A WHERE clause is used to filter the rows from the source (usually a table).

A HAVING clause is used to filter aggregated values after a GROUP BY clause has been applied. It should not be used for basic row filtering that can be achieved in a WHERE clause.

2025-05-24