LogoInterview Master

SQL Learning Hub

SQL HAVING Clause

Understanding the SQL HAVING Clause

The HAVING clause in SQL is specifically designed to filter groups based on a specified condition, typically involving an aggregate function. It works in tandem with the GROUP BY clause, allowing you to apply criteria *after* rows have been grouped and aggregate calculations (like COUNT, SUM, AVG, MIN, MAX) have been performed.

Think of it this way: WHERE filters individual rows before they enter the grouping process, while HAVING filters the final summary rows produced by GROUP BY.

Basic Syntax

Key HAVING Concepts & Interview Questions

  • What is the fundamental difference between the WHERE clause and the HAVING clause? When must you use HAVING?
  • Can HAVING be used in a query without a GROUP BY clause? (Technically yes in some DBs, acting like WHERE, but it's unconventional and confusing).
  • Provide an example where you would filter groups based on their COUNT or SUM.
  • How does the logical processing order of SQL clauses (FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY) explain the difference between WHERE and HAVING?
  • Can you use columns that are *not* aggregated and *not* in the GROUP BY clause within the HAVING condition? (Generally no, similar to the SELECT list rule).

HAVING vs. WHERE: The Crucial Difference

WHERE filters ROWS, HAVING filters GROUPS

  • WHERE Clause: Operates on individual rows. Filters data BEFORE any grouping or aggregation occurs. You cannot use aggregate functions (like COUNT(*), SUM(views)) directly in the WHERE clause because these functions operate on groups, which haven't been formed yet.
  • HAVING Clause: Operates on the summary rows created by GROUP BY. Filters data AFTER grouping and aggregation have occurred. Its primary purpose is to filter based on the results of aggregate functions.

Using Only HAVING

Let's find users who have made more than 5 interactions (likes, comments, etc.).

We must use HAVING COUNT(*) > 5 because the condition depends on the result of the COUNT(*) aggregate function, which is only available after the GROUP BY operation.

Using Both WHERE and HAVING

Combine both clauses for powerful, multi-stage filtering. Recall that WHERE filters rows before grouping, while HAVING filters groups after grouping.

Query Flow:

  1. Join Videos and Users.
  2. WHERE keeps only rows for videos uploaded in 2023.
  3. GROUP BY groups the remaining rows by user_id.
  4. COUNT calculates the number of videos in each user group.
  5. HAVING keeps only those user groups where the count is 2 or more.
  6. SELECT returns the user ID and their count.
  7. ORDER BY sorts the final result.

Practical HAVING Examples with TokTuk

Finding Videos with High Average Views

Identify videos whose average interaction timestamp is recent (indicating ongoing engagement).

This query helps find content that is still actively being engaged with. We group interactions by video and use HAVING to filter for videos where the maximum (latest) interaction timestamp falls within the last week.

Identifying Users with Significant Total Views

This identifies the platform's "power creators" based on their cumulative view count.

Finding Categories (e.g., Users) Meeting Multiple Criteria

Apply multiple aggregate conditions in the HAVING clause.

This finds users who are both reasonably prolific (at least 3 videos) and consistently popular (average views > 1000).

Advanced HAVING Scenarios

Filtering Based on Ratios or Expressions of Aggregates

Conditions in HAVING can involve calculations based on multiple aggregate functions.

This uses conditional aggregation (COUNT with CASE) and then filters groups based on the ratio between the calculated like and comment counts.

Using Subqueries within HAVING

While possible, comparing an aggregate result to a value derived from a subquery within HAVING can sometimes be complex or less efficient than using CTEs or joining pre-aggregated results.

This compares each user's average view count against the global average calculated in a subquery. While valid, alternative approaches using window functions or CTEs might be preferred for readability or performance in some databases.

Summary

The HAVING clause is the designated tool in SQL for filtering based on the results of aggregate functions applied to groups:

  • It filters groups after the GROUP BY clause has formed them and aggregate functions have been computed.
  • It contrasts with the WHERE clause, which filters individual rows before grouping.
  • Conditions in HAVING typically involve aggregate functions like COUNT, SUM, AVG, MIN, MAX.
  • You can use multiple conditions combined with AND or OR within a single HAVING clause.
  • It allows you to answer questions like "Which categories have a total sum greater than X?" or "Which groups have an average value below Y?".

Correctly understanding and applying the distinction between WHERE and HAVING is crucial for writing accurate and efficient aggregate queries in SQL, and it's a common point of discussion in technical interviews.

Test Your Knowledge

Take a quick quiz to reinforce what you've learned about SQL HAVING Clause

Ready for hands-on SQL practice?

We have 200+ questions on real companies and real products.

Find a question to practice
Dawn AI