0 votes
1 view
in SQL by (20.3k points)

Why do you need to place columns you create yourself (for example select 1 as "number") after HAVING and not WHERE in MySQL?

And are there any downsides instead of doing WHERE 1 (writing the whole definition instead of a column name)?

1 Answer

0 votes
by (40.4k points)

To answer the first part of your question: WHERE clause is used before GROUP BY, and HAVING is used after (and can filter on aggregates) it.

In general, you can't reference aliases in any of these clauses, but MySQL allows referencing SELECT level aliases in GROUP BY, ORDER BY and HAVING.

To answer the second part of your question: If the calculated expression does not contain any aggregates, then putting it into the WHERE clause will most probably be more efficient.

Related questions

0 votes
1 answer
asked Jul 17, 2019 in SQL by Tech4ever (20.3k points)
+3 votes
1 answer
asked Jul 3, 2019 in SQL by Tech4ever (20.3k points)
0 votes
1 answer
asked Oct 20 in SQL by dev_sk2311 (20.5k points)
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...