I wonder how this query is executing successfully. As we know 'having' clause execute before the select one then here how alias name used in 'select' statement working in having condition and not giving any error.
CodePudding user response:
As we know 'having' clause execute before the select one
This affirmation is wrong. The HAVING
clause is used to apply filters in aggregation functions (such as SUM
, AVG
, COUNT
, MIN
and MAX
). Since they need to be calculated BEFORE applying any filter, in fact, the SELECT
statement is done when the HAVING
clause start to be processed.
Even if the previous paragraph was not true, it is important to consider that SQL statements are interpreted as a whole before any processing. Due to this, it doesn't really matter the order of the instructions: the interpreter can link all references so they make sense in runtime.
So it would be perfectly feasible to put the HAVING
clause before the SELECT
or in any part of the instruction, because this is just a syntax decision. Currently, HAVING
clause is after GROUP BY
clause because someone decided that this syntax makes more sense in SQL.
Finally, you should consider that allowing you to reference something by an alias is much more a language feature than a rational on how the instruction is processed.
CodePudding user response:
the order of exution is
- Getting Data (From, Join)
- Row Filter (Where)
- Grouping (Group by)
- Group Filter (Having)
- Return Expressions (Select)
- Order & Paging (Order by & Limit / Offset)
I still don't get, why you are asking about, syntactially your seelect qiery is correct, but if it the correct result we can not know