How to fetch last n rows only in DB2?

2 posts / 0 new
Last post
Anonymous
How to fetch last n rows only in DB2?

 

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                                       
"; ; ; ;
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
Log in or register to post comments
Sponsored Listing