Search:     Advanced search
Browse by category:
C/C++   |   Java   |   Oracle/Database   |   SAP   |   ASP .NET   |   Mainframe-DB2   |   Freshers

How can we rewrite sub-queries into simple select statements or with joins?

Add comment
Views: 111
Votes: 0
Comments: 0
Yes we can write using Common Table Expression (CTE). A Common Table Expression (CTE) is an expression that can be thought of as a temporary result set which is defined within the execution of a single SQL statement. A CTE is similar to a derived table in that it is not stored as an object and lasts only for the duration of the query.

E.g.

USE AdventureWorks
GO
WITH EmployeeDepartment_CTE AS (
SELECT EmployeeID,DepartmentID,ShiftID
FROM HumanResources.EmployeeDepartmentHistory
)
SELECT ecte.EmployeeId,ed.DepartmentID, ed.Name,ecte.ShiftID
FROM HumanResources.Department ed
INNER JOIN EmployeeDepartment_CTE ecte ON ecte.DepartmentID = ed.DepartmentID
GO
Others in this Category
document What is Replication and Database Mirroring?
document What are different normalization forms?
document What command would you use to create a backup control file?
document What is Catalog Views?
document What is Difference between Function and Stored Procedure?
document What is an execution plan? When would you use it? How would you view the execution plan?
document What is Data Compression?
document What is SQL Server Agent?
document What is Identity?
document Q: What is ON DELETE CASCADE ?
» More articles



RSS