Mode Analytics
Sign In
Sign Up

SQL Editor

Query your connected data sources with SQL

Notebooks

Use Python and R for advanced analysis

Reports

Build customizable, sharable reports


Compare plans Dashboards Security Embedded analytics Customers Integrations

Gallery Learn SQL Learn Python

Mode Business Blog Forum

Mode Analytics

Mode Business
Product

SQL Editor

Query your connected data sources with SQL

Notebooks

Use Python and R for advanced analysis

Reports

Build customizable, sharable reports

Compare plans Dashboards Embedded analytics Customers Integrations Security
Resources

Gallery

Explore example analysis and visualizations

Learn SQL

Answer data questions with SQL

Learn Python

Learn Python for business analysis

Documentation Getting started webinar Forum Blog Contact us
Sign In Sign Up – Free Forever

SQL Tutorial

Basic SQL

Intermediate SQL

  • SQL Aggregate Functions
  • SQL COUNT
  • SQL SUM
  • SQL MIN/MAX
  • SQL AVG
  • SQL GROUP BY
  • SQL HAVING
  • SQL DISTINCT
  • SQL CASE
  • SQL Joins
  • SQL INNER JOIN
  • SQL Outer Joins
  • SQL LEFT JOIN
  • SQL RIGHT JOIN
  • SQL Joins Using WHERE or ON
  • SQL FULL OUTER JOIN
  • SQL UNION
  • SQL Joins with Comparison Operators
  • SQL Joins on Multiple Keys
  • SQL Self Joins

Advanced SQL

SQL Analytics Training

Python Tutorial

A complete analytical toolkit, free forever

SQL, Python, R, and built-in charts, all in one place.

Sign Up – Free Forever

SQL HAVING

Starting here? This lesson is part of a full-length tutorial in using SQL for Data Analysis. Check out the beginning.

The SQL HAVING clause

In the previous lesson, you learned how to use the GROUP BY clause to aggregate stats from the Apple stock prices dataset by month and year.

However, you’ll often encounter datasets where GROUP BY isn’t enough to get what you’re looking for. Let’s say that it’s not enough just to know aggregated stats by month. After all, there are a lot of months in this dataset. Instead, you might want to find every month during which AAPL stock worked its way over $400/share. The WHERE clause won’t work for this because it doesn’t allow you to filter on aggregate columns—that’s where the HAVING clause comes in:

SELECT year,
       month,
       MAX(high) AS month_high
  FROM tutorial.aapl_historical_stock_price
 GROUP BY year, month
HAVING MAX(high) > 400
 ORDER BY year, month

Note: HAVING is the “clean” way to filter a query that has been aggregated, but this is also commonly done using a subquery, which you will learn about in a later lesson.

Query clause order

As mentioned in prior lessons, the order in which you write the clauses is important. Here’s the order for everything you’ve learned so far:

  1. SELECT
  2. FROM
  3. WHERE
  4. GROUP BY
  5. HAVING
  6. ORDER BY
Next Tutorial SQL DISTINCT

Looks like you've got a thing for cutting-edge data news.

So do we. Stay in the know with our regular selection of the best analytics and data science pieces, plus occasional news from Mode. Sign up here and we'll keep you posted:

Thanks! Keep an eye on your inbox for the next newsletter!

Contact

Request a Demo

hi@modeanalytics.com

415-689-7436

208 Utah Street, Suite 400
San Francisco CA 94103

Product

Compare plans

Mode Business

SQL editor

Notebooks

Reports

Dashboards

Embedded analytics

Mode for Slack

Customers

Integrations

Security

Resources

Help & support

Getting started webinar

Forum

Learn SQL

Learn Python

Data jobs

Data news

Drag and drop webinar

Scaling analytics webinar

Retention analytics ebook

Salesforce CRM ebook

Company

About

Careers

Blog

© Mode Analytics, Inc. 2018 Terms of Service Privacy Policy