0% found this document useful (0 votes)
17 views4 pages

What Is CTE in SQL Server

CTE stands for common table expression, which allows users to define a temporary result set that can be used and referenced within a SELECT, INSERT, UPDATE or DELETE statement. CTEs improve readability of complex queries by breaking them into multiple logical blocks. A CTE is defined at the start of a query and exists only for the duration of query execution, unlike views which are stored objects. CTEs can reference other CTEs defined in the same WITH clause and be used to simplify queries involving multiple joins, unions or recursive hierarchies.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views4 pages

What Is CTE in SQL Server

CTE stands for common table expression, which allows users to define a temporary result set that can be used and referenced within a SELECT, INSERT, UPDATE or DELETE statement. CTEs improve readability of complex queries by breaking them into multiple logical blocks. A CTE is defined at the start of a query and exists only for the duration of query execution, unlike views which are stored objects. CTEs can reference other CTEs defined in the same WITH clause and be used to simplify queries involving multiple joins, unions or recursive hierarchies.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

What Is CTE In SQL Server ?

 CTE stands for common table expression.


 CTE is introduced in SQL Server 2005.
 A CTE allows you to define a temporary result set, that can be linked immediately with
the select, insert, update or delete statement.
 CTE is similar to a temporary resultset defined within the execution scope of a single
SELECT, INSERT, UPDATE, DELETE, or CREATE VIEW statement.
 The CTE can also be used in a View.
 A CTE is defined at the start of a query and can be referenced several times in the outer
query.
 Key advantages of CTEs are improved readability and ease in maintenance of complex
queries.

Syntax Of Defining CTE


WITH expression_name (column1, column2, ……)
AS
(
CTE_definition
)
SQL_statement;

CTE In SQL
 In SQL, CTE exists in memory only while the query is running. After the query is run,
the CTE is discarded; it cannot be used for the next SQL query unless we define it again.
Still, the same CTE might be referenced several times in the main query and any
subqueries.
 A view is a stored SQL query that is executed each time you reference it in another
query. Note that a view doesn’t store the output of a particular query – it stores the query
itself.
 The key thing to remember about SQL views is that, in contrast to a CTE, a view is a
physical object in a database and is stored on a disk. However, views store the query
only, not the data returned by the query. The data is computed each time you reference
the view in your query.
 CTEs are limited in scope to the execution of the outer query. Hence, when the outer
query ends, the lifetime of the CTE will end.
 You need to define a name for a CTE and also, define unique names for each of the
columns referenced in the SELECT clause of the CTE.
 It is possible to use inline or external aliases for columns in CTEs.
 A single CTE can be referenced multiple times in the same query with one definition.
 Multiple CTEs can also be defined in the same WITH clause.

CTE VS Sub Query


 CTE is created before the outer query.
 Sub Query is created after the outer query.

SQL Queries For CTE


select * from student;

-- EXAMPLE NO: 1
with New_CTE
as
(
select * from student where gender = 'Male'
)
select * from New_CTE

-- EXAMPLE NO: 2
with New_CTE
as
(
select * from student where gender = 'Male'
)
select count(*) from New_CTE

-- EXAMPLE NO: 3
with New_CTE
as
(
select * from student where gender = 'Male'
)
--select 'Hello World'
select * from New_CTE where age >= 17

-- EXAMPLE NO: 4 - WITH COLUMNS


with New_CTE(std_id, std_name, std_class)
as
(
select id, name, [standard] from student where gender = 'Male'
)
select std_id, std_name from New_CTE

-- EXAMPLE NO: 5
with New_CTE
as
(
select * from student
)
select * from New_CTE

-- EXAMPLE NO: 6 - CTE WITH INSERT UPDATE DELETE


with New_CTE
as
(
select * from student
)
delete New_CTE where id = 18
update New_CTE set name = 'Zain' where id = 18
insert New_CTE values('Afaq','Male',15,8)

-- EXAMPLE NO: 7 - CREATING VIEW USING CTE


create view vWMyNewView
as
with New_CTE
as
(
select * from student where [standard] = 12
)
select * from New_CTE

select * from vWMyNewView;

-- CREATING MULTIPLE CTES USING SINGLE WITH CLAUSE


with New_CTE
as
(
select * from student where [standard] = 12
),
New_CTE2
as
(
select * from student where [standard] = 11
)
select * from New_CTE
union all
select * from New_CTE2

with New_CTE
as
(
select count(*) as [Total_Male_Students] from student where gender = 'Male'
)
select * from New_CTE

You might also like