Fetch only the last 5 records from a table?

Asked By 20 points N/A Posted on -
qa-featured

What if I need to fetch only the last 5 records from a table with fields like ID, City, State, and Address? How will I make this possible?

SHARE
Best Answer by Gina Veronica
Best Answer
Best Answer
Answered By 0 points N/A #152083

Fetch only the last 5 records from a table?

qa-featured

Hello Barb Brennan!

The answer is pretty easy. You just need to tell the query to order the records/data/information by descending order and get the last five rows. Here's the code: SELECT ID, City, State, Address FROM TABLE ORDER BY DESC FETCH LAST 5 ROWS ONLY; Code explanation: SELECT – select the columns you want to get TABLE – this is the table where you are going to get the records ORDER BY DESC – order the records in descending FETCH LAST 5 ROWS ONLY – will only get the last 5 row records from the table. You can also use FETCH FIRST 10 ROWS ONLY depending on what you want to achieve.

Answered By 0 points N/A #152084

Fetch only the last 5 records from a table?

qa-featured

Hello Barb Brennan,

There is very simple solution for this particular problem

You could use a loop if you are using PL/SQL to fetch only five records

Steps:

1. Firstly arrange the data in descending order or reverse order whatever is applicable.

2. Make a loop which starts from 1 and ends at 5 with one increment.

3. In Body write your code to fetch the 5th row. (Where 1<=i<=5 )

That's It.

Enjoy..:)

Related Questions