From the course: SQL for Non-Programmers
Unlock the full course today
Join today to access over 23,400 courses taught by industry experts.
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
Getting started with aggregate functions6m 59s
-
(Locked)
Making aggregate functions work for you5m 35s
-
(Locked)
Group data using GROUP BY6m 26s
-
(Locked)
Filter on groups of data with HAVING4m 36s
-
(Locked)
Challenge: Practice with aggregate functions1m 38s
-
(Locked)
Solution: Aggregate functions solved5m 6s
-
-
-
-