Page 1 of 2

How to get the last 10 records from a table

PostPosted: Thu Dec 27, 2007 10:39 am
by anupam
How to get last 10 records from a table using SQL query?

Re: How to get the last 10 records from a table

PostPosted: Thu Jan 03, 2008 11:45 am
by the_gautam
select * from <table-name> A where 10 > ( select count (*) from <table-name> B where A.<column-name> = B.<column-name> );

Re: How to get the last 10 records from a table

PostPosted: Mon Jan 14, 2008 4:34 pm
by ronmathew
the_gautam wrote:select * from <table-name> A where 10 > ( select count (*) from <table-name> B where A.<column-name> = B.<column-name> );


I cannot understand ur solution...

Do v get the 10 record with this...and it has to be the last records....plz explain!!

Re: How to get the last 10 records from a table

PostPosted: Wed Jan 16, 2008 1:59 am
by dick scherrer
Hello,

There is no such thing as the "last 10 rows" in a table.

Re: How to get the last 10 records from a table

PostPosted: Wed Jan 16, 2008 5:30 pm
by ronmathew
dick scherrer wrote:Hello,

There is no such thing as the "last 10 rows" in a table.


SO what do we say...no last 10 rows...10 records in a table

Re: How to get the last 10 records from a table

PostPosted: Wed Jan 16, 2008 6:07 pm
by arunprasad.k
Use FETCH FIRST and ORDER BY (DESC) to get the last n records.

Refer the URL:

http://publibz.boulder.ibm.com/cgi-bin/bookmgr_OS390/BOOKS/DSNSQH11/4.4.7?SHELF=&DT=20010718164132

Especially the last paragraph.

Arun.

Re: How to get the last 10 records from a table

PostPosted: Thu Jan 17, 2008 12:42 am
by dick scherrer
Hello,

SO what do we say...no last 10 rows...10 records in a table
We should not use that kind of terminology. First/last are sequential file concepts. When teachers or text material use this sort of reference for database activity, they are mis-leading the students.

Re: How to get the last 10 records from a table

PostPosted: Tue Apr 01, 2008 7:27 pm
by edward.lan
hi, try this

select * from TABLE
order by KEY DESC
fetch first 10 rows only

edward

Re: How to get the last 10 records from a table

PostPosted: Tue Apr 01, 2008 9:40 pm
by dick scherrer
Hello edward.lan and welcome to the forums,

Please understand that the "solution" you posted does not "get the last 10" in the table - as mentioned before, there is no last 10 (nor is there a first 10). . . Databases to not have a front and a back row or a beginning and an end row.

Your query should return the 10 rows with the highest KEY, but that does not make them the "last 10".

Re: How to get the last 10 records from a table

PostPosted: Wed Apr 30, 2008 5:11 pm
by progressabhi1
SELECT * FROM <table-name>
WHERE SEQ_NO >
(SELECT MAX(SEQ_NO)-20 FROM <table-name> );
====
note:-where SEQ_NO must be a column(attribute) in the table
*************************************************************************************************************
SELECT * FROM <table-name>
ORDER BY SEQ_NO DESC
FETCH FIRST 20 ROWS ONLY;
====
This one is just in accordance to the previous query,but with arrangment done in descending order.
*************************************************************************************************************