Well it is the behavior of MySql extension to GROUP BY
MySQL Extensions to GROUP BY
MySQL extends this behavior to permit the use of an alias in the HAVING clause for the aggregated column
You can disable that extension with sql_mode
ONLY_FULL_GROUP_BY
SET [SESSION | GLOBAL] sql_mode = ONLY_FULL_GROUP_BY;
If you try to execute above-mentioned query in ONLY_FULL_GROUP_BY
sql_mode you'll get as the following error message:
Non-grouping field 'media_sucursal' is used in HAVING clause: SELECT nombre_sucursal, AVG(saldo) AS media_sucursal FROM cuenta GROUP BY nombre_sucursal HAVING media_sucursal > 800
as expected.
Here is SQLFiddle demo that illustrates that.