SQL Tutorial
SQL Database
SQL Table
SQL Select
SQL Order By
SQL Insert
SQL Update
SQL Delete
Difference
SQL Injection
SQL String Functions
Miscl
- SQL Formatter
- SQL group by
- SQL add/drop/update column operation
- SQL CAST Function
- SQL Comments
- SQL CONCAT Function
- CTE (Common Table Expression)SQL
- How to use distinct in SQL?
- Joining Three or More Tables in SQL
- What is Web SQL?
- How to create functions in SQL?
- How to run SQL Script?
- How to Delete Duplicate Rows in SQL?
- Nth Highest salary
- 12 Codd's Rules
- SQL EXCEPT
- Types of SQL JOIN
- Change datatype of column in SQL
- SQL Auto Increment
- SQL Like
- Commit and Rollback in SQL
- SQL Concatenate
- SQL get month from the date
- Savepoint in SQL
- SQL ORDER BY DATE
- TIME Datatype in SQL
- SQL BETWEEN
- CRUD Operations in SQL
- SQL INDEX
- Scalar Functions in SQL
- SET Operators in SQL
- Types of SQL Commands
- TCL Commands in SQL
- SQL Subquery
- SQL View
- Constraints in SQL
- Pattern Matching in SQL
- SQL Date Functions
- DDL Commands in SQL
- DML Commands in SQL
- SQL CASE
- SQL Inner Join
- SQL IN Operator
- Check Constraint in SQL
- SQL CLAUSES
- SQL LOGICAL OPERATORS
- Delete Column from Table
- Add Column in the Table
- Delete one row in SQL
- Change the Column Value
- How to Add Foreign Key in SQL
- Add a Primary Key
- Insert One or More rows
- How to Use LIKE in SQL
- Cursor in SQL
- Difference Between DROP and Truncate
- SQL Comparison Operators
- SQL COUNT WHERE
- SQL SELECT MIN
- SQL Stored Procedure
- SQL SELECT AVG
- SQL SELECT MAX
- SQL ADD COLUMN
- How to use Auto-Increment in SQL
- SQL Languages
- SQL Arithmetic Operators
- How to Use GROUP BY in SQL
- How to Use ORDER BY in SQL
- Trigger in SQL
- What is Race Condition
- SQL COUNT DISTINCT
PL/SQL Tutorial
Sql Interview Question
SQl Quiz
CTE (Common Table Expression)SQL
The Common Table Expressions (CTE) are imported into the SQL to simplify many classes of the Structured Query Language (SQL) for a derived table, which is unsuitable. It was introduced in 2005 SQL SERVER version.
The common table expressions (CTE) are a result set, which we reference with the SELECT, INSERT, UPDATE, or DELETE statement. In SQL 2008, we add a CTE for the unique MERGE statement.
How to use CTEs in T-SQL?
The use of Common Text Expression is to add the clause "WITH" before the SELECT, INSERT, UPDATE, DELETE or MERGE statement. The WITH clause contain one or more CTEs, which are separated by commas.
::::=
cte_name [(column_name[, ...])]__Write the name of column here
AS (cte_query)
We generate CTEs when we refer to any table. The CTE result set is not accessible to any statements when we run the particular statement.
Creating a Recursive table expression
The recursive CTE is used when we are working with hierarchical data. An example of hierarchical data in the table is the list of students in the group. For each student, the counter generates a ReferenceID and a NAME. The ReferenceID references itself like an employee ID in a recursive table. We use the CTE to display the position of employee's database.
If the CTE is created wrong, it enters into the infinite loop.
To prevent the endless loop, MAXRECURSION will added in the OPTION clause of the INSERT, DELETE, UPDATE, SELECT or MERGE statement.
Use below code to create a table:
{
EmployeeID int NOT NULL PRIMARY KEY,
FirstName varchar(50) NOT NULL,
LastNamevarchar(50) NOT NULL,
Manager ID int NULL
}
INSERT INTO Employees VALUES(1, 'Ken', 'Thompson', NULL)
INSERT INTO Employees VALUES(2, 'Kent', 'Thompson', 1)
INSERT INTO Employees VALUES(3, 'Williams', 'Thompson', 1)
INSERT INTO Employees VALUES(4, 'Charles', 'Thompson', 2)
INSERT INTO Employees VALUES(5, 'Michal', 'Thompson', 3)
INSERT INTO Employees VALUES(6, 'Gill', 'Thompson', 3)
INSERT INTO Employees VALUES(7, 'Danyl', 'Thompson', 3)
INSERT INTO Employees VALUES(8, 'Monty', 'Thompson', 5)
INSERT INTO Employees VALUES(9, 'Rob', 'Thompson', 6)
INSERT INTO Employees VALUES(10, 'Robert', 'Thompson',6)
After the Employee table is created, a SELECT statement, which is preceded by a WITH clause that includes a CTE named cteReports is created:
cteReports (EmpID, FirstName, LastName, MgrID, EmpLevel
AS
(
Select EmployeeID, FirstName, LastName, ManagerID, 1
FROM Employees
//WHERE ManagerID IS NULL
UNION ALL
SELECT e.StudentID, e.FirstName, e.LastName, e.ManagerID,
r. StuLevel + 1
FROM Students p
INNER JOIN cteReports s
ON e.ManagerID = r.StuID
)
SELECT
First Name+ ' ' + LAST NAME AS FullName,StuLevel,
(SELECT FirstName + '.....' +LastName FROM Students
WHERE StudentID = cteReports.MgrID) AS ManagerFROM cteReports
ORDER BY StuLevel, MgrID
It is an essential tool to generate the inconsistent result set, and retrieved in the SELECT, UPDATE, INSERT, MERGE, or DELETE the statement.