zlacker

[return to "Pipe Syntax in SQL"]
1. Cianti+06[view] [source] 2024-08-24 16:16:12
>>legran+(OP)
Here is one example from the PDF:

    FROM r JOIN s USING (id)
    |> WHERE r.c < 15
    |> AGGREGATE sum(r.e) AS s GROUP BY r.d
    |> WHERE s > 3
    |> ORDER BY d
    |> SELECT d, s, rank() OVER (order by d)
Can we call this SQL anymore after this? This re-ordering of things has been done by others too, like PRQL, but they didn't call it SQL. I do think it makes things more readable.
◧◩
2. andy80+D7[view] [source] 2024-08-24 16:29:10
>>Cianti+06
The multiple uses of WHERE with different meanings is problematic for me. The second WHERE, filtering an aggregate, would be HAVING in standard SQL.

Not sure if this is an attempt to simplify things or an oversight, but favoring convenience (no need to remember multiple keywords) over explicitness (but the keywords have different meanings) tends to cause problems, in my observation.

◧◩◪
3. singro+k8[view] [source] 2024-08-24 16:34:25
>>andy80+D7
In the query plan, filtering before or after an aggregation is the same, so it's a strange quirk that SQL requires a different word.
◧◩◪◨
4. 0cf861+la[view] [source] 2024-08-24 16:50:46
>>singro+k8
Indeed. Just as I think git’s N different ways to refer to the same operation was a blunder.
◧◩◪◨⬒
5. andy80+4b[view] [source] 2024-08-24 16:57:29
>>0cf861+la
But pre- and post- aggregation filtering is not really "the same" operation.
◧◩◪◨⬒⬓
6. 0cf861+rb[view] [source] 2024-08-24 17:01:29
>>andy80+4b
If I use a CTE and filter the aggregate, feels the same to me.
◧◩◪◨⬒⬓⬔
7. andy80+yl[view] [source] 2024-08-24 18:20:20
>>0cf861+rb
If you perform an aggregation query in a CTE, then filter on that in a subsequent query, that is different, because you have also added another SELECT and FROM. You would use WHERE in that case whether using a CTE or just an outer query on an inner subquery. HAVING is different from WHERE because it filters after the aggregation, without requiring a separate query with an extra SELECT.
◧◩◪◨⬒⬓⬔⧯
8. RaftPe+fI[view] [source] 2024-08-24 21:21:58
>>andy80+yl
> HAVING is different from WHERE because it filters after the aggregation, without requiring a separate query with an extra SELECT.

Personally I rarely use HAVING and instead use WHERE with subqueries for the following reasons:

1-I don't like repeating/duplicating a bunch of complex calcs, easier to just do WHERE in outer query on result

2-I typically have outer queries anyway for multiple reasons: break logic into reasonable chunks for humans, also for join+performance reasons (to give the optimizer a better chance at not getting confused)

[go to top]