The SQL WITH clause is used to provide a sub-query block which can be referenced in several places within the main SQL query. It was introduced by oracle in oracle 9i release2 database.
There is an example of employee table:
Syntax for the SQL WITH clause ?
This syntax is for SQL WITH clause using a single sub-query alias.
- WITH <alias_name> AS (sql_sub-query_statement)
- SELECT column_list FROM <alias_name> [table name]
- [WHERE <join_condition>]
When you use multiple sub-query aliases, the syntax will be as follows.
- WITH <alias_name_A> AS (sql_sub-query_statement)
- <alias_name_B> AS (sql_sub-query_statement_from_alias_name_A
- Or sql_sub-query_statement)
- SELECT <column_list>
- FROM <alias_name_A >,< alias_name_B >, [tablenames]
- [WHERE < join_condition>]