Tuesday, October 11, 2016

SQL subquery

Subquery is called a query that a query inside it another query or nested query .Sub query support Select , Insert, Update, Delete .Sub query combine two different query into one query. We can use like, =,<,>,>=,<=,In, between with sub query .

Rules using subquery
  1. Subquery must be enclosed with parentheses.
  2. Order by can’t use by the inner query but outer query can use it.
  3.  Between operator can used inside the inner query or parentheses.

Table 
subquery
Select Subquery

SELECT * FROM EmployeeInfo where
EmployeeId IN
(SELECT  EmployeeId from EmployeeInfo where Salary>500)


Result




Insert  Subquery:

Insert into EmployeeInfo
SELECT * From EmployeeInfo
where EmployeeId IN (1 , 4);

Result:

Insert Subquery



Update Subquery:
Update  EmployeeInfo SET
Salary = Salary * 0
where EmployeeId IN (1 , 4);


Result:

update subquery



 Delete query:
Delete from   EmployeeInfo where EmployeeId  
IN(SELECT  EmployeeId from EmployeeInfo where Salary>500)


Result:

Delete subquery

No comments:

Post a Comment