JT,
First of all, Oracle doesn't store rows in any particular order in a table. So, when you say "Getting Last 25 Rows of a Table", are you interested in:
1) the last
physical 25 (possibly randomly ordered) rows (which probably mean nothing from a business perspective), or
2) the last
logical 25 rows, based upon some order that you specify?
In either case, there is code, below, that gives gives you any number of (first or last) rows from any table you want. First, let's post some sample data from a table:
Code:
select last_name, salary from s_emp;
LAST_NAME SALARY
------------------------- ----------
Velasquez 2500
Ngao 1450
Nagayama 1400
Quick-To-See 1450
Ropeburn 1550
Urguhart 1200
Menchu 1250
Biri 1100
Catchpole 1300
Havel 1307
Magee 1400
Giljum 1490
Sedeghi 1515
Nguyen 1525
Dumas 1450
Maduro 1400
Smith 940
Nozaki 1200
Patel 795
Newman 750
Markarian 850
Chang 800
Patel 795
Dancs 860
Schwartz 1100
25 rows selected.
Let's now run my script, "LastNRows.sql", to show some behaviours of this script:
Code:
SQL> @LastNRows
How many of the 'last' rows of a table do you wish to see?: 4
Enter the (possibly qualified) name of the table
from which you wish to extract the last 4 rows: test.s_emp
Enter the expressions you wish to SELECT/display: salary, last_name
Complete this clause: 'Order by ...': salary
[i](Note: order by "salary" gives lowest paid 4 employees)[/i]
SALARY LAST_NAME
---------- -------------------------
750 Newman
795 Patel
795 Patel
800 Chang
SQL> @LastNRows
How many of the 'last' rows of a table do you wish to see?: 3
Enter the (possibly qualified) name of the table
from which you wish to extract the last 3 rows: s_emp
Enter the expressions you wish to SELECT/display: salary, last_name
Complete this clause: 'Order by ...': salary desc
[i](Note: order by "salary [b]desc[/b]" gives highest paid 3 employees)[/i]
SALARY LAST_NAME
---------- -------------------------
2500 Velasquez
1550 Ropeburn
1525 Nguyen
SQL> @LastNRows
How many of the 'last' rows of a table do you wish to see?: 3
Enter the (possibly qualified) name of the table
from which you wish to extract the last 3 rows: s_emp
Enter the expressions you wish to SELECT/display: last_name
Complete this clause: 'Order by ...': rownum
[i](Note: order by "rownum" gives first 3 physical rows of table)[/i]
LAST_NAME
-------------------------
Velasquez
Ngao
Nagayama
SQL> @LastNRows
How many of the 'last' rows of a table do you wish to see?: 3
Enter the (possibly qualified) name of the table
from which you wish to extract the last 3 rows: s_emp
Enter the expressions you wish to SELECT/display: last_name
Complete this clause: 'Order by ...': rownum desc
[i](Note: order by "rownum [b]desc[/b]" gives last 3 physical rows of table)[/i]
LAST_NAME
-------------------------
Schwartz
Dancs
Patel
Now, here is the code for the "LastNRows.sql" script:
Code:
accept cnt prompt "How many of the 'last' rows of a table do you wish to see?: "
prompt
prompt Enter the (possibly qualified) name of the table
accept tabname prompt " from which you wish to extract the last &cnt rows: "
prompt
accept Selects prompt "Enter the expressions you wish to SELECT/display: "
prompt
accept orderby prompt "Complete this clause: 'Order by ...': "
prompt
select &selects
from (select rownum rn, x.*
from (select * from &tabname
order by &orderby) x)
where rn <= &cnt;
Let us know if you have questions.
![[santa] [santa] [santa]](/data/assets/smilies/santa.gif)
Mufasa
(aka Dave of Sandy, Utah, USA)
[I provide low-cost, remote Database Administration services:
www.dasages.com]