What is CTE and its example

CTE stands for Common Table Expression, use Dr. Google for more specifications.
According to me CTE means a temp view/table to hold any query result. Its very useful in writing complex recursive queries. click to see

Here I am demonstrating a small example of CTE.

Suppose, You have a table M_Users, see below :-

M_Users
Username FirstName
admin Gagan
uAman Aman
uNaman Naman

now execute following query :-

;WITH ExampleCTE(Username,FirstName)
AS
(
    SELECT Username, firstname from M_Users
)
SELECT * FROM ExampleCTE

Output :

ExampleCTE
Username FirstName
admin Gagan
uAman Aman
uNaman Naman

Output is quite simple, here you can write multiple queries defining joins and all. depends upon your requirement.

Comments

Popular posts from this blog

FastEndpoint vs Minimal APIs vs Controller

Kick start 2021 😃😃

Work with Raspberry PI Zero W