What is the difference between HAVING and WHERE clause?

What is the difference between HAVING and WHERE clause?

HAVING and WHERE clauses are two different functions that can be used in SQL. Their primary function is for use with SELECT queries. The main difference between them is that while the WHERE is used in any SELECT query, the HAVING clause is only used in SELECT queries, those that contain aggregate functions or groups by clause. However, there are some other differences between the two as stated in the table below.

HAVING clause

WHERE clause

Is only used in SELECT queries, which contains aggregate function or group by clause

Can be used in any SELECT query

Specifies a search condition for a group or an aggregate function used in SELECT statement.

Specifies the criteria which individual records must meet to be selected by a query

Cannot be used without the GROUP BY clause.

Can be used without the GROUP BY clause.

Selects rows after grouping

Selects rows before grouping

Can contain aggregate functions

Cannot contain aggregate functions

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.