Thursday 30 October 2014

Correlated SubQuery...

What Is Correlated Subquery...??
If the Subquery depends on the outer query for its values than that Subquery  is called Correlated Subquery

Example:

First of create  2 Table Like

(1) Table Tbl_Product
create table Tbl_Product
(
    Id int identity ,
    Name varchar(500)
)
(2)Table Tbl_Product_Sales
 create table Tbl_Product_Sales
(
    Id int identity,
    ProductId int,
    Quntity int
)

Now Insert Record Into Table Like.

Insert Record As per Image Given Below Into Tbl_Product ..





Insert Record As per Image Given Below Into Tbl_Product_Sales ..

Now, How to Write SubQuery......(Show Example1, Example2)

Example1:-
select Id , Name  from
Tbl_Product where Id NOT IN (select distinct ProductId   from Tbl_Product_Sales )

Example2:-
select Name ,
(select SUM (Quntity)  from Tbl_Product_Sales where ProductId  = Tbl_Product .Id  )as TotalQuantity
 from Tbl_Product

Enjoyyyyyyyyyyyyyyyyyyy......
By:- Arvind Kataria.