MySQL
SQL
database
ID retrieval
last updated row

How to get ID of the last updated row in MySQL?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

There is no direct MySQL equivalent of LAST_INSERT_ID() for UPDATE statements. That is the key fact behind this question. An update does not automatically produce one special "last updated row ID," especially when the statement can affect multiple rows. If you need that ID, the correct approach depends on how the update is written and what "last updated" is supposed to mean in your application.

If you update by primary key, you already know the ID

The simplest case is an update that targets one row by its primary key. In that case, the ID is not something MySQL needs to discover later, because you already used it in the WHERE clause.

sql
UPDATE users
SET email = '[email protected]'
WHERE id = 42;

Here the updated row ID is 42. There is nothing else to ask the database afterward.

This sounds obvious, but it is the right answer surprisingly often.

Multi-row updates do not have one natural "last row"

A statement like this may update many rows:

sql
UPDATE orders
SET status = 'archived'
WHERE created_at < '2024-01-01';

What would the "last updated row" mean here?

  • the row with the highest ID?
  • the last row visited internally by the engine?
  • the row most recently modified by timestamp?

SQL does not define one special answer. For multi-row updates, you need your own rule and usually your own query or schema support.

Add an updated_at column if you need "most recently changed"

If the business question is really "which row was updated most recently," add a timestamp column and write your update so it records that fact.

sql
1UPDATE users
2SET email = '[email protected]',
3    updated_at = CURRENT_TIMESTAMP
4WHERE id = 42;

Then you can query by that timestamp:

sql
1SELECT id, updated_at
2FROM users
3ORDER BY updated_at DESC
4LIMIT 1;

This gives meaning to "last updated." Without a stored timestamp or log, the concept does not exist reliably.

Use transactions when the update and follow-up must stay connected

If you update one row and then immediately need its data back, wrap the logic in a transaction and query by the same key.

sql
1START TRANSACTION;
2
3UPDATE users
4SET email = '[email protected]',
5    updated_at = CURRENT_TIMESTAMP
6WHERE id = 42;
7
8SELECT id, email, updated_at
9FROM users
10WHERE id = 42;
11
12COMMIT;

This is much more reliable than trying to infer some session-global "last updated ID" that MySQL does not actually track.

Use an audit table or trigger for history

If you need to know which rows were changed over time, not just in one immediate code path, record that history explicitly.

sql
1CREATE TABLE user_update_log (
2    log_id BIGINT AUTO_INCREMENT PRIMARY KEY,
3    user_id BIGINT NOT NULL,
4    changed_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
5);

Then your application or trigger can insert into that log whenever a row changes. Once you do that, "last updated row" becomes a query against the log rather than a guess.

Why LAST_INSERT_ID() does not help

LAST_INSERT_ID() is specifically about auto-increment inserts. It does not tell you which row an UPDATE touched. If you see code trying to call it after an update, that is a sign the wrong mental model is being applied.

This is one of the most common misunderstandings around MySQL metadata functions.

Common Pitfalls

The biggest mistake is assuming MySQL remembers one global "last updated row ID" for an UPDATE. It does not.

Another issue is asking for the last updated row after a multi-row update without defining what "last" should mean. SQL engines do not guarantee an application-friendly row order for that idea.

People also reach for LAST_INSERT_ID() because it sounds close to the problem. It is only for insert-generated auto-increment values.

Finally, if you truly need update history, store it explicitly with timestamps, audit tables, or triggers instead of trying to reconstruct it after the fact.

Summary

  • MySQL has no built-in LAST_UPDATE_ID() function.
  • If you update by primary key, you already know which row was changed.
  • Multi-row updates do not have one meaningful built-in "last updated row."
  • Add updated_at or audit logging if you need to query the most recently changed row later.
  • Do not use LAST_INSERT_ID() to solve an update-tracking problem.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.