T-SQL (Transact-SQL) is a programming language used to manage and manipulate data in Microsoft SQL Server. It is a powerful language that allows developers to write complex queries and perform advanced data operations. In this blog post, we will discuss advanced T-SQL techniques that can help you improve the performance of your queries, write more efficient code, and solve complex data problems.

 

1) Window Functions

Window functions are a powerful tool in T-SQL that allows you to perform aggregate calculations without grouping the data. This is useful when you need to calculate running totals, moving averages, or cumulative values. Window functions are also more efficient than using subqueries or temporary tables.

 

The basic syntax of a window function is:

<function>() OVER (PARTITION BY <partition_column> ORDER BY <order_column>)

 

 For example, the following query calculates the running total of sales for each product:

SELECT product_id, sales, SUM(sales) OVER (PARTITION BY product_id ORDER BY date) as running_total

FROM sales_table

 

This query partitions the data by product_id and orders it by date. The SUM function calculates the running total of sales for each product.

 

2) Common Table Expressions (CTE)

Common Table Expressions (CTE) are temporary named result sets that you can reference within a SELECT, INSERT, UPDATE, or DELETE statement. CTEs can simplify complex queries by breaking them down into smaller, more manageable pieces. They are also more efficient than using subqueries or temporary tables.

The basic syntax of a CTE is:

 

WITH <cte_name> AS (

    <query_definition>

)

 

For example, the following query uses a CTE to calculate the average sales for each product:

 

WITH product_sales AS (

    SELECT product_id, AVG(sales) as average_sales

    FROM sales_table

    GROUP BY product_id

)

SELECT product_id, sales, average_sales

FROM sales_table

JOIN product_sales ON sales_table.product_id = product_sales.product_id

 

The CTE named product_sales calculates the average sales for each product. The main query joins the sales_table with the product_sales CTE to retrieve the average sales for each product.

 

3) Pivot and Unpivot

Pivot and unpivot are techniques used to transform data from rows to columns (pivot) or columns to rows (unpivot). These techniques are useful when you need to perform calculations or analysis on data that is not in the desired format.

The basic syntax of a pivot operation is:

 

SELECT <non-pivoted column(s)>,

    [first pivoted column] AS <column name>,

    [second pivoted column] AS <column name>,

    ...

FROM

    (<source table>) 

PIVOT

(

    <aggregation function>(<pivoted column>)

    FOR <pivoted column>

    IN ([first pivoted column], [second pivoted column],

        ... [last pivoted column])

) AS <alias for the pivot table>

 

For example, the following query pivots the data to display the total sales for each product by year:

 

SELECT product_id, [2019], [2020], [2021]

FROM

(

    SELECT product_id, year, sales

    FROM sales_table

) AS source_table

PIVOT

(

    SUM(sales)

    FOR year IN ([2019], [2020], [2021])

) AS pivot_table

 

The PIVOT operation aggregates the sales data by year and displays the total sales for each product in columns labeled 2019, 2020, and 2021.

 

The basic syntax of an unpivot operation is:

 

SELECT <non-unpivoted column>, <unpivoted column>, <value>

FROM

(

SELECT <non-unpivoted column>, <pivoted column 1>, <pivoted column 2>, ...

FROM <source table>

) AS source_table

UNPIVOT

(

<value> FOR <unpivoted column> IN (<pivoted column 1>, <pivoted column 2>, ...)

) AS unpivot_table

 

For example, the following query unpivots the data to display the sales for each product by year:

 

SELECT product_id, year, sales

FROM

(

SELECT product_id, [2019], [2020], [2021]

FROM sales_table

) AS source_table

UNPIVOT

(

sales FOR year IN ([2019], [2020], [2021])

) AS unpivot_table

 

The UNPIVOT operation transforms the data by converting the columns labeled 2019, 2020, and 2021 into a single column named year and a corresponding column named sales.

 

4) Recursive Queries

Recursive queries allow you to perform hierarchical queries on data that has a parent-child relationship. Recursive queries are useful when you need to retrieve data from a tree-like structure, such as an organizational chart or a file system.

The basic syntax of a recursive query is:

 

WITH <cte_name> AS (

SELECT <non-recursive columns>, 0 as level

FROM <source table>

WHERE <base case>

UNION ALL

SELECT <non-recursive columns>, level + 1

FROM <source table>

JOIN <cte_name> ON <recursive condition>

)

SELECT <final columns> FROM <cte_name>

 

graphql



For example, the following query retrieves the managers of an employee:

 

WITH employee_hierarchy AS (

SELECT employee_id, manager_id, 0 as level

FROM employee_table

WHERE employee_id = @employee_id

UNION ALL

SELECT employee_table.employee_id, employee_table.manager_id, level + 1

FROM employee_table

JOIN employee_hierarchy ON employee_table.employee_id = employee_hierarchy.manager_id

)

SELECT employee_id, level

FROM employee_hierarchy

WHERE level > 0

 

The CTE named employee_hierarchy retrieves the manager of an employee using a recursive query. The main query selects the employee_id and level columns from the CTE, filtering out the base case where level = 0.

 

Conclusion

In this blog post, we discussed advanced T-SQL techniques that can help you improve the performance of your queries, write more efficient code, and solve complex data problems. These techniques include window functions, common table expressions, pivot and unpivot, and recursive queries. By mastering these techniques, you can take your T-SQL skills to the next level and become a more effective developer.