# WHERE VS HAVING in SQL

**URL:** <https://forum.freecodecamp.org/t/where-vs-having-in-sql/216138>\
**Category:** General\
**Created:** [August 9, 2018, 11:01am UTC](https://forum.freecodecamp.org/t/where-vs-having-in-sql/216138 "2018-08-09T11:01:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zhouxiang19910319](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/zhouxiang19910319/32/98849_2.png) [@zhouxiang19910319](https://forum.freecodecamp.org/u/zhouxiang19910319)\
**Post date:** [August 9, 2018, 11:01am UTC](https://forum.freecodecamp.org/t/where-vs-having-in-sql/216138/1 "2018-08-09T11:01:53Z")

</div>

```auto
CREATE TABLE exercise_logs
    (id INTEGER PRIMARY KEY AUTOINCREMENT,
    type TEXT,
    minutes INTEGER, 
    calories INTEGER,
    heart_rate INTEGER);

INSERT INTO exercise_logs(type, minutes, calories, heart_rate) VALUES ("biking", 30, 115, 110);
INSERT INTO exercise_logs(type, minutes, calories, heart_rate) VALUES ("biking", 10, 45, 105);
INSERT INTO exercise_logs(type, minutes, calories, heart_rate) VALUES ("dancing", 15, 200, 120);
INSERT INTO exercise_logs(type, minutes, calories, heart_rate) VALUES ("dancing", 15, 165, 120);
INSERT INTO exercise_logs(type, minutes, calories, heart_rate) VALUES ("tree climbing", 30, 70, 90);
INSERT INTO exercise_logs(type, minutes, calories, heart_rate) VALUES ("tree climbing", 25, 72, 80);
INSERT INTO exercise_logs(type, minutes, calories, heart_rate) VALUES ("rowing", 30, 70, 90);
INSERT INTO exercise_logs(type, minutes, calories, heart_rate) VALUES ("hiking", 60, 80, 85);

SELECT * FROM exercise_logs;

SELECT type, SUM(calories) AS total_calories FROM exercise_logs GROUP BY type;

SELECT type, SUM(calories) AS total_calories FROM exercise_logs
    GROUP BY type
    HAVING total_calories > 150
    ;

```

In the above code, the last select will list you all the exercises where its total calories burned is more than 150.  
How come I cannot use WHERE instead of having?  
Is it because I am executing something on the new column? (SUM(calories))

When do I know I can use WHERE and when do I know I need to use HAVING instead?

---

<div class="post-metadata">

**Author:** ![zhouxiang19910319](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/zhouxiang19910319/32/98849_2.png) [@zhouxiang19910319](https://forum.freecodecamp.org/u/zhouxiang19910319)\
**Post date:** [August 9, 2018, 11:02am UTC](https://forum.freecodecamp.org/t/where-vs-having-in-sql/216138/2 "2018-08-09T11:02:37Z")

</div>

@camperextraordinaire

(20 character thingy)

---

<div class="post-metadata">

**Author:** ![camperextraordinaire](https://avatars.discourse-cdn.com/v4/letter/c/5f9b8f/32.png) [@camperextraordinaire](https://forum.freecodecamp.org/u/camperextraordinaire)\
**Post date:** [August 9, 2018, 12:35pm UTC](https://forum.freecodecamp.org/t/where-vs-having-in-sql/216138/3 "2018-08-09T12:35:27Z")

</div>

> [@zhouxiang19910319](#):
>
> How come I cannot use WHERE instead of having?

Because you need to filter records of a grouped result.

> [@zhouxiang19910319](#):
>
> Is it because I am executing something on the new column? (SUM(calories))

Yes

> [@zhouxiang19910319](#):
>
> When do I know I can use WHERE and when do I know I need to use HAVING instead?

A WHERE clause is used is filter records from a result. The filter occurs before any groupings are made. A HAVING clause is used to filter values from a group.

You can have situations where you want to filter the selection before the groups are created. For example, if I wanted to list all the exercises where the total calories burned is more than 150 but did not want to include ‘dancing’ as a type, then I would write.

```auto
SELECT type, SUM(calories) as total_calories FROM exercise_logs
WHERE type <> 'dancing'
GROUP BY type
HAVING total_calories > 150; 

```

You will notice the WHERE clause comes before the GROUP BY.

The above would produce;

| type | total\_calories |
| --- | --- |
| biking | 160 |

---

<div class="post-metadata">

**Author:** ![zhouxiang19910319](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/zhouxiang19910319/32/98849_2.png) [@zhouxiang19910319](https://forum.freecodecamp.org/u/zhouxiang19910319)\
**Post date:** [August 10, 2018, 12:35pm UTC](https://forum.freecodecamp.org/t/where-vs-having-in-sql/216138/4 "2018-08-10T12:35:31Z")

</div>

thanks again, for the in depth explanation.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 17, 2021, 8:17pm UTC](https://forum.freecodecamp.org/t/where-vs-having-in-sql/216138/5 "2021-01-17T20:17:35Z")

</div>

Closed on January 17, 2021, 8:17pm UTC


