Subqueries

Subqueries allow SQL statements to restrict where clause results based on the evaluated result of a select statement nested within the SQL statement. Using its nested query capability, a single SQL select statement can perform a task that may take many statements in procedural programming languages such as C. Subqueries are specified as a where clause relational expression as defined by the syntax below.

relational_expr:
        value_expr [NOT] rel_oper value_expr
   |    value_expr [NOT] BETWEEN constant AND constant
   |    value_expr [NOT] IN (constant [, constant]...) 
   |    column_ref  IS [[NOT]] 
   |    string_expr [NOT] NULL LIKE "string"
   |    [NOT] relational_expr
   |    ( conditional_expr )
   |    value_expr [NOT] rel_oper [{ANY | SOME} | ALL] sub_query
   |    value_expr [NOT]IN sub_query
   |    [NOT] EXISTS sub_query
rel_oper:
          =  |  ==                /* equal */
     |    <                         /* less than */
     |    >                         /* greater than */
     |    <=                       /* less than or equal */
     |    >=                       /* greater than or equal */
     |    <>  |  !=  |  /=       /* not equal */
sub_query:
          ( sub_select )
sub_select:
          SELECT {* | named_expr} FROM table_ref [, table_ref]...
               [WHERE conditional_expr] [grouping]
sub_select:
          SELECT {* | named_expr} FROM table_ref [, table_ref]...
               [WHERE conditional_expr] [grouping]

RaimaDB SQL can evaluate the following subquery classes.

  • Simple, single-value subquery
  • Multi-value subquery
  • Complex, correlated subquery
  • Existence check subquery

Each of these types of subqueries are described in the following sections.