This SQLite post explains how to use the SQLite HAVING clause with syntax and examples.
The SQLite HAVING clause is used in combination with the GROUP BY clause to restrict the groups of returned rows to only those whose the condition is TRUE.
The syntax for the HAVING clause in SQLite is:
The expressions that are not encapsulated within an aggregate function and must be included in the GROUP BY clause.
A function such as sum, count, min, max, or avg functions.
This is the column or expression that the aggregate_function will be used on.
The tables that you wish to retrieve records from. There must be at least one table listed in the FROM clause.
Optional. These are the conditions for the records to be selected.
This is a further condition applied only to the aggregated results to restrict the groups of returned rows. Only those groups whose condition evaluates to TRUE will be included in the result set.
Let's look at an example that shows how to use the HAVING clause with the sum function in SQLite.
You could use the sum function to return the department and the total salaries (for that department). The SQLite HAVING clause will filter the results so that only departments with total salaries greater than $10,000 will be returned.
Let's look at how to use the HAVING clause with the count function in SQLite.
You could use the count function to return the department and the number of employees (for that department) where the state is 'CA'. The SQLite HAVING clause will filter the results so that only departments with more than 20 employees in the state of 'CA' will be returned.
Let's next look at how to use the HAVING clause with the min function in SQLite.
You could also use the min function to return the name of each department and the minimum salary in the department. The SQLite HAVING clause will return only those departments where the minimum salary is less than $20,000.
For example, you could also use the max function to return the name of each department and the maximum salary in the department where the employee's favorite_website is 'AODBA.com'. The SQLite HAVING clause will return only those departments whose maximum salary is greater than or equal to $30,000.