Skip to Content
Custom Search

How to fetch last n rows only in DB2?

1 reply [Last post]
Anonymous

 

I Know there is a command in SQL to fetch first n rows only
 
For example:-
 
SELECT Employee_no, Salary FROM Employee-table
WHERE Salary > 10000
FETCH FIRST 10 ROWS ONLY
 
The above query will fetch first 10 rows from Employee-table
 
But if I have to fetch last 10 rows from a table, I tried using 
 
 
 "FETCH LAST 10 ROWS only"
 
 
But this command gave me error 
 
SQLCODE = -104, ERROR:  ILLEGAL SYMBOL "LAST". SOME SYMBOLS THAT
MIGHT BE LEGAL ARE: FIRST                                       
"; ; ; ;
Sponsored Listing
Anonymous
How to fetch last n rows only in DB2?

 

You can use ORDER BY clause ... the syntax is as below
 
SELECT COL_A
FROM YOUR.TABLE
WHERE KEY_A = :KEY-A
ORDER BY COL_A DESC
FETCH FIRST n ROWS ONLY

Click the +1 button  below to share it with your friends and colleagues

 

Share this if you liked it!

 

Disclaimer



Who's online

There are currently 0 users and 45 guests online.
Dr. Radut | forum