Difference Between WHERE And HAVING Clause In MySQL

What Is WHERE Clause?

WHERE Clause in MySQL is a keyword used to specify the exact criteria of data or rows that will be affected by the specified SQL statement.  In other words, WHERE Clause is used to specify a condition while fetching the data from a single table or by joining with multiple tables. If the given condition is satisfied, then only it returns a specified value from the table. The WHERE clause can be used with SQL statements like INSERT, UPDATE, SELECT and DELETE to filter records and perform various operations on the data.

What You Need To Know About WHERE Clause

  • The WHERE clause specifies the criteria which individual records must meet to be selected by a query. It can be used without the GROUP BY clause.
  • Apart from SELECT queries, you can use WHERE clause with, SELECT, UPDATE and DELETE statement.
  • WHERE clause is used to filter the records from the table based on the specified condition.
  • WHERE clause is processed right after FROM clause in the logical order of query processing, which means it is processed before GROUP BY.
  • WHERE clause implements in row operations.
  • The WHERE clause cannot contain aggregate functions.
  • WHERE clause is used to impose condition on SELECT statement as well as single row function and is used before GROUP BY.
  • WHERE clause is used with single row function like UPPER, LOWER etc.

What Is HAVING Clause?

HAVING Clause is used with SQL queries to give more precise condition for a statement. It is used to filter the records from the groups based on the given condition in the HAVING Clause. A HAVING Clause specifies that an SQL SELECT statement must only return rows where aggregate values meet the specified conditions. It places conditions on groups created by the GROUP BY Clause. Those groups that satisfy the given condition will appear in the final result. The HAVING Clause must follow the GROUP BY Clause in a query and must also precede the ORDER BY Clause if used. HAVING Clause can only be used with SELECT statement.

What You Need To Know About Having Clause

  • The HAVING clause cannot be used without the GROUP BY Clause.
  • HAVING clause can only be used with SELECT statement.
  • HAVING clause is used to filter record from the groups based on the specified condition.
  • HAVING clause is executed after groups are created.
  • HAVING clause implements in column operation.
  • The HAVING clause can contain aggregate functions.
  • HAVING clause is used to impose condition on GROUP function and is used after GROUP BY in the query.
  • HAVING clause is used with multiple row function like SUM, COUNT etc.

Difference Between WHERE And HAVING Clause In Tabular Form

BASIS OF COMPARISON WHERE CLAUSE HAVING CLAUSE
Description The WHERE clause specifies the criteria which individual records must meet to be selected by a query. It can be used without the GROUP BY clause.   The HAVING clause cannot be used without the GROUP BY Clause.  
Compatible Statements Apart from SELECT queries, you can use WHERE clause with, SELECT, UPDATE and DELETE statement.   HAVING clause can only be used with SELECT statement.  
Use WHERE clause is used to filter the records from the table based on the specified condition.   HAVING clause is used to filter record from the groups based on the specified condition.  
Execution WHERE clause is processed right after FROM clause in the logical order of query processing, which means it is processed before GROUP BY.   HAVING clause is executed after groups are created.  
Implementation WHERE clause implements in row operations.   HAVING clause implements in column operation.  
Aggregate Function The WHERE clause cannot contain aggregate functions.   The HAVING clause can contain aggregate functions.  
Function WHERE clause is used to impose condition on SELECT statement as well as single row function and is used before GROUP BY.   HAVING clause is used to impose condition on GROUP function and is used after GROUP BY in the query.  
Row Function WHERE clause is used with single row function like UPPER, LOWER etc.   HAVING clause is used with multiple row function like SUM, COUNT etc.