A Subquery can be also called as Inner query or Nested query is a query within another main SQL query and embedded within the WHERE clause.
The Following are the properties of Subquery:
Subqueries can be used with the SELECT, INSERT, UPDATE, and DELETE statements along with the operators like =, <, >, >=, <=, IN, BETWEEN, etc.
Subqueries can return individual values or a list of records
Subqueries must be enclosed with parenthesis
The BETWEEN operator cannot be used with a subquery. However, the BETWEEN operator can be used within the subquery.
SUBQUERY WITHIN A SELECT STATEMENT
Let’s consider a demo database with Tables Employee & IndividualSalary.
Figure 45.1. Employee TABLE
Figure 45.2. IndividualSalary TABLE
Selecting Employee Details from IndividualSalary using a Where Condition
Figure45.3. SELECT SUBQUERY OUTPUT
SUBQUERY WITH THE INSERT STATEMENT
SUBQUERY WITH THE UPDATE STATEMENT
Updating Employee Details from IndividualSalary using a Where Condition
Figure 45.4. UPDATE SUBQUERY OUTPUT
SUBQUERY WITH THE DELETE STATEMENT
Deleting Employee Details from IndividualSalary using a Where Condition
Figure 45.5 DELETE SUBQUERY OUTPUT
Want More SQL Deep Dives