Can you explain the difference between the WHERE and HAVING clauses in SQL?
💡 Model Answer
The WHERE clause filters rows before any grouping or aggregation occurs, while HAVING filters groups after aggregation. For example, if you have a table of sales with columns OrderID, CustomerID, and Amount, you can use WHERE to select only orders over $100: SELECT * FROM Sales WHERE Amount > 100;. If you want to find customers whose total sales exceed $10,000, you need aggregation and HAVING: SELECT CustomerID, SUM(Amount) AS Total FROM Sales GROUP BY CustomerID HAVING SUM(Amount) > 10000;. WHERE works on individual rows; HAVING works on the result of GROUP BY. In practice, use WHERE for row-level filtering and HAVING for group-level filtering.
This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.
🎤 Get questions like this answered in real-time
Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers — invisible to screen sharing.
Get Assisting AI — Starts at ₹500