SQL Reference Guide
Revision Time: 2 mins
16. SQL Order of Execution Reference
How the database engine executes SQL query clauses under the hood.
FROM
Return: ValueStep 1: Identify source tables.
Syntax signature:
FROM tableCode snippet:
python
Determines query context.Expected Output:
Initial raw set loaded.Remember: Always ensure correct syntax formatting when calling FROM.
JOIN
Return: TableStep 2: Combine joined tables on keys.
Syntax signature:
JOIN table2 ON keysCode snippet:
python
Correlates rows.Expected Output:
Composite dataset in memory.Remember: Always ensure correct syntax formatting when calling JOIN.
WHERE
Return: ValueStep 3: Filter base tables rows before grouping.
Syntax signature:
WHERE conditionCode snippet:
python
Discards rows.Expected Output:
Subset of rows.Remember: Always ensure correct syntax formatting when calling WHERE.
GROUP BY
Return: ValueStep 4: Collapse rows sharing target column values.
Syntax signature:
GROUP BY columnsCode snippet:
python
Groups categories.Expected Output:
Aggregated bucket lists.Remember: Always ensure correct syntax formatting when calling GROUP BY.
HAVING
Return: ValueStep 5: Filter aggregated buckets.
Syntax signature:
HAVING group_conditionCode snippet:
python
Discards groups.Expected Output:
Subset of grouped records.Remember: Always ensure correct syntax formatting when calling HAVING.
SELECT
Return: TableStep 6: Extract matching columns and assign expressions.
Syntax signature:
SELECT columnsCode snippet:
python
Extracts fields.Expected Output:
Selected fields.Remember: Always ensure correct syntax formatting when calling SELECT.
DISTINCT
Return: ValueStep 7: Deduplicate columns cells.
Syntax signature:
DISTINCTCode snippet:
python
Removes duplicate rows.Expected Output:
Unique output set.Remember: Always ensure correct syntax formatting when calling DISTINCT.
ORDER BY
Return: ValueStep 8: Sort matching output rows.
Syntax signature:
ORDER BY columnsCode snippet:
python
Sorts fields layout.Expected Output:
Sorted result dataset.Remember: Always ensure correct syntax formatting when calling ORDER BY.
LIMIT
Return: ValueStep 9: Slice resulting rows.
Syntax signature:
LIMIT countCode snippet:
python
Restricts rows count.Expected Output:
Sliced output rows.Remember: Always ensure correct syntax formatting when calling LIMIT.