T-SQL Tutorials - TSQLTutorials.com | |||||||||||||||||||||||||||||||||
WITH - Common Table Expressions | |||||||||||||||||||||||||||||||||
|
|
Introduction
The WITH Common Table Expression (CTE) is used for creating temporary named result sets. This T-SQL Expression begins with the keyword WITH. The results from the WITH expression are stored in a temporary named result set that can be queried. Using the WITH expression, allows for the simplification of query logic, by allowing the separation of logic into separate steps. The WITH expression has a few basic parts:
following the 'AS' keyword will be the SQL Query (surrounded by parentheses). The number of columns selected in the query must match the number of columns listed in the Table Expression Definition (the columns listed after name of resultset). Beginning Example
This example will use the following table:
SELECT Name, Position, Salary
Using the WITH expression we can create a temporary named result set. In this example we will limit the result set to only the Software Developers from the above Employees table.
WITH Developers (Name,Salary)
|
|
|||||||||||||||||||||||||||||||