Beginner

455. JOIN with HAVING

Learn how HAVING filters grouped results created after a JOIN.

📘 Lesson 455 of 728 ⏱ 12 min 🎯 Intermediate

HAVING filters groups after GROUP BY has created them and is commonly used with aggregate functions.

Business questions often require filtering summarized joined data, such as customers whose total sales exceed a specific amount.

Find customers whose total order value is greater than ₹100,000.

SQL
SELECT
    c.CustomerName,
    SUM(o.OrderAmount) AS TotalSales
FROM Customers c
INNER JOIN Orders o
    ON c.CustomerID = o.CustomerID
GROUP BY c.CustomerName
HAVING SUM(o.OrderAmount) > 100000;

HAVING with JOIN is useful for sales targets, customer segmentation, branch performance, and financial reporting.

  • HAVING filters groups.
  • WHERE filters individual rows before grouping.
  • HAVING is commonly used with aggregate functions.
  • JOIN can provide the data being aggregated.
  • HAVING is useful for business threshold analysis.
What is the difference between WHERE and HAVING?

WHERE filters rows before grouping, while HAVING filters groups after aggregation.

Can HAVING be used with JOIN?

Yes. HAVING can filter aggregate results produced from joined tables.

Have you finished this lesson?

Introduction to SQL – All Lessons