What Is a Subquery? A subquery is a SELECT statement embedded in a clause of another SQL statement ( SELECT , INSERT , UPDATE , DELETE ). SQL subquery is usually added in the WHERE Clause of the SQL statement . SELECT . . . FROM . . . WHERE . . . (SELECT . . . FROM . . . WHERE . . .) Main Query Subquery
Subquery Subquery or Inner query or Nested query. An alternate way of returning data from multiple tables . Subqueries can be used with the following SQL statements along with the comparison operators like =, <, >, >=, <= etc . Subqueries can also be used inside the WHERE or HAVING clause. SELECT select_list From table WHERE OOP operator SELECT select_list From table
Sub-query Syntax The subquery ( inner query ) executes once before the main query. The result of the subquery is used by the main query ( outer query ).
Types of Subqueries
Types of Subqueries Single-row subqueries: Queries that return only one row from the inner SELECT statement.
Types of Subqueries Multiple-row subqueries: Queries that return more than one row from the inner SELECT statement.