---
title: "How to Use Common Table Expression [CTE] in DB2"
description: "I have shared the Common Table expression of advanced SQL. You can save a lot of CPU time. And you can improve query performance."
url: https://srinimf.com/2020/08/30/sql-common-table-expression-best-example/
date: 2020-08-30
modified: 2026-10-10
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2020/08/pexels-photo-6908502.jpeg?fit=1880%2C1255&ssl=1
categories: ["SQL"]
tags: ["SQL Common Table expression example"]
type: post
lang: en
---

# How to Use Common Table Expression [CTE] in DB2

Here is an example and [how to use](https://srinimf.com/2015/08/20/db2-avg-column-function-best-example/) Common Table Expression (CTE) [in DB2](https://srinimf.com/2017/01/02/the-best-substring-example-in-db2-for-quick-use/). How to identify it? The ‘WITH’ Clause [in SQL](https://srinimf.com/2023/05/13/how-to-convert-rows-to-columns-pivot-function/) query says that it is a CTE.

## CTE the best example SQL query

```
WITH DEPTOTAL (WORKDEPT, MAXSALARY) AS
(SELECT WORKDEPT, SUM(SALARY+COMM)
  FROM DSN8910.EMP
  GROUP BY WORKDEPT)
SELECT WORKDEPT FROM DEPTOTAL
WHERE MAXSALARY =
  (SELECT MAX(MAXSALARY)
          FROM DEPTOTAL);
```

Here, the derived value is ‘WORKDEPT’ from the WITH table ‘DEPTOTAL‘.

![SQL CTE Top Interview Questions](https://i0.wp.com/srinimf.com/wp-content/uploads/2021/06/sql-cte-example.jpg?resize=1080%2C608&ssl=1)

## 3 Steps How CTE SQL Works

**Step-1**

Get MAXSALARY.

**Step-2**

Apply expression SUM(SALARY+COMM), and select the rows.

S**tep-3**

You will get desired Result.

![](https://i0.wp.com/srinimf.com/wp-content/uploads/2011/12/db2.png?ssl=1)
DB2 Tough Interview Questions.

Useful for interviews and Projects.

$1.00

## [Interview questions](https://srinimf.com/2026/04/26/quick-sql-interview-questions-for-data-engineers-little-tricky/)

**1.What’s CTE recursion?**

It’s not true recursion. Here same table is referenced. The WITH values will be used to get data from other table.

**2. CTE Vs Sub-query?**

It looks like sub-query. Here the table is same.

Related Posts

- [Calculate Match Percentage in PySpark SQL](https://srinimf.com/2024/12/22/calculate-match-percentage-in-pyspark-sql/)DateDecember 22, 2024
- [Top 10 SQL Queries for Efficient Data Transformation](https://srinimf.com/2023/12/24/10-common-sql-queries-that-use-in-data-transformation/)DateDecember 24, 2023
- [5 SQL Queries: You Should not Miss](https://srinimf.com/2024/10/01/5-sql-queries-you-should-not-miss/)DateOctober 1, 2024

References for Recursive

- SQL CTE Recursive nature

### Share this:

-
- [Tweet](https://twitter.com/share)
- [![](https://i0.wp.com/assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png)](https://www.pinterest.com/pin/create/link/?url=https%3A%2F%2Fsrinimf.com%2F2020%2F08%2F30%2Fsql-common-table-expression-best-example%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2020%2F08%2Fpexels-photo-6908502.jpeg%3Ffit%3D1200%252C801%26ssl%3D1&description=How%20to%20Use%20Common%20Table%20Expression%20%5BCTE%5D%20in%20DB2)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2020/08/30/sql-common-table-expression-best-example/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2020/08/30/sql-common-table-expression-best-example/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2020/08/30/sql-common-table-expression-best-example/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2020/08/30/sql-common-table-expression-best-example/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%20How%20to%20Use%20Common%20Table%20Expression%20%5BCTE%5D%20in%20DB2&body=https%3A%2F%2Fsrinimf.com%2F2020%2F08%2F30%2Fsql-common-table-expression-best-example%2F&share=email)
-

### Like this:

Like Loading…
