How do I retrieve the last row of a table?

Showing Answers 1 - 75 of 91 Answers

sureshkumar

  • Mar 16th, 2006
 

how doyou write the code for login and password ?

gomathi

  • Mar 18th, 2006
 

select * from having rowid=(select max(rowid) from )

  Was this answer useful?  Yes

Amit Kumar

  • Apr 6th, 2006
 

SELECT emp_id, lname, fname, job_id, (SELECT COUNT(*) FROM employee e2 WHERE e2.lname <= e.lname ) AS RowNumber
FROM employee e
Where (SELECT COUNT(*) FROM employee e2 WHERE e2.lname <= e.lname ) = (Select count(*) from employee)

  Was this answer useful?  Yes

by baiju

  • Feb 25th, 2007
 

select *from emp where  rowid=(select max(rowid) from emp)

(select rownum,emp.* from emp
where
rownum<=
(select max(rownum) from emp))
minus
(select rownum,emp.* from emp
where
rownum<=
(select (max(rownum)-1) from emp))

  Was this answer useful?  Yes

Spectromage

  • Jul 31st, 2008
 

If you are looking for an SQL server 2005 query then the following:

SELECT? rowid
FROM
emp
WHERE? (rowid =

? (SELECT? COUNT(*) AS Expr1
? FROM? emp AS emp_1))

  Was this answer useful?  Yes


If you are using MS SQL Server 2005 then following command will help you to get the last row of the table.

select top 1 * from table_name order by column_name desc;

Note:
To get the first row, you can use

select top 1 * from table_name;

Thanks,
Deepali Shahabadi

  Was this answer useful?  Yes

In order to retrieve the last row of a table for MS SQL database 2005, You can use the following query:

select top 1 column_name from table_name order by column_name desc;

// Note: To get the first row of the table for MS SQL database 2005, You can use the following query:

select top 1 column_name from table_name;

Thanks,
Deepali Shahabadi

  Was this answer useful?  Yes

jnvpankaj

  • Sep 23rd, 2010
 

If identity column is present table then
select top 1 * from table_name order by 1 desc

If identity column is not present in table then
select top 1 *,row_number() over(order by (select 1)) as num from table_name order by num desc

use it and enjoy

  Was this answer useful?  Yes

The correct ans is select * from table_name where rowid =(select max(rowid) from table_name ) . . If table name is employees then . . Select*from employees where rowid=(select max(rowid) from employees); that's it . .

  Was this answer useful?  Yes

triger07

  • Dec 21st, 2010
 

select * from tablename where id=(  select count(*) from tablename)

it is correct when id column is in order(1,2,3,4...), but if the id is not in order(1,2,9,4,5,8... ) means then how....

  Was this answer useful?  Yes

Abhijeet singh

  • Jul 21st, 2011
 

Code
  1. SELECT * FROM emp

  2. WHERE ROWID=(SELECT MAX(ROWID) FROM emp )

  Was this answer useful?  Yes

Rashmi Ranjan Pattnaik

  • Sep 9th, 2011
 

select top 1 Column_Name from Table_Name where Column_Name = 'Condiion' order by Sl_No DESC

  Was this answer useful?  Yes

Chetan Kantharia

  • Sep 18th, 2011
 

SELECT * FROM employees ORDER BY rownum DESC LIMIT 1

  Was this answer useful?  Yes

Aarya

  • Dec 6th, 2011
 

This SQL Statement will retrieve the last row from table emp..

Code
  1. SELECT * FROM(SELECT rownum nbr, t.* FROM emp t ORDER BY empno)

  2. WHERE nbr>(SELECT count(*)-1 FROM emp)

  Was this answer useful?  Yes

ashok panguluri

  • Feb 11th, 2012
 

Code
  1. SELECT * FROM emp WHERE rowid=(SELECT max(rowid) FROM emp);


We can retrieve details of the last row of given emp table use the max(rowid).

  Was this answer useful?  Yes

ARNAB

  • Mar 31st, 2012
 

How to find last 5 entries to a table?

  Was this answer useful?  Yes

Balaji.S

  • Apr 16th, 2012
 

We can use Stored Procedure for this kind of Process, like


Code
  1. CREATE procedure procName

  2.  AS

  3.  begin

  4. DECLARE @COUNT INT

  5. SELECT @COUNT=COUNT(*) FROM TABLE

  6. SELECT TOP @COUNT * FROM TABLE WHERE PRIMARY KEY FIELD NOT IN (SELECT TOP @COUNT-5 PRIMARY KEY FIELD FROM TABLE)

  7.  end

  8.  

  9.  

  10. Execute This Query BY

  11.  

  12. exec procName

  Was this answer useful?  Yes

Ambarish

  • Apr 19th, 2012
 

Select last(column_name) from Table.

  Was this answer useful?  Yes

Arbind Kumar Singh

  • Jul 23rd, 2012
 

sql query for retrieving last row particular column value depending upon its ID???

QUERY:
select top 1 [column_name1],[column_name2] from [table_name] where ID=[ID_value] order by index desc


here index is a column name which contain the number of row in the table..the above Query help you to retrieve last row in case when you dont the index contain value.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions