Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

sql
having
where

HAVING vs. WHERE in SQL

Educative Answers Team

​In SQL, the HAVING and WHERE clauses are both used to impose a condition. However, HAVING is used to filter out results from aggregated data, whereas WHERE is used to add conditions to individual rows.

Code

Suppose that we have the following tables:

1. customer_details

ID Fname Lname Age
1 Dan Phil 35
2 Hannah King 28
3 Tim Brown 40

2. Order_details

Cust_ID Car Price(millions)
3 Honda Civic 4
1 Toyota Camry 6
1 Ford Fiesta 7
2 Audi A8 50
3 Ford Edge 10

Code using HAVING to find customer ID’s with total purchase amount exceeding 13 million

SELECT Cust_ID, 
SUM(Price) AS Total 
FROM order_details
GROUP BY Cust_ID
HAVING total > 13

Code using WHERE to find customers who are less than 40 years old

SELECT Fname, Lname 
FROM customer_details
WHERE Age < 40

RELATED TAGS

sql
having
where
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring