Example:
WITH TotalOrder AS ( SELECT CustomerID, SUM(Amount) TotalOrderAmount FROM [Order] GROUP BY CustomerID ) SELECT c.ID, c.FirstName, c.LastName, co.TotalOrderAmount FROM Customer c JOIN TotalOrder co ON co.CustomerID = c.ID
In the above example, TotalOrder is a Common Table Expression (CTE), created using the WITH clause to temporarily hold the result returned from the SELECT query defined in the WITH clause. After the WITH clause, the CTE is referred to in main query the same way as referring a database table. A CTE is considered as a virtual table.