Having, a less understood SQL clause
smallthingssql.com
Having, a less understood SQL clause
1–10 of 83 posts
Re: Having, a less understood SQL clause
#2Re: Having, a less understood SQL clause
#3Re: Having, a less understood SQL clause
#4HAVING is less understood? There’s nothing strange about HAVING, it’s just like WHERE, but it applies after GROUP BY has grouped the rows, and can use the grouped row values. (Obviously, this is only useful if you actually have a GROUP BY clause.) If HAVING did not exist, you could just as well do the same thing using a subselect (i.e. doing SELECT * FROM (SELECT * FROM … WHERE … GROUP BY …) WHERE …; is, IIUC, equiva…
Re: Having, a less understood SQL clause
#5After 20+ years of SQL usage (as an ordinary dev, not business/reporting/heavy SQL dev), I learned about `group by cube` from this article...
"group by cube/coalesce" is much more complicated thing in this article than "having" (that could be explained as where but for group by)
Re: Having, a less understood SQL clause
#6Re: Having, a less understood SQL clause
#7HAVING is less understood? There’s nothing strange about HAVING, it’s just like WHERE, but it applies after GROUP BY has grouped the rows, and can use the grouped row values. (Obviously, this is only useful if you actually have a GROUP BY clause.) If HAVING did not exist, you could just as well do the same thing using a subselect (i.e. doing SELECT * FROM (SELECT * FROM … WHERE … GROUP BY …) WHERE …; is, IIUC, equiva…
FWIW I just tested a somewhat complex query using HAVING vs a sub-select as you indicated and Postgres generated the same query plan (and naturally, results) for both.
Caesar if you see this thanks for being a great mentor.
Re: Having, a less understood SQL clause
#8I also dislike code that does not have all lines included. The UNION clauses here are missing which i simply find irritating.
Re: Having, a less understood SQL clause
#9e.g. rather than having to do
SELECT
COALESCE(extract_district(rm.district), extract_district(p.project_name), NULLIF(rm.district, '')) AS district,
...
FROM ...
WHERE COALESCE(extract_district(rm.district), extract_district(p.project_name), NULLIF(rm.district, '')) IS NOT NULL
just do SELECT
COALESCE(extract_district(rm.district), extract_district(p.project_name), NULLIF(rm.district, '')) AS district,
...
FROM ...
HAVING district IS NOT NULL
Hopefully the optimizer understands that these are equivalent, I haven't checked.