Having clause is used with SQL Queries to give more precise condition for a statement. It is used to mention condition in
Group by based SQL queries, just like
WHERE clause is used with
HAVING clause is,
SELECT column_name, function(column_name)
WHERE column_name condition
GROUP BY column_name
HAVING function(column_name) condition
Example of SQL Statement using
Consider the following Sale table.
Suppose we want to find the customer whose previous_balance sum is more than 3000.
We will use the below SQL query,
FROM sale GROUP BY customer
HAVING sum(previous_balance) > 3000
Result will be,
The main objective of the above SQL query was to find out the name of the customer who has had a previous_balance more than 3000, based on all the previous sales made to the customer, hence we get the first row in the table for customer