GeekInterview.com
Series: Subject: Topic:
Question: 72 of 190

how to select the recently UPDATED records from the table?

Asked by: sat.inn | Member Since Feb-2007 | Asked on: Feb 22nd, 2007

View all questions by sat.inn   View all answers by sat.inn

Showing Answers 1 - 5 of 5 Answers
Ashish Prasad

Answered On : Mar 27th, 2007

There is no native methods to fetch recently updated records as the prime functionality for an RDBMS is to store records in best way that makes prefeching faster, so storage is not based on update order but other attributes that makes DBMS fast



If you want to attain the functionality for getting recent record, yo need to create another column in the table that stores the timestamp of the records insterted. Based on that you can filter your records based on the new column data.e.g :SELECT * from ORD O1 where O1.row_insrt_ts > '2006-12-03 00:00:00'

  
Login to rate this answer.
Nitesh Srivastava

Answered On : Sep 14th, 2011

You can execute the following query and get the latest updated record

SELECT *
FROM emp e
WHERE ROWID IN (SELECT MAX (ROWID)
FROM emp e1)

  
Login to rate this answer.

The problem with the question is it is too vague. How recent is recently UPDATED? The past millisecond? second? minute? hour? day? week? month? year? If you are working on Tables in a RDBMS, be it Oracle or SQL-Server or MySQL or DB2 or any others, one should always assume that you're not alone in your access of and work on a Table.

Say you have a Table, call it T1, which has a timestamp column in it, call it LastModified, and you're hoping to get the last Update base on SELECT statement pulling back the most recent record based on the LastModified column. Sounds simple, but you are not the only person doing UPDATES and INSERTS in the table. By the time you're ready to run your SELECT statement somebody else has also done a UPDATE, it's not your UPDATED record that's returned, it's their UPDATED record.

Well, what about using ROWID, well that won't work either since ROWID doesn't change. The only times ROWID will change are:

Import/Export of the table
Altering the Table by MOVE, SHRINK SPACE, or FLASHBACK
Splitting or Combining partitions
Update a value such that it moves the record into a new partition

I know at this point you may be jumping on the "Update a value such that it moves the record into a new partition" but you don't want to be doing that with every Update, thus that will be a very unlikely event.

So, you need to ask, What do you mean by most recent? and What is the scope of the most recent Updates, is it limited to only one user on a table or all users on a table?

SELECT *
FROM Update_Table UT1
WHERE UT1.Modified_Date = (SELECT MAX(UT2.Modified_Date)
FROM Update_Table UT2
WHERE UT2.PK = UT1.PK
AND UT2.Last_User = user)

This query assumes that every user to the database has a individual user id to access the database, and not some generic id that everybody who access the database used.

  
Login to rate this answer.

select * from emp e where rowid in( select max(rowid) from emp );

  
Login to rate this answer.
Ayyappa

Answered On : Aug 24th, 2012

Hello leelakrishna302 ... Your query ...

Code
  1.  
  2. SELECT * FROM emp e
  3. WHERE rowid IN (SELECT max(rowid) FROM emp);
  4.  

gives only latest inserted records not the latest updated records..

  
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.