Resolved: The sequence is always WHERE, GROUP BY, HAVING and Order BY
Is this the sequence of these SQL functions? My question is that while solving SQL queries where we use these above functions- is this always the same sequence we have to follow ?
1 answers ( 1 marked as helpful)
Hi Anas!
Thanks for reaching out.
Nice question! It means that you learn with understanding!
The actual execution of MySQL statements is a bit tricky. However, the standard does specify the order of interpretation of elements in the query. The order is the following:
- FROM clause
- WHERE clause
- SELECT clause
- GROUP BY clause
- HAVING clause
- ORDER BY clause
This is important for understanding how queries are parsed. You cannot use a column alias defined in a SELECT in the WHERE clause, for instance, because the WHERE is parsed before the SELECT. On the other hand, such an alias can be in the ORDER BY clause.
Hope this helps.
Best,
Tsvetelin