Jump to content

HAVING vs WHERE in SQL

Go to solution Solved by Limecat86,
1 hour ago, mrchow19910319 said:

--SNIP--

so the difference between HAVING and WHERE is that WHERE can only apply to one element inside the database? and HAVING coming after GROUP BY while WHERE is always before GROUP BY? 

am I understanding this correctly? 

Yes that's correct. HAVING operates on aggregate functions (ex. COUNT) on grouped results. WHERE operates on each individual row.

Found some very good explanations on stackoverflow: https://stackoverflow.com/questions/9253244/sql-having-vs-where 

```
CREATE table table_name
(
  name TEXT,
  subject TEXT,
  number_grade INTEGER
  );

INSERT INTO table_name (name, subject, number_grade) VALUES ("person_a","math",81);
INSERT INTO table_name (name, subject, number_grade) VALUES ("person_a","science",75);
INSERT INTO table_name (name, subject, number_grade) VALUES ("person_b","math",76);
INSERT INTO table_name (name, subject, number_grade) VALUES ("person_b","science",90);
INSERT INTO table_name (name, subject, number_grade) VALUES ("person_c","math",81);
INSERT INTO table_name (name, subject, number_grade) VALUES ("person_c","science",100);
INSERT INTO table_name (name, subject, number_grade) VALUES ("person_c","chemistry",90);

SELECT * FROM table_name;
```

Name of the game is to find everyone who had a grade that is bigger than 80 for every subject they took. 

I tried this: 

```
SELECT name FROM table_name 
WHERE number_grade > 80
GROUP BY NAME;
```

and it gives me all the names in the table. 
and the correct answer is this: 

```
SELECT name FROM table_name 
GROUP BY name
HAVING min(number_grade) > 80; 
```

so the difference between HAVING and WHERE is that WHERE can only apply to one element inside the database? and HAVING coming after GROUP BY while WHERE is always before GROUP BY? 

am I understanding this correctly? 

If it is not broken, let's fix till it is. 

Link to comment
https://linustechtips.com/topic/963655-having-vs-where-in-sql/
Share on other sites

Link to post
Share on other sites

1 hour ago, mrchow19910319 said:

--SNIP--

so the difference between HAVING and WHERE is that WHERE can only apply to one element inside the database? and HAVING coming after GROUP BY while WHERE is always before GROUP BY? 

am I understanding this correctly? 

Yes that's correct. HAVING operates on aggregate functions (ex. COUNT) on grouped results. WHERE operates on each individual row.

Found some very good explanations on stackoverflow: https://stackoverflow.com/questions/9253244/sql-having-vs-where 

CPU: i7-12700KF Grill Plate Edition // MOBO: Asus Z690-PLUS WIFI D4 // RAM: 16GB G.Skill Trident Z 3200MHz CL14 

GPU: MSI GTX 1080 FE // PSU: Corsair RM750i // CASE: Thermaltake Core X71 // BOOT: Samsung Evo 960 500GB

STORAGE: WD PC SN530 512GB + Samsung Evo 860 500GB // COOLING: Full custom loop // DISPLAY: LG 34UC89G-B

Link to comment
https://linustechtips.com/topic/963655-having-vs-where-in-sql/#findComment-11676159
Share on other sites

Link to post
Share on other sites

22 minutes ago, Limecat86 said:

Yes that's correct. HAVING operates on aggregate functions (ex. COUNT) on grouped results. WHERE operates on each individual row.

Found some very good explanations on stackoverflow: https://stackoverflow.com/questions/9253244/sql-having-vs-where 

Thanks! 

If it is not broken, let's fix till it is. 

Link to comment
https://linustechtips.com/topic/963655-having-vs-where-in-sql/#findComment-11676190
Share on other sites

Link to post
Share on other sites

No offense, but this is a pretty google-able question. I don't want to discourage question asking, but you've asked quite a few smaller questions here and I think you might be doing yourself a disservice by not growing your google-fu skills. Being able to seek out information is an important skill, and it takes practice. By going straight to LTT forums to have people give you the answer, you are not letting yourself grow. again, asking questions is never bad, but it's also good to expose yourself to scouring the web for solutions.

Gaming build:

CPU: i7-7700k (5.0ghz, 1.312v)

GPU(s): Asus Strix 1080ti OC (~2063mhz)

Memory: 32GB (4x8) DDR4 G.Skill TridentZ RGB 3000mhz

Motherboard: Asus Prime z270-AR

PSU: Seasonic Prime Titanium 850W

Cooler: Custom water loop (420mm rad + 360mm rad)

Case: Be quiet! Dark base pro 900 (silver)
Primary storage: Samsung 960 evo m.2 SSD (500gb)

Secondary storage: Samsung 850 evo SSD (250gb)

 

Server build:

OS: Ubuntu server 16.04 LTS (though will probably upgrade to 17.04 for better ryzen support)

CPU: Ryzen R7 1700x

Memory: Ballistix Sport LT 16GB

Motherboard: Asrock B350 m4 pro

PSU: Corsair CX550M

Cooler: Cooler master hyper 212 evo

Storage: 2TB WD Red x1, 128gb OCZ SSD for OS

Case: HAF 932 adv

 

Link to comment
https://linustechtips.com/topic/963655-having-vs-where-in-sql/#findComment-11676763
Share on other sites

Link to post
Share on other sites

48 minutes ago, reniat said:

No offense, but this is a pretty google-able question. I don't want to discourage question asking, but you've asked quite a few smaller questions here and I think you might be doing yourself a disservice by not growing your google-fu skills. Being able to seek out information is an important skill, and it takes practice. By going straight to LTT forums to have people give you the answer, you are not letting yourself grow. again, asking questions is never bad, but it's also good to expose yourself to scouring the web for solutions.

totally agree with what you said. that being said, I have found the solution on google before someone replied me here... 

I think in the future I will google more, then come here ask questions. 

about all the other questions, I really don't know any thing about it. all the questions I posted about web server etc.

It is pretty new to me and I had been googling those things for a while now. digital ocean has some similar question, stack overflow has some similar question, but i have yet found a solution that works for me.

 

so I will try to google more and find out more myself, if I did not find any solution, I will include my working progress and quote other people's solution online and tell you guys which one I have tried, yet failed, which one I did not try cos I don't know how to. 

 

 

nonetheless thanks for the suggestion! 

If it is not broken, let's fix till it is. 

Link to comment
https://linustechtips.com/topic/963655-having-vs-where-in-sql/#findComment-11676939
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×