---
title: "Fetch first 1 Row only Real Usage in DB2"
description: "In DB2 database there are many ways to achieve performance. I explained one way here. Read more."
url: https://srinimf.com/2018/02/25/db2-performance-tips/
date: 2018-02-25
modified: 2026-10-16
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2018/02/pexels-photo-907489.jpeg?fit=1733%2C1300&ssl=1
categories: ["Categories", "DB2"]
tags: ["DB2 Performance tips", "DB2 Tips and Solutions"]
type: post
lang: en
---

# Fetch first 1 Row only Real Usage in DB2

In many projects, the scenario is that you need to find if the rows exist or not for a particular condition.

Performance is the main issue if you write complex SQL queries. The best and latest approach is [to use FETCH](https://srinimf.com/2014/10/26/db2-fetch-first-n-rows-only/) FIRST 1 ROW ONLY. When you specify it in your SQL query, the result will be 1 or more than one row. Our aim is to find whether the rows exist or not.

Related[DB2 Tough Questions (3 of 4)](https://srinimf.com/2012/02/01/db2-tough-questions-3-of-4/)

## The sample SQL query below helps you see a real use of the above phrase.

FETCH FIRST 1 ROW ONLY is as efficient as a correlated subquery, and it is a more straightforward coding style.

Related[DB2 Fetch ideas on how to optimize result](https://srinimf.com/2012/05/02/db2-fetch-rules/)

**Fetch First 1 Row Only:**

In the below query, when a single row is found, you will get SQLCODE as zero. So that you can set the flag as rows found to true.

```
MOVE ZEROS TO HOST-VARIABLE1
SELECT  1
INTO    :HOST-VARIABLE1
FROM    TABLE1
WHERE   COLUMN1    =  :HOST-VARIABLE-X
  AND   COLUMN2    =  :HOST-VARIABLE-Y
FETCH FIRST 1 ROW ONLY

EVALUATE SQLCODE …‥

IF SQLCODE = 0
    SET  ROWS-FOUND   TO TRUE
END-IF
```

### 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%2F2018%2F02%2F25%2Fdb2-performance-tips%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2018%2F02%2Fpexels-photo-907489.jpeg%3Ffit%3D1200%252C900%26ssl%3D1&description=Fetch%20first%201%20Row%20only%20Real%20Usage%20in%20DB2)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2018/02/25/db2-performance-tips/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2018/02/25/db2-performance-tips/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2018/02/25/db2-performance-tips/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2018/02/25/db2-performance-tips/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%20Fetch%20first%201%20Row%20only%20Real%20Usage%20in%20DB2&body=https%3A%2F%2Fsrinimf.com%2F2018%2F02%2F25%2Fdb2-performance-tips%2F&share=email)
-

### Like this:

Like Loading…
