SQL Query Processing Order
The following steps show the logical processing order, or binding order, for a SELECT statement. This order determines when the objects defined in one step are made available to the clauses in subsequent steps. For example, if the query processor can bind to (access) the tables or views defined in the FROM clause, these objects and their columns are made available to all subsequent steps. Conversely, because the SELECT clause is step 8, any column aliases or derived columns defined in that clause cannot be referenced by preceding clauses. However, they can be referenced by subsequent clauses such as the ORDER BY clause. Note that the actual physical execution of the statement is determined by the query processor and the order may vary from this list.
which gives the following order:
FROM
ON
JOIN
WHERE
GROUP BY
WITH CUBE or WITH ROLLUP
HAVING
SELECT
DISTINCT
ORDER BY
TOP
Complete SELECT query
SELECT DISTINCT column, AGG_FUNC(column_or_expression), …
FROM mytable
JOIN another_table
ON mytable.column = another_table.column
WHERE constraint_expression
GROUP BY column
HAVING constraint_expression
ORDER BY column ASC/DESC
LIMIT count OFFSET COUNT;
Each query begins with finding the data that we need in a database, and then filtering that data down into something that can be processed and understood as quickly as possible. Because each part of the query is executed sequentially, it's important to understand the order of execution so that you know what results are accessible where.
Query order of execution
1. FROM and JOINs
The
FROM clause, and subsequentJOINs are first executed to determine the total working set of data that is being queried. This includes subqueries in this clause, and can cause temporary tables to be created under the hood containing all the columns and rows of the tables being joined.
2. WHERE
Once we have the total working set of data, the
WHERE constraints are applied to the individual rows initially, and rows that do not satisfy the constraint are discarded. Each of the constraints can only access columns directly from the tables requested in the FROM clause. The aliases in the Select query are not accessible as they are not estimated yet and cannot be used in any other part of the query.
3. GROUP BY
The remaining rows after the
WHERE constraints are applied are then grouped based on common values in the column specified in the GROUP BY clause. As a result of the grouping, there will only be as many rows as there are unique values in that column. Implicitly, this means that you should only need to use this when you have aggregate functions in your query.
4. HAVING
If the query has a
GROUP BY clause, then the constraints in the HAVING clause are then applied to the grouped rows, discard the grouped rows that don't satisfy the constraint. Like the WHERE clause, aliases are also not accessible from this step in SQL Server.
5. SELECT
Any expressions in the
SELECT part of the query are finally computed. Any clause present in the SELECT are computed with it, that is any subquery or any case statement is estimated along with it and is available for later parts of the query to be processed.
6. DISTINCT
Of the remaining rows, rows with duplicate values in the column marked as
DISTINCT will be discarded.
7. ORDER BY
If an order is specified by the
ORDER BY clause, the rows are then sorted by the specified data in either ascending or descending order. Since all the expressions in the SELECT part of the query have been computed, you can reference aliases in this clause.
8. LIMIT / OFFSET OR TOP
Finally, the rows that fall outside the range specified by the
LIMIT and OFFSET are discarded, leaving the final set of rows to be returned from the query. The TOP clause is also estimated in the end post the ordering of the rows after the ORDER BY Clause.Conclusion
Not every query needs to have all the parts we listed above, but a part of why SQL is so flexible is that it allows developers and data analysts to quickly manipulate data without having to write additional code, all just by using the above clauses.
Sample Interview Questions:
1. Where will be the Order By clause will be executed ? Is it before the Select Clause, or after the Select clause?
2. Does SQL returns data in an ordered way by default? If not, how could we achieve this ?
Sample Interview Questions:
1. Where will be the Order By clause will be executed ? Is it before the Select Clause, or after the Select clause?
2. Does SQL returns data in an ordered way by default? If not, how could we achieve this ?
Vry nc but it doesnt contain all the clauses and conditions suppose if we have a case statements in select then what will happen?
ReplyDeleteIf Case is there in Select it will be estimated along with Select clause, if the Case statement is in the Order By Clause it will be executed along with it.
ReplyDelete