Cte Sql.
Cte Sql.
With New_CTE
As
(
Select * from Student where gender ='male'
)
Select * from New_CTE;
With New_CTE
As
(
Select * from Student where gender ='male'
)
With New_Stud
As
(
Select * from Student where gender = 'Male')
Example
with New_CTE (std_ID, std_name, std_class)
AS
(
Select id, name, standard from student where
gender='female'
)
select std_ID, std_name, std_class from New_CTE;
Example
With New_CTE
as
(
Select count (*) as Total_Gender from Student where
gender= 'female'
)
Select * from New_CTE
with New_CTE
As
(
Select * from Student
)
with New_CTE
As
(
Select * from Student
)
with New_CTE
as (
select * from Student
)
--Update New_CTE set Gender='female' where id=9;
Syntax:
With Expression_name( Col1,col2)
As
(
CTE Defination
)
View in CTE
With New_CTE
As
(
Select * from student where [standard] =12
)
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
Advantages
CTEs are improved Readability
Ease in maintenance of complex queries
For better Performance
Example
Created Emp_Details table
Select * From Emp_Details;
Fetch Employee who earn more than avg. salary of all employees.
Find store who’s sales where better than the average sales across all
stores.
Recursive :
A recursive common table expression (CTE) is a CTE that
references itself. The CTE repeatedly executes, returns subsets
of data, until it returns the complete result set.
Syntax:
Example:
With Emp_hierarchy
As
(
Select Id, name, manager_ID, designation, 1 as level from emp_info
where name=’Asha’
Union All
Select E.id, E.name,E.manager_Id, E.designation, h.level+1 as Level
from emp_hierarchy h
Join emp_info E on H. manager_id=E.id
)
Order BY In CTE
WITH Sal_OrderBy AS(
SELECT
ROW_NUMBER() OVER (
ORDER BY salary
) row_num,
Emp_ID,
Emp_Name,
salary
FROM
Emp_Details
)
SELECT * FROM Sal_OrderBy WHERE row_num > 2 AND
row_num <=5;
Finding Nth Highest Salary from Emp_Info table
With Salary_CTE
As
( select * , dense_rank() over (order by salary desc) as sal_order from
Emp_info)
Delete from EmpDetail where ID not in (select max(ID) from EmpDetail Group
By EmpName,Departmemt,Age,Gender,Salary)
WITH duprow AS
(Select
EmpName,Departmemt,Age,Gender,Salary ,ROW_NUMBER()OVER(PARTIT
ION BY EmpName,Departmemt,Age,Gender,Salary ORDER BY ID) AS
RowNumber FROM EmpDetail)
--Select * from duprow
Stored Procedure
A stored proc is group of SQL state. If we have a situation,
where we write the same query over and over again, then we can
save that specific query as stored procedure and call it just by its
name.
To execute
Exec GetEmp_infoByPara 7, Manager
Exec GetEmp_infoByPara ( Proc name) @Manager_ID= 7 ,
@Designation= ‘Manager’
End;
GO
Temporary Tables
Insert into #PersonDetails Values(1, 'Mike)
Insert into #PersonDetails Values(2, 'John')
Insert into #PersonDetails Values(3, 'Todd')
Select * from #PersonDetails
Select name from tempdb..sysobjects
where name like '#PersonDetails%'
Insert into #PersonDetails Values(1, 'Mike')
Insert into #PersonDetails Values(2, 'John')
Insert into #PersonDetails Values(3, 'Todd')
Select * from #PersonDetails
End
Exec spCreateLocalTempTable
Example
Create Table ##EmployeeDetails(Id int, Name nvarchar(20))