WHERE vs HAVING

Why is it that you need to place columns you create yourself (for example “select 1 as number”) after HAVING and not WHERE in MySQL? WHERE is applied before GROUP BY, HAVING is applied after (and can filter on aggregates). In general, you can reference aliases in neither of these clauses, but MySQL allows referencing … Read more

Can you use an alias in the WHERE clause in mysql?

You could use a HAVING clause, which can see the aliases, e.g. HAVING avg_rating>5 but in a where clause you’ll need to repeat your expression, e.g. WHERE (sum(reviews.rev_rating)/count(reviews.rev_id))>5 BUT! Not all expressions will be allowed – using an aggregating function like SUM will not work, in which case you’ll need to use a HAVING clause. … Read more