I'm studying databases with the book "Database System Concepts" from Silberschatz, Korth and Surdashan (I quote the authors because the relational algebra notation is different against other authors...
Well, I think that the execution order of a SQL query it's based in the relational algebra, for example:
If I have this SQL query:
SELECT nombre_sucursal, AVG(saldo) AS media_sucursal
FROM cuenta
GROUP BY nombre_sucursal
HAVING media_sucursal > 800
The corresponding relational algebra expression is the following:
Π nombre_sucursal (σ saldo > 800 (nombre_sucursal Ģ avg(saldo) (cuenta))
Well... For that, in my opinion, the execution order in a SQL query is (if you look at the relational algebra expression backwards):
- FROM - (cuenta) the base relation in the relational algebra expression
- GROUP BY nombre_sucursal - nombre_sucursal Ģ avg(saldo) in the relational algebra expression
- HAVING media_sucursal > 800 - σ saldo > 800 the selection operation in the relational algebra expression
- SELECT nombre_sucursal, AVG(saldo) AS media_sucursal - Π nombre_sucursal ???
I put the ??? symbols because this is the part that I don't understand:
If the SELECT clause is the last part of a SQL query, how can I rename the result of the aggregation function as "media_sucursal" and in the HAVING clause I'm able to use it?
I checked this question but it confirms what I put above, but not answer my question.
Any help is welcome!