Explore Courses Blog Tutorials Interview Questions
0 votes
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.7k 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, 2020 in SQL by dev_sk2311 (45k points)
0 votes
1 answer
asked Dec 4, 2020 in SQL by Appu (6.1k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

30.5k questions

32.5k answers


108k users

Browse Categories