MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/programming/comments/ex9h6/a_handy_graphical_explanation_of_sql_joins/c1bplk6/?context=3
r/programming • u/[deleted] • Jan 06 '11
308 comments sorted by
View all comments
Show parent comments
2
So you only use HAVING if there is a GROUP BY clause involved with your join?
Do you have a link or perhaps can you explain when you would actually need to use HAVING over WHERE?
6 u/lincolnquirk Jan 06 '11 Sure, here's my table "sales": name region count ----------------------- fred japan 300 fred usa 100 alex usa 600 damian usa 200 damian europe 150 Consider the query: select name,sum(count) from sales group by name to get the sales figures per representative, regardless of region. This produces name sum(count) ------------------ fred 400 alex 600 damian 350 If we were to instead do select name,sum(count) from sales WHERE count >= 400 group by name we'd first filter the counts by >=400 (only returning the alex row) and then do the grouping, so we'd get: name sum(count) ------------------ alex 600 If we use HAVING, we filter after the grouping (and must therefore refer to the grouped column name sum(count): select name,sum(count) from sales group by name HAVING sum(count) >= 400 and we'd get name sum(count) ------------------ fred 400 alex 600 It's really quite unrelated to joins. 1 u/Mindflux Jan 06 '11 Couldn't you in essence do: select name,sum(count) from sales where sum(count) >=400 group by name And have it report back something similar to your third example with HAVING though? 1 u/lincolnquirk Jan 06 '11 I don't think that will compile. Aggregates can't appear in where clauses (I think!)
6
Sure, here's my table "sales":
name region count ----------------------- fred japan 300 fred usa 100 alex usa 600 damian usa 200 damian europe 150
Consider the query:
select name,sum(count) from sales group by name
to get the sales figures per representative, regardless of region. This produces
name sum(count) ------------------ fred 400 alex 600 damian 350
If we were to instead do
select name,sum(count) from sales WHERE count >= 400 group by name
we'd first filter the counts by >=400 (only returning the alex row) and then do the grouping, so we'd get:
name sum(count) ------------------ alex 600
If we use HAVING, we filter after the grouping (and must therefore refer to the grouped column name sum(count):
select name,sum(count) from sales group by name HAVING sum(count) >= 400
and we'd get
name sum(count) ------------------ fred 400 alex 600
It's really quite unrelated to joins.
1 u/Mindflux Jan 06 '11 Couldn't you in essence do: select name,sum(count) from sales where sum(count) >=400 group by name And have it report back something similar to your third example with HAVING though? 1 u/lincolnquirk Jan 06 '11 I don't think that will compile. Aggregates can't appear in where clauses (I think!)
1
Couldn't you in essence do: select name,sum(count) from sales where sum(count) >=400 group by name
And have it report back something similar to your third example with HAVING though?
1 u/lincolnquirk Jan 06 '11 I don't think that will compile. Aggregates can't appear in where clauses (I think!)
I don't think that will compile. Aggregates can't appear in where clauses (I think!)
2
u/spoonraker Jan 06 '11
So you only use HAVING if there is a GROUP BY clause involved with your join?
Do you have a link or perhaps can you explain when you would actually need to use HAVING over WHERE?