Wednesday, August 21, 2013

TSQL Interview Questions: Sub Query

SubQuery is the query which returns resultset that can be used in main query. Subquery is like a table which joins with other tables in main query or can be used in where clause of main query. SubQuery is also reffered as derived table.
Performance wise subquery is not a better option as SQL Server doesnt creates execution plan of sub query.
Example: Let says user want the Employees who have more than one promotions in their carrer time.
For this you need to join employee table with history table.
Select EmpName from Emp
inner join (select EmpID , count(*) from EmpHist  group by having Count(*)> 1 )A on A.EmpID=Emp.EmpID
the statement in parenthesis is sub query.

No comments:

Post a Comment