---
title: "5 Tricky DB2 SQL Queries for Practice"
description: "DB2 SQL is popular in mainframe and open source. I have given DB2 tricky SQL queries for your reference."
url: https://srinimf.com/2020/12/29/db2-five-sql-query-tricky-questions/
date: 2020-12-29
modified: 2026-10-10
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2020/12/pexels-photo-5986826.jpeg?fit=1880%2C1253&ssl=1
categories: ["SQL"]
tags: ["Tricky DB2 SQL Queries"]
type: post
lang: en
---

# 5 Tricky DB2 SQL Queries for Practice

![5 Tricky DB2 SQL Queries for Practice](https://i0.wp.com/srinimf.com/wp-content/uploads/2022/12/5-tricky-db2-sql-queries.jpg?resize=800%2C400&ssl=1)

Here are the tricky DB2 [SQL queries](https://srinimf.com/2014/08/17/sql-puzzles/) for practice. **You May Also Like:** [DB2 Developer’s Guide](https://amzn.to/3rGjWKs)

## Tricky DB2 SQL Queries

**1. The SELECT SQL statement used to query data.**

```
SELECT * FROM employee;
```

**2. The INSERT, UPDATE, and DELETE SQL statements used to modify table data.**

```
INSERT INTO employee
VALUES ( 
'000998', 'SMITH', 'A', 'JOHN',  
NULL, NULL, NULL, NULL, 18,
'M', NULL, NULL, NULL, NULL 
);

UPDATE employee
SET (hiredate, workdept) = 
(SELECT CURRENT DATE, deptno
FROM department
WHERE deptname='PLANNING')
WHERE empno='000999';

DELETE from employee 
where empno='000999' 
and deptname='PLANNING';
```

Advertisements

**3. Recursive SQL statements.**

```
WITH temptab (person_id, name, parent_id) AS (1)
(SELECT person_id, name, parent_id           (2)
FROM children
WHERE name = 'Jenny'
UNION ALL                                    (3)
SELECT c.person_id, c.name, c.parent_id      (4)
FROM children c, temptab super
WHERE c.person_id = super.parent_id
) 
SELECT * FROM temptab;                        (5)
```

**4. How to query data that just got inserted, updated, or deleted in the same SQL statement.**

```
SELECT * FROM sales
WHERE sales_date IN ('03/29/1996', '04/01/2006');
```

> “Technology is best when it brings people together.”

**5. The MERGE SQL statement used to combine insert, update, and/or delete operations in one statement.**

```
MERGE INTO T2 as target
    USING (SELECT ... FROM T1) AS source
       ON target.id=source.id
    WHEN NOT MATCHED THEN
        INSERT INTO T2 ...
    WHEN MATCHED THEN
        UPDATE T2 SET ...
```

Keep Reading

- [SQL SELECT Statement 5 Associated Clauses](https://srinimf.com/2017/01/23/tricky-db2-question-on-select-statement/)DateJanuary 23, 2017
- [Mainframe: How to find Primary Key in Zos DB2](https://srinimf.com/2010/07/22/db2-how-to-find-primary-key/)DateJuly 22, 2010
- [NEXT_DAY Vs DAYNAME Top SQL Examples](https://srinimf.com/2020/07/12/next_day-vs-dayname-top-sql-examples/)DateJuly 12, 2020

### 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%2F12%2F29%2Fdb2-five-sql-query-tricky-questions%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2020%2F12%2Fpexels-photo-5986826.jpeg%3Ffit%3D1200%252C800%26ssl%3D1&description=5%20Tricky%20DB2%20SQL%20Queries%20for%20Practice)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2020/12/29/db2-five-sql-query-tricky-questions/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2020/12/29/db2-five-sql-query-tricky-questions/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2020/12/29/db2-five-sql-query-tricky-questions/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2020/12/29/db2-five-sql-query-tricky-questions/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%205%20Tricky%20DB2%20SQL%20Queries%20for%20Practice&body=https%3A%2F%2Fsrinimf.com%2F2020%2F12%2F29%2Fdb2-five-sql-query-tricky-questions%2F&share=email)
-

### Like this:

Like Loading…
