WHAT IS CTE – COMMON TABLE EXPRESSION IN MSSQL

In Microsoft SQL Server, a Common Table Expression (CTE) is a temporary result set that you can reference within the context of a SELECT, INSERT, UPDATE, or DELETE statement.

CTEs allow you to create derived tables within a query, making the SQL code more readable and modular. With CTE we can create a subset of data with specific criteria before using it in the main query.

CTEs are a mechanism to store the query and it’s results in memory temproarily on a batch level. CTEs are applicable for faster access to data for quick comparisons and recursive operations.

In real world, the CTEs can be used to avoid self joins and subqueries.

Here is the basic syntax for a CTE in T-SQL (Transact-SQL):

Syntax:

Simple example of CTEs

Example 1: Simple select using CTEs

Example 2: How to insert using a CTE

Advanced SQL use cases of CTEs

Common Table Expressions (CTEs) in SQL are often used in real-time scenarios where complex queries or data transformations are required. Here are some common use cases for CTEs:

Example 3: Using CTE in Recursive queries and self joins

  • CTEs can be used to perform recursive queries, such as traversing hierarchical data structures like organizational charts or bill of materials. This type of CTE calls the same CTE inside it.
  • To implement recursive CTE, we need an ANCHOR ELEMENT [BASE DATA, START VALUE] & TERMINATION CHECK
  • The classic example of CTEs in recursive queries is to find the managers of employees.
  • Let’s assume that our requirement is to report list of all managers in the hierarchy for a given employee with the user id = 7

The above example checks who is the manager for the user with the id 7 and it checks further the manager of that person and so on, until the termination check fails.

The output of this query would be,

Example 4: Using CTEs for Data Aggregations, Data Validation and Data Cleansing

  • CTEs are useful for transforming and aggregating data before using it in subsequent parts of a query.
  • CTEs are handy for validating data against certain conditions or business rules before using it in further processing, inorder to check the data integrity issues before performing updates or inserts.
  • CTEs can assist in data cleansing tasks by identifying and handling anomalies or outliers to remove or correct inconsistent data before processing.
  • CTEs makes the code look cleaner.

Here is an example of one other use of CTE, where we join 2 tables, perform the aggregations, check if data is not null etc in our WITH clause.

Example 5: Using multiple CTEs for Derived Tables, Complex Joins and Filtering and Dynamic SQL Generation

  • CTEs can help enrich data by joining it with other tables or datasets. For example, we can join data with lookup tables to replace codes with descriptive values.
  • CTEs can be part of a larger query-building process with multiple WITH clauses, especially when generating dynamic SQL, by building a dynamic query to retrieve data based on user-specified parameters.
  • CTEs simplify complex join operations and filtering conditions, making the query more maintainable.

Let us see another example on how we can use multiple CTEs

The above query has 2 WITH statements with different operations and the SELECT query outside look much cleaner where it joins the CTEs.

The output of the above query will look like,

Example 6: Using CTEs for Calculating Running Totals, Time Series Analysis and Window Functions:

  • CTEs can be applied to implement pagination or ranking of results, especially when using the ROW_NUMBER() or RANK() window functions. For example, calculating average sales for each product category using window functions.
  • CTEs are useful for calculating running totals or cumulative aggregates over an ordered set of data and time-based calculations. For example, calculating the running total of sales over time.

Let us see an example of how Windows functions are used with CTEs

Popular posts