A sub-query is used with the other SQL Server query and embedded with the WHERE clause. The subquery is used to return data, which is used in the main question as a condition to restrict the data to retrieve.
There are few rules that sub queries follows -
Subqueries with SELECT Statement:
Subqueries are used with the SELECT statement.
Below is the syntax.
See the EMPLOYEES table:
We apply the subquery with the help of the SELECT statement.
Subqueries with INSERT Statement
Sub queries can be used with INSERT statements. The selected data of the sub query can modify the character, number or date functions.
The syntax of INSERT Statement is:
The syntax is used to copy the complete EMPLOYEES table into the EMPLOYEES_BKP.
Subqueries with UPDATE Statement
The sub query is used with UPDATE statement in the conjunction. When we use a sub query with the UPDATE statement single or more columns.
Below is the basic syntax.
The below command update the SALARY by 0.25 times into the EMPLOYEES table for the EMPLOYEES whose AGE> =31.
Two rows are given below in the table:
Sub queries with DELETE Statement:
The sub query is used in conjunction with the DELETE statement, with other comments generated above.
The syntax is.
The query deletes the records of the table EMPLOYEES whose AGE is greater than or equal to 31.
It has two rows, and the EMPLOYEES table will have the below records.