Skip to content

Subquery

Subqueries, also known as nested queries or sub-selects, are the query methods in which SELECT subquery statements are embedded in another SQL query.

See the following subquery example:

SELECT * FROM t1 WHERE column1 = (SELECT column1 FROM t2);

In the example, SELECT * FROM t1 WHERE column1 is an external query (or an external statement), and (SELECT column1 FROM t2) is a subquery. Subquery statements must be written in brackets and then nested in external query statements, or nested in other subquery statements to form multi-layer nesting.

Main advantages of subquery:

  • Subqueries can divide statements and provide structured queries.

  • Subqueries can replace complex JOIN and UNIONS statements.

  • Subqueries are more readable than complex JOIN and UNIONS.

A subquery has the following categories:

  • SELECT subquery
  • FROM subquery
  • WHERE Subquery

For more information, see:

limit

MatrixOne does not support selecting multiple columns for subquery.