Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


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.

Answers were Sorted based on User's Feedback



how to retrieve last tree records from table? select *from emp where rownum > (select count(*)-3..

Answer / alok narayan

select * from (select * from emp order by rowid desc )
where rownum <= 3 order by rowid;

Is This Answer Correct ?    6 Yes 0 No

how to retrieve last tree records from table? select *from emp where rownum > (select count(*)-3..

Answer / mah6326955

SELECT empno
FROM (SELECT ROWNUM r, emp_code
FROM emp
ORDER BY empno DESC) t1
WHERE t1.r <= 3

Is This Answer Correct ?    9 Yes 4 No

how to retrieve last tree records from table? select *from emp where rownum > (select count(*)-3..

Answer / 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

how to retrieve last tree records from table? select *from emp where rownum > (select count(*)-3..

Answer / gnreddy

try this one.
select * from emp where 3 >( select count(1) from emp e where e.rowid > emp.rowid);

Is This Answer Correct ?    7 Yes 4 No

how to retrieve last tree records from table? select *from emp where rownum > (select count(*)-3..

Answer / pavithra

select * from emp where rowid in(select rowid from emp
where rownum<=&upto minus select rowid from emp where
rownum<&start)

Is This Answer Correct ?    4 Yes 1 No

how to retrieve last tree records from table? select *from emp where rownum > (select count(*)-3..

Answer / alok narayan

Rownum assigned at the runtime on the basis of rownum you
not fetch last three records.

for no records output: Rownum always use <= or >= , if use
rownum < or > its giving no output.

Is This Answer Correct ?    1 Yes 0 No

how to retrieve last tree records from table? select *from emp where rownum > (select count(*)-3..

Answer / ramareddy

u don't use minum operator to count(*) in subquerry

Is This Answer Correct ?    4 Yes 4 No

how to retrieve last tree records from table? select *from emp where rownum > (select count(*)-3..

Answer / ammu

Hi Prakash Kumar,

this is the problem with rownum. In general never use = or
> with rownum. I will explain the reason.

Siuppose u hav 2 rowns in a table.

when u say select * from emp where rownum =2.

then 2 row will get selected ...but as soon as it gets
selected..since it is the only row in the selection its row
num will change to 1.

so this will return 0 rows.

similarly..

when u say rownum >1
2 row will get selected but as soon as it get selected its
rownum changes to 1.. so no rows selected again

Is This Answer Correct ?    1 Yes 1 No

how to retrieve last tree records from table? select *from emp where rownum > (select count(*)-3..

Answer / bikash khuntia

select * from (
select rownum rw,bk.sal from
(select sal from TEMP_SAL order by rowid desc) bk) bik
where bik.rw<=3

Is This Answer Correct ?    0 Yes 0 No

how to retrieve last tree records from table? select *from emp where rownum > (select count(*)-3..

Answer / arvind kumar

select * from employees minus select * from employees where
rownum < (select count(*)-2 from employees);

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More SQL PLSQL Interview Questions

What is nested table in pl sql?

0 Answers  


I have a package in which a table was used in a procedure and compiled later i have dropped the table used in the package what would be the status of package specification and body

1 Answers   TCS,


What are the string functions in sql?

0 Answers  


how to check the 3rd max salary from an employee table? One of the queries used is as follows: select sal from emp a where 3=(select count(distinct(sal)) from emp b where a.sal<=b.sal). Here in the sub query "select count(distinct(sal)) from emp b where a.sal<=b.sal" or "select count(distinct(sal)) from emp b where a.sal=b.sal" should reveal the same number of rows is in't it? Can any one here please explain me how is this query working perfectly. However, there is another query to get the 3rd highest of salaries of employees that logic I can understand. Pls find the query below. "select min(salary) from emp where salary in(select distinct top 3 salary from emp order by salary desc)" Please explain me how "select sal from emp a where 3=(select count(distinct(sal)) from emp b where a.sal<=b.sal)" works source:http://www.allinterview.com/showanswers/33264.html. Thanks in advance Regards, Karthik.

4 Answers  


Is sql the best database?

0 Answers  


Does sql support programming?

0 Answers  


What is the difference between having and a where in sql?

0 Answers  


How to combine two stored procedures in sql?

0 Answers  


What are the most important characteristics of pl/sql?

0 Answers  


What are % type and % rowtype?

0 Answers  


Easy way to convert tableau "IF - ELSEIF" statements to Netezza "CASE" statements.

1 Answers   CTS,


what is the difference between a local and a global temporary table? : Sql dba

0 Answers  


Categories