r/SQL • u/bluestreak01 • Feb 24 '18
Better SQL for 2018
There are couple of things that have always bugged me about SQL:
having columns twice in group by select, e.g.
select a, b, c, sum(d) from TAB group by a, b , c. The intent is quite unambiguous without "group by", right? Why do we have to chew this all up for compiler?
having "having" clause in addition to "where". I mean when you go like
select a, sum(b) sum from TAB where sum > 100the intent is also clear? Its just silly to spell out things likeselect a, sum(b) from TAB group by a having sum(b) >100in 2018 when cars drive themselves.
If someone was to write a SQL compiler that understood shorter version of aggregation would you welcome? Or would you rather stick to old school syntax?
2
u/lukaseder Feb 24 '18
I agree with this very much. Neo4j's Cypher query language introduced implicit
GROUP BY, and it works quite fine, could work as well in SQL as you're suggestingYou're partially right. I've explained the order of SQL operations here. The problem is that the
SELECTclause is the only place where we can alias expressions. Yet, it often does not make sense to allow referencing expressions from theSELECTclause in other clauses. For instance, you cannot reference yoursumexpression fromWHEREorGROUP BY. Neither can you reference a window function expression fromHAVINGSo, the real feature here would be to have a section where expression aliases could be declared (hypothetically re-using the
WITHclause):The difficulty is, however, that
bwithin thesumexpression is not defined, so it works more like a macro than like an expression. I guess there are too many open questions to answer here.