in a select
query when some records doesn’t satisfy the condition in where clause, they will be omitted for executing subsequent clauses or not?
for example consider we have a group by
clause and after that, we have a condition in having
clause on max(someThing)
.
if a record has the maximum value of property someThing
and it didn’t satisfy the condition in the where
clause, it will be examining in the having or not?
Advertisement
Answer
in a select query when some records doesn’t satisfy the condition in where clause, they will be omitted for executing subsequent clauses or not?
Yes, they will be omitted. Clauses are evaluated sequentially, and records that are expurged by a clause are not taken into account by the following clauses.
if a record […] didn’t satisfy the condition in the where clause, it will be examining in the having or not?
In MySQL, The typical order of evaluation of clauses in a SQL statement is as follows: FROM
, WHERE
, SELECT
, GROUP BY
, HAVING
, ORDER BY
.
Also MySQL is a bit different that other RDBMS about the sequence of the SELECT
clause (in many other RDBMS, it is executed after the GROUP BY
and HAVING
clauses), you can see that the WHERE
clause executes very early (which is true for most, if not all RDBMS). So records that do not satisfy it are expurged before the GROUP BY
and HAVING
clauses are evaluated.
You can refer to this SO post for more details on that topic.