SQL
ORDER
OF
EXECUTION
Understand the order in which the SQL
engine processes the various
components of a query.
WHAT IS THE
ORDER OF
EXECUTION?
The order of execution refers to
the sequence in which different
components of a query are processed
by the SQL engine to produce the
final output.
BENEFITS OF
UNDERSTANDING
THE ORDER OF
EXECUTION:
Write accurate and efficient queries
Faster query execution times
Avoid errors or unexpected results
EACH FOLLOWING
SLIDE WILL BE IN
THE ORDER
OF EXECUTION ->
FROM clause
The FROM clause is the first to execute in
the sequence and it's purpose is to
identify the table or tables that the
query will retrieve data from.
JOIN clause
If there are any joins in the query, they
will be processed after the FROM clause.
A JOIN clause is used to combine rows
from two or more tables, based on a
related column between them.
WHERE clause
The WHERE clause is executed after the
FROM and JOIN claiuse. This clause
filters the rows returned by the FROM
and JOIN clauses.
GROUP BY clause
The GROUP BY clause groups rows that
have the same values into summary
rows. If the query has a WHERE clause,
the GROUP BY will execute after the data
has been filtered in the WHERE clause.
HAVING clause
The HAVING clause filters the grouped
rows produced by the GROUP BY clause.
Therefore to use this clause, your query
must have the GROUP BY clause.
The HAVING clause by nature must
execute after the GROUP BY clause.
SELECT clause
When writing a query you start with the
SELECT clause. But in the order of
execution the SELECT clause only
precedes the clauses that work off of
the result set. This is because this clause
specifies which columns will be included
in the result set.
ORDER BY clause
The ORDER BY clause sorts the rows in
the result set based on one or more
columns. This clause sorts the records in
ascending order by default. To sort the
records in descending order, use the
DESC keyword.
LIMIT/OFFSET clause
The LIMIT clause is used to specify the
number of records to return. The OFFSET
clause specifies the number of rows to
skip in the result table before any rows
are retrieved. The LIMIT/OFFSET clauses
will always be the last to execute in the
order of execution.
Note: These keywords do not exist in all
flavors of SQL.
Now Execute the
following steps in order:
1. LIKE this post
2. FOLLOW me on LinkedIn
3. HIT the notification bell
https://www.linkedin.com/in/drew-zeimetz/