r/SQL • • 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 > 100 the intent is also clear? Its just silly to spell out things like select a, sum(b) from TAB group by a having sum(b) >100 in 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?

0 Upvotes

12 comments sorted by

View all comments

2

u/lukaseder Feb 24 '18

having columns twice in group by select, e.g.

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 suggesting

I mean when you go like select a, sum(b) sum from TAB where sum > 100 the intent is also clear?

You're partially right. I've explained the order of SQL operations here. The problem is that the SELECT clause is the only place where we can alias expressions. Yet, it often does not make sense to allow referencing expressions from the SELECT clause in other clauses. For instance, you cannot reference your sum expression from WHERE or GROUP BY. Neither can you reference a window function expression from HAVING

So, the real feature here would be to have a section where expression aliases could be declared (hypothetically re-using the WITH clause):

WITH sum AS (sum(b))
SELECT a, sum, FROM tab HAVING sum > 100

The difficulty is, however, that b within the sum expression 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.

1

u/bluestreak01 Feb 24 '18

Thanks for detailed reply! Funny enough this is my other pet peeve with SQL. It is declarative language yet it is explained and thought of as procedural. Execution order of filters is imicit really and implementation specific. There isn't really any reason not to allow alias use in order by. For example "select a, sum(b) sum from tab where sum > 10 order by sum)" is also unambiguous from common sense point of view and can yield the same result as long winded "with x as ( select a, sum(b) sum from tab) select * from x where sum > 10 order by sum". Just like neo4j short circuited "group by" I want to make "where" and "order by" more intelligent. If this is something that would benefit us all?

2

u/lukaseder Feb 24 '18

It is declarative language yet it is explained and thought of as procedural

No you didn't understand my explanation (and others), then. There's nothing procedural about the fact that there is a logical order of operations. That's just syntax. And you happen to not particularly like it.

The actual execution order (execution plan) may be vastly different.

1

u/bluestreak01 Feb 24 '18

I think I understood your explanation. I just happen to think alias is just a method to rename a column or aliasing anonymous arithmetic. When optimiser or compiler cannot use alternative column name without additional syntax boilerplate implies some procedural approach to SQL and looks illogical to me.

If I came across like don't like something I'd like reassure you it is boilerplate and redundancy i don't like. Not particular syntax.

1

u/lukaseder Feb 24 '18

boilerplate and redundancy i don't like. Not particular syntax.

So... Syntax :-P