Home >Database >Mysql Tutorial >What's the Exact Execution Order of SQL Clauses: FROM, WHERE, ORDER BY, and TOP?
Order of Execution for an SQL Statement
When executing an SQL statement, the order of execution for its clauses is crucial. Let's examine the execution order of a statement that combines a TOP clause, a WHERE clause, and an ORDER BY clause:
SELECT TOP 5 C.CustomerID,C.CustomerName,C.CustomerSalary FROM Customer C WHERE C.CustomerSalary > 10000 ORDER BY C.CustomerSalary DESC
According to the SELECT statement documentation, the logical processing order is:
However, it's important to note that the actual physical execution order may vary depending on the query processor.
Explanation:
Therefore, the execution order follows a logical sequence, filtering the data based on the WHERE clause, sorting it as specified in the ORDER BY clause, and finally retrieving the top results limited by the TOP clause.
The above is the detailed content of What's the Exact Execution Order of SQL Clauses: FROM, WHERE, ORDER BY, and TOP?. For more information, please follow other related articles on the PHP Chinese website!