Difficulty: Medium
Correct Answer: Order the rows in descending order by a suitable column and use FETCH FIRST 5 ROWS ONLY to return the last five entries.
Explanation:
Introduction / Context:
Returning only the last few rows of a result set is a common requirement in reporting and application development. In DB2, this is usually done by combining a meaningful ordering column, such as a timestamp or identity column, with a row limiting clause. This question tests knowledge of ORDER BY and FETCH FIRST syntax rather than imaginary SQL keywords.
Given Data / Assumptions:
Concept / Approach:
In DB2, result sets have no inherent order unless an ORDER BY clause is used. To get the last five rows, we first decide what last means, usually based on the highest values in a timestamp or sequence column. We then order the rows in descending order and apply FETCH FIRST 5 ROWS ONLY. In some designs, an outer query might reorder them ascending again, but the core idea is descending order plus row limiting.
Step-by-Step Solution:
Step 1: Identify a column such as TRANS_DATE or ID that represents the natural ordering of the data.
Step 2: Write a SELECT statement with ORDER BY that column in descending order so that the most recent or largest rows appear first.
Step 3: Append FETCH FIRST 5 ROWS ONLY to restrict the result set to five rows.
Step 4: Optionally, wrap this query as a subquery and reorder those five rows in ascending order for user friendly display.
Step 5: Recognize that no special LAST keyword is needed; ordering and row limiting are sufficient.
Verification / Alternative check:
You can test this pattern by running a query such as SELECT * FROM TRANS ORDER BY TRANS_DATE DESC FETCH FIRST 5 ROWS ONLY. When compared with an ordered list of all rows, you will see that only the five rows with the highest TRANS_DATE values are returned. This confirms that the technique correctly extracts the last five logical entries according to the chosen sort key.
Why Other Options Are Wrong:
Option B is wrong because DB2 does not support SELECT LAST 5 ROWS ONLY syntax, and without ORDER BY there is no defined row order.
Option C is wrong because REORG is a utility for physical reorganization, not a query tool for limiting rows; it does not replace ORDER BY and FETCH FIRST.
Option D is wrong because DB2 clearly supports row limiting via FETCH FIRST n ROWS ONLY or similar clauses and does not always have to return all rows.
Common Pitfalls:
A common pitfall is to assume that rows are inherently stored and returned in insertion order. Without ORDER BY, queries can return rows in any sequence that suits the optimizer. Another mistake is to misuse FETCH FIRST without specifying an ordering column, which produces an arbitrary subset rather than the true last rows. Developers should always combine ordering with row limiting when they care about specific portions of the ordered result set.
Final Answer:
The correct method is to order the rows in descending order by a suitable column and use FETCH FIRST 5 ROWS ONLY to return the last five entries.
Discussion & Comments