A typical SQL query contains any of the clauses select, from, where, group by, having and finally limit. But, out of these clauses which one will be executed first, second and so on.
SQL engines follow below order of execution for the clauses.
Order | Clause | Function |
1 | FROM | Choose tables and joins to get the base data |
2 | WHERE | Filtering the data using the given conditions |
3 | GROUP BY | Aggregating the base data using column values |
4 | HAVING | To apply filters on aggregated data |
5 | SELECT | Returning final data |
6 | ORDER BY | Sorting the result data |
7 | LIMIT | Limiting the number of rows to be returned |