How to retrieve last record from a table
for eg we have emp table from that i wan to retriew last inserted record without seeing or knowing how can i get it
Printable View
How to retrieve last record from a table
for eg we have emp table from that i wan to retriew last inserted record without seeing or knowing how can i get it
Dear Friend,
Try this,
[B][COLOR="DarkSlateBlue"]SELECT * FROM [/COLOR][/B](
[B][COLOR="Teal"]SELECT ROWNUM [/COLOR][/B]RN,A.* FROM TABLE_NAME A [B][COLOR="teal"]WHERE[/COLOR][/B] [COLOR="Indigo"][B]ROWNUM[/B][/COLOR] < ([COLOR="Purple"][B]SELECT COUNT(*)+1 FROM TABLE_NAME[/B][/COLOR]) )
[COLOR="DarkGreen"][B]WHERE RN [/B][/COLOR]= ([COLOR="purple"][B]SELECT count(*) FROM TABLE_NAME[/B][/COLOR])
Have a pleasant time.
Thank your for your query sreekumar.
What is the use of
WHERE ROWNUM < (SELECT COUNT(*)+1 FROM TABLE_NAME
in the above query.We can get output even without this where clause.
[QUOTE=krishnaindia2007;23124]Thank your for your query sreekumar.
What is the use of
WHERE ROWNUM < (SELECT COUNT(*)+1 FROM TABLE_NAME
in the above query.We can get output even without this where clause.[/QUOTE]
Dear Krishna,
Thank you for your reply.
Yes you are right, we can get the output without
WHERE ROWNUM < (SELECT COUNT(*)+1 FROM TABLE_NAME
Actually i was first selecting all rows using the above WHERE clause.
Now from this list, i was selecting the last row. COUNT(*)+1 was used
just to select all rows. Because if i use only count(*) then i might no get
the last row, thats why.
Well your query is right.
Thank you once again.
Have a pleasant time.
Merry Christmas
Try this also
select * from emp
minus
select * from emp where ROWNUM < (select max(rownum) from emp)
In sql server hw we will do that
[QUOTE=susarlasireesha;23258]Try this also
select * from emp
minus
select * from emp where ROWNUM < (select max(rownum) from emp)[/QUOTE]
in sql server hw we will do this
[QUOTE=amisha_me;23260]in sql server hw we will do this[/QUOTE]
for sql i posted above query ,for sql server use this
select top 1 * from TABLE_NAME order by ID desc
[QUOTE=susarlasireesha;23262]for sql i posted above query ,for sql server use this
select top 1 * from TABLE_NAME order by ID desc[/QUOTE]
select * from emp
minus
(select * from emp where ROWNUM < (select top 1 * from productstate order by productid desc)
There is no TOP keyword in awl server