I am a new learner of SQL language to add knowledge to my career, I came to learn that in writing a query, there is a "Order of writing" vs "Order of execution", however I can't seem to find a full list of available SQL functions listing out the hierarchy
So far from what I learn I got this table, can someone with better knowledge help confirm if my table below is correct? And perhaps add any other functions that I might have missed, I am not sure where I should put the JOIN
in the table below
Also, is there a difference (either in order or name of function) if I am using different Sql platforms? MySql vs BigQuery for eg.
Your help is deeply appreciated, big thanks in advance for reading this post by a beginner
Order of writing | Order of execution |
---|---|
Select | From |
Top | Where |
Distinct | Group by |
From | Having |
Where | Select |
Group by | Window |
Having | QUALIFY |
Order by | Distinct |
Second | Order by |
QUALIFY | Top |
Limit | Limit |
SQL is a declarative language, not a procedural language. That means that the SQL compiler and optimizer determine what operations are actually run. These operations typically take the form of a directed acyclic graph (DAG) of operations.
The operators have no obvious relationship to the original query -- except that the results it generates are guaranteed to be the same. In terms of execution there are no clauses, just things like "hash join" and "filter" and "sort" -- or whatever the database implements for the DAG.
You are confusing execution with compilation and probably you just care about scoping rules.
So, to start with SQL has a set of clauses and these are in a very specified order. Your question contains this ordering -- at least for a database that supports those clauses.
The second part is the ordering for identifying identifiers. Basically, this comes down to:
FROM
clause. So this can be considered as "first" for scoping purposes.SELECT
clause. By the SQL Standard, column aliases can be used in the ORDER BY
. Many databases extend this to the QUALIFY
(if supported), HAVING
, and GROUP BY
clauses. In general, databases do not support them in the WHERE
clause.FROM
have the same column name, then the column has to be qualified to identify the table. The one exception to this is when the column is a key in a JOIN
and the USING
clause is used. Then the unqualified column name is fine.SELECT
conflicts with a table alias in a clause that supports column aliases, then it is up to the database which to choose.