All for Joomla All for Webmasters


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.

  1. WITH <alias_name> AS (sql_sub-query_statement)
  2. SELECT column_list FROM <alias_name> [table name]
  3. [WHERE <join_condition>]

When you use multiple sub-query aliases, the syntax will be as follows.

  1. WITH <alias_name_A>  AS (sql_sub-query_statement)
  2. <alias_name_B> AS (sql_sub-query_statement_from_alias_name_A
  3. Or sql_sub-query_statement)
  4. SELECT <column_list>
  5. FROM <alias_name_A >,< alias_name_B >, [tablenames]
  6. [WHERE < join_condition>]
submit to reddit

Leave a Reply