Beginner

498. JOIN with HAVING

Learn how HAVING can filter grouped results created from joined tables.

📘 Lesson 498 of 728 ⏱ 12 min 🎯 Intermediate

HAVING filters groups after GROUP BY has created them. When used with JOIN, it can filter aggregated results such as customers whose total sales exceed a threshold.

Business reports often need conditions based on calculated totals, counts, or averages rather than individual rows.

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;

Used for high-value customer analysis, sales reporting, branch performance, and financial summaries.

  • HAVING filters groups.
  • WHERE filters rows before grouping.
  • HAVING is commonly used with aggregate functions.
  • JOIN can provide the data being aggregated.
  • Use HAVING for conditions on calculated group results.
What is the difference between WHERE and HAVING?

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

Can HAVING be used with JOIN?

Yes. HAVING can filter aggregated groups created from joined tables.

Have you finished this lesson?

Introduction to SQL – All Lessons