Nested Queries :

A Nested query is a query within another SQL query and embedded within the Where clause. It is used to return data that will be used in the main query as a condition to further restrict the data to be retrieved. The Nested query is also known as Sub-query.

The Nested query can be used with the SELECT, INSERT,UPDATE, and DELETE statements along with the operators like =, <, >, >=, <=, IN, BETWEEN, etc. There are few rules that Nested queries must follow :

  • In Nested query, the Sub-queries must be enclosed within parentheses.
  • A Nested query can have only one column in the SELECT clause, unless multiple columns are in the main query for the sub-query to compare its selected columns.
  • An ORDER BY command cannot be used in a Nested query, although the main query can use an ORDER BY. The GROUP BY command can be used to perform the same function as the ORDER BY in a Nested query.
  • Nested queries that returns more than one row can only be used with multiple value operators such as the IN operator.
  • A Nested query cannot be immediately enclosed in a set function. 
  • The Between Operator can not be used with a Nested query. However, the BETWEEN operator can be used within the sub-query.
  • The SELECT list cannot include any references to values that evaluate to a BLOB, ARRAY, CLOB, or NCLOB.

Next Topic :

No comments:

Post a Comment