Fetch only the last 5 records from a table?
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?
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?
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.
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..:)