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

SYNTAX

EXAMPLE

Let’s consider a demo database with Tables Employee & IndividualSalary. 

Figure 45.1. Employee TABLE

Figure 45.2. IndividualSalary TABLE

QUERY

Selecting Employee Details from IndividualSalary using a Where Condition

Figure45.3. SELECT SUBQUERY OUTPUT

 

SUBQUERY WITH THE INSERT STATEMENT

SYNTAX

SUBQUERY WITH THE UPDATE STATEMENT

SYNTAX

 

QUERY

Updating Employee Details from IndividualSalary using a Where Condition

Figure 45.4. UPDATE SUBQUERY OUTPUT

 

SUBQUERY WITH THE DELETE STATEMENT

SYNTAX

QUERY

Deleting Employee Details from IndividualSalary using a Where Condition

Figure 45.5 DELETE SUBQUERY OUTPUT

©2020 by Pavan Lalwani. 

  • Udemy
  • Facebook
  • Twitter
  • YouTube
  • Linkedin