how to retrieve last tree records from table?
select *from emp where rownum > (select count(*)-3 from
emp);
i am using this query to get last three records from table
but its not giving any output, so please tell me what is the
error in this query.

Answer Posted / sunil bisht

Hi Prakash,

You did one mistake in this query when you are use rownum
is always start fetch the record from very first record
from table
Row num can work only with the follow using operators

=(only with 1)(select * from emp where rownum=1;) if you
are use 2 or other number in place of 1 so result is no
data found;

<(any vlaue)(select * from emp where rownum<12 if you are
using the > sign in place of < sign so result is no data
found

<=(any value)

Between 1 and any value

>=(only with 1)

<>(any value) this will return all records less than the
given value


if you want to retrive last three records from table then
you can used

select * from emp where rownum<=(select count(*) from emp)
minus select *from emp where rownum <=(select count(*)-3
from emp)

you get the last three record from the table

other method is

select * from emp e where 3>(select count(*) from emp d
where d.rowid>e.rowid);

Is This Answer Correct ?    4 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What does the file extension accdb stand for?

545


what is oracle database ? : Sql dba

557


List the ways to get the count of records in a table?

505


does sql support programming? : Sql dba

605


How does postgresql compare to "nosql"?

578






how does a local variable is defined using t-sql? : Transact sql

539


Why stored procedure is faster than query?

515


explain access control lists. : Sql dba

555


Is postgresql a nosql database?

531


What are some emotional triggers?

568


Can a table have no primary key?

551


Why is pl sql used?

525


What is cold data?

501


What is t sql used for?

548


What is before trigger?

550