You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SELECT DISTINCT column, AGGREGATE(column)
FROM table1
JOIN table2
ONtable1.column=table2.columnWHERE constraint_expression
GROUP BY column
HAVING constraint_expression
ORDER BY column ASC/DESCLIMIT count;
Each part of the query is executed sequentially, so it’s important to understand the order of execution :
FROM and JOIN: The FROM clause, and subsequent JOINs are first executed to determine the total working set of data that is being queried
WHERE: Once we have the total working set of data, the WHERE constraints are applied to the individual rows, and rows that do not satisfy the constraint are discarded.
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.
HAVING: If the query has a GROUP BY clause, then the constraints in the HAVING clause are applied to the grouped rows, and the grouped rows that don’t satisfy the constraint are discarded.
SELECT: Any expressions in the SELECT part of the query are finally computed.
DISTINCT: Of the remaining rows, rows with duplicate values in the column marked as DISTINCT will be discarded.
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.
LIMIT: Finally, the rows that fall outside the range specified by the LIMIT are discarded, leaving the final set of rows to be returned from the query.
The text was updated successfully, but these errors were encountered:
https://dataschool.com/sql-optimization/order-of-a-sql-query/
Let’s take a look at a sample SQL query :
Each part of the query is executed sequentially, so it’s important to understand the order of execution :
The text was updated successfully, but these errors were encountered: