Back

3rd December 2024

#Databases

WHERE vs. HAVING in SQL

Blog image

In SQL both WHERE and HAVING clauses are used to filter data, but context, in which they are used and their functionalities are different. This topic often make beginner developers confused, so I decided to describe these differences in short and concise way.

WHERE

WHERE clause is definitely easier to understand, because I think, that it is used in more queries than HAVING. Remember about WHERE:

Example

SELECT *
FROM products
WHERE price > 100;

In this example WHERE clause retains only these products, whose price is greater than 100.

HAVING

HAVING clause is used in fewer queries, because we use it only to filter groups of records. Remember about HAVING:

Example

SELECT category, AVG(price) AS average_price
FROM products
GROUP BY category
HAVING AVG(price) > 100;

In this example HAVING clause retains only these categories, where the average price is greater than 100.

Combining WHERE and HAVING in a single query

It is of course possible to use both clauses in one query. For example:

SELECT category, COUNT(*) AS products_count
FROM products
WHERE price > 50
GROUP BY category
HAVING COUNT(*) > 5;

In this example WHERE clause keeps only products, whose price is greater than 50. Next, data is grouped by category and filtered by HAVING clause. It will keep only these categories, where products count is greater than 5.

Back to articles