Last answered:

23 Nov 2022

Posted on:

22 Nov 2022


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)
Posted on:

23 Nov 2022


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:

  1. FROM clause
  2. WHERE clause
  3. SELECT clause
  4. GROUP BY clause
  5. HAVING clause
  6. 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.

Submit an answer