A subquery is a query nested in another query with parentheses. A subquery can be used in SELECT, FROM, INSERT, DELETE, UPDATE, and WHERE clauses, and can also be nested inside another subquery. Subqueries can be beneficial when a query may require complex joins and unions. A subquery can return either a single value, row, column, or table.
A subquery is the inner query of another query, which is considered the outer query. The inner query is executed before the outer query so that the inner query results are passed to the outer query. For example, in the following code, the query inside parentheses is the inner query, while the query outside parentheses is the outer query:
SELECT col1
FROM table1
WHERE col1 IN
(SELECT col1 FROM table 2 WHERE col1 = 'test')