Blog

Common Table Expression in SQL Server

Posted by Rohatash Kumar Blogs | SQL Nov 26, 2012
In this blog, I have explained Common Table Expression in SQL Server.

SQL Common Table Expression (CTE)

SQL Server has a very powerful feature has been added for the programmers' benefit: Common Table Expression (CTE).  Common Table Expressions, or CTE, are a new construct introduced in Microsoft SQL Server 2005 that offer a more readable form of the derived table that can be declared once and referenced multiple times in a query.

We have a simple table Employee in our database.

Employee-table-in-Sql-Server.jpg

Example

;WITH EmployeeCTE  AS

( SELECT  [EmpID]

      ,[EmpName]

      ,[EmpSalary]

FROM [master].[dbo].[Employee]

  WHERE [EmpSalary]>4000

)

SELECT * FROM EmployeeCTE

Now press F5 to execute.

Output

CTE-in-Sql-Server.jpg

post comment
     
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter