In this post, we will understand the difference between WHERE clause and HAVING clause in SQL.
WHERE Clause
It is used to filter the records from the table based on a specific condition.
It can be used without the ‘GROUP BY’ clause.
It can be used with row operations.
It can’t contain the aggregate functions.
It can be used with the ‘SELECT’, ‘UPDATE’, and ‘DELETE’ statements.
It is used before the ‘GROUP BY’ clause if required.
It is used with a single row function such as ‘UPPER’, ‘LOWER’.
HAVING Clause
It is used to filter out records from the groups based on a specific condition.
It can’t be used without the ‘GROUP BY’ clause.
It works with the column operation.
It can contain the aggregate functions.
It can only be used with the ‘SELECT’ statement.
It is used after the ‘GROUP BY’ clause.
It can be used with multiple row functions such as ‘SUM’, ‘COUNT’.
Following is the syntax:
SELECT column1, column2 FROM table1, table2 WHERE [ conditions ] GROUP BY column1, column2 HAVING [ conditions ] ORDER BY column1, column2