From the course: SQL for Non-Programmers

Unlock the full course today

Join today to access over 23,100 courses taught by industry experts.

Filter on groups of data with HAVING

Filter on groups of data with HAVING - SQL Tutorial

From the course: SQL for Non-Programmers

Filter on groups of data with HAVING

We have conquered the essentials of aggregate functions and grouping in SQL. But there is one additional feature that we should learn in order to take full control of our data when performing calculations. The HAVING clause is used to filter the results of a GROUP BY query based on specific conditions. The HAVING clause is placed after the GROUP BY clause and before any ORDER BY clause. We have been using the WHERE clause often, which is a filter applied to the entire data table, filtering rows before they are grouped. The HAVING clause can be used to filter groups of data after they have been formed. In other words, where filters rows and having filters groups. Besides this key difference, all of the same rules apply for both WHERE and HAVING. This is easiest to understand with an example. Let's try it out in SLS. We can begin with our query for sales performance by state. SELECT customer state sum of order total as sum of orders. COUNT asterisk as order count, average order total as…

Contents