GeekInterview.com
Series: Subject: Topic:
Question: 332 of 427

How do I retrieve the last row of a table?

Asked by: Interview Candidate | Asked on: Mar 14th, 2006
Showing Answers 1 - 37 of 37 Answers
sureshkumar

Answered On : Mar 16th, 2006

How doyou write the code for login and password ?

Yes  1 User has rated as useful.
  
Login to rate this answer.
gomathi

Answered On : Mar 18th, 2006

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

  
Login to rate this answer.
kumarbp2005

Answered On : Mar 20th, 2006

View all answers by kumarbp2005

The Query is:SELECT * FROM(SELECT rownum nbr, t.* FROM dept t order by deptno) where nbr>(select count(*)-1 from dept)

Yes  1 User has rated as useful.
  
Login to rate this answer.
rahultripathi

Answered On : Mar 20th, 2006

View all answers by rahultripathi

on SQL Server     select Top 1 * from table order by en Descin Oracle select * from having rowid=(select max(rowid) from )If any prob don't hesitate  rahultripathi@myway.com 

  
Login to rate this answer.
Amit Kumar

Answered On : Apr 6th, 2006

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

  
Login to rate this answer.
nileshsingh

Answered On : May 19th, 2006

View all answers by nileshsingh

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

  
Login to rate this answer.
by baiju

Answered On : Feb 25th, 2007

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

Yes  1 User has rated as useful.
  
Login to rate this answer.

SELECT * FROM WHERE ROWID =(SELECT MAX (ROWID FROM<TABLE NAME)

  
Login to rate this answer.

Hi this is the ans to display last record  along with rownum.Asume that the table name is empselect rownum,emp.*from emp where  rowid=(select max(rowid) from emp)

Yes  1 User has rated as useful.
  
Login to rate this answer.
tariq_oracle

Answered On : May 12th, 2008

View all answers by tariq_oracle

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

  
Login to rate this answer.
mohd sajjad

Answered On : Jun 25th, 2008

View all answers by mohd sajjad

select Sal from emp minus select Sal from emp where rownum

  
Login to rate this answer.
Spectromage

Answered On : Jul 31st, 2008

View all answers by Spectromage

  
Login to rate this answer.

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

  
Login to rate this answer.
gowthambaalaji

Answered On : Feb 26th, 2009

View all answers by gowthambaalaji

Use Go bottomSelect * from Table name Now it will reterive the last row of a table

  
Login to rate this answer.

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

  
Login to rate this answer.
smithveg

Answered On : May 31st, 2009

View all answers by smithveg

It should be:SELECT * FROM tablename ORDER BY field_id DESC limit 0,1

  
Login to rate this answer.
dj_dj_dj

Answered On : Jun 2nd, 2009

View all answers by dj_dj_dj

Answer: Select * from table_name where rowid in (select max(row_id) from table_name)

  
Login to rate this answer.
halammal29

Answered On : Dec 7th, 2009

View all answers by halammal29

Select *from tablename order by colname desc limit 1;

  
Login to rate this answer.
hmounir

Answered On : Dec 9th, 2009

View all answers by hmounir

SELECT * FROM(SELECT * FROM employees ORDER BY rownum DESC)WHERE rownum < 2

  
Login to rate this answer.
info_overload

Answered On : Sep 17th, 2010

View all answers by info_overload

You would have to use a column in the table that you know gives you the last row. So for instance a timestamp of when the row was inserted.

  
Login to rate this answer.
jnvpankaj

Answered On : Sep 23rd, 2010

View all answers by jnvpankaj

If identity column is present table thenselect top 1 * from table_name order by 1 descIf identity column is not present in table thenselect top 1 *,row_number() over(order by (select 1)) as num from table_name order by num descuse it and enjoy

  
Login to rate this answer.

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 . .

  
Login to rate this answer.
triger07

Answered On : Dec 21st, 2010

View all answers by triger07

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....

  
Login to rate this answer.
Shen Guow Lee

Answered On : Apr 26th, 2011

View all answers by Shen Guow Lee

Use the following query to achieve your purpose:select * from (select * from tmname order by xxx) where rownum=1

  
Login to rate this answer.
vivekrajece

Answered On : May 19th, 2011

View all answers by vivekrajece

Select * from tablename where rowid=(select max(rowid) from tablename);

  
Login to rate this answer.
Dheeran Bala

Answered On : Jun 17th, 2011

View all answers by Dheeran Bala

Select top 1 * from table_name orderby column_id desc

  
Login to rate this answer.
hmounir

Answered On : Jun 18th, 2011

View all answers by hmounir

Select * from (select * from employees order by rownum desc) where rownum < 2;

  
Login to rate this answer.
Abhijeet singh

Answered On : Jul 21st, 2011

Code
  1. SELECT * FROM emp
  2. WHERE ROWID=(SELECT MAX(ROWID) FROM emp )

  
Login to rate this answer.
Rashmi Ranjan Pattnaik

Answered On : Sep 9th, 2011

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

  
Login to rate this answer.
Chetan Kantharia

Answered On : Sep 18th, 2011

SELECT * FROM employees ORDER BY rownum DESC LIMIT 1

  
Login to rate this answer.
Aarya

Answered On : Dec 6th, 2011

View all answers by Aarya

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)

  
Login to rate this answer.

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

  
Login to rate this answer.
ashok panguluri

Answered On : 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).

  
Login to rate this answer.
ARNAB

Answered On : Mar 31st, 2012

How to find last 5 entries to a table?

  
Login to rate this answer.
Balaji.S

Answered On : 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

  
Login to rate this answer.
Ambarish

Answered On : Apr 19th, 2012

Select last(column_name) from Table.

  
Login to rate this answer.
Arbind Kumar Singh

Answered On : 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.

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.