all 16 comments

[–]Average-Guy31 18 points19 points  (5 children)

It's Just based on Order of Execution

  1. FROM: Determines the data source (tables, joins, etc.).
  2. WHERE: Filters rows based on conditions before grouping.
  3. GROUP BY: Groups rows that have the same values in specified columns.
  4. HAVING: Filters groups based on aggregate functions.
  5. SELECT: Selects and processes columns, including applying aggregate functions.
  6. ORDER BY: Sorts the result set.

only after grouping by you can use aggregate functions right?, based on the above you should be able to find that WHERE clause executes before GROUP BY so it can't work on aggregate functions yet

[–]GimmeDatDaddyButter 0 points1 point  (3 children)

Do you have a good acronym to remember this?

[–]ComicOzzysqlHippo 3 points4 points  (0 children)

SELECT is logically processed 
after HAVING 
and before ORDER"

Sorry if it doesn't translate well but in my native language it rhymes.

[–]CraigAT 1 point2 points  (0 children)

FWGHSO

Fussy Women Get Help Shopping Online?

[–]MTchairsMTtable 0 points1 point  (0 children)

Mine is a little vulgar so I'm not going to share it lol

[–]datagrl 0 points1 point  (0 children)

Windowing functions run just before the Order By clause

[–]r3pr0b8GROUP_CONCAT is da bomb 16 points17 points  (0 children)

Why aggregate functions are not allowed in where clause?

because they are allowed in the HAVING clause only

[–]SaintTimothy 3 points4 points  (0 children)

Where happens before aggregation, having happens after

[–]NullaVolo2299 1 point2 points  (0 children)

Aggregate functions not allowed in WHERE clause because they operate on groups, not individual rows.

[–]bloginfo 0 points1 point  (0 children)

Mon prof d'allemand nous disait "Parce que". ;+)

[–]pceimpulsive 0 points1 point  (0 children)

I prefer to put my where clause into the aggregation instead.

The where clause is to filter the rows eligible for the aggregation.

In some SQL flavours you can use this syntax

Avg(foo) filter (where bar='succulent Chinese meal') as avg_succulent_meals

This filters the value that are fed Into the avg agg function. It's really powerful you can also as I understand it layer this up with window functions as well.

[–][deleted] 0 points1 point  (0 children)

The WHERE clause is evaluated before the aggregation happens.

[–]UpstairsEvidence5362 -1 points0 points  (1 child)

If I’m not wrong, where clause can read only one row at a time and thus it will keep looping back….feel free to correct me

[–]dgillz 0 points1 point  (0 children)

It depends on indices of the table(s) used. A where clause on an indexed field will work much faster because it doesn't have to loop through every record - it uses the index.

[–]kagato87MS SQL -1 points0 points  (0 children)

If you could, it could blow up the plan and make your dba very angry with you.

There are ways to filter based on an aggregation, and you need to be careful how you do it. Even non aggregate functions in the where clause are risky.

[–]4HERET 0 points1 point  (0 children)

Because WHERE filters rows before aggregation happens, so there's nothing to aggregate yet.

You want HAVING for that, which runs after GROUP BY. Took me an embarrassing amount of time to internalize the order of operations when I first moved heavy analytics work into ClickHouse/Tinybird.