Sqlite Last_insert_rowid Function

SQLite: last_insert_rowid Function

This SQLite post explains how to use the SQLite last_insert_rowid function with syntax and examples.

Description

The SQLite last_insert_rowid function returns the ROWID of the last INSERT in the database for the current session.

Syntax

The syntax for the last_insert_rowid function in SQLite is:

last_insert_rowid()

Parameters or Arguments

There are no parameters or arguments for the last_insert_rowid function.

Applies To

The last_insert_rowid function can be used in the following versions of SQLite:

  • SQLite 3.8.6, SQLite 3.8.x, SQLite 3.7.x, SQLite 3.6.x

Example

Let's look at some SQLite last_insert_rowid function examples and explore how to use the last_insert_rowid function in SQLite.

For example, if we had the following employees table:

CREATE TABLE employees 
( employee_id int,
  last_name varchar(50),
  first_name varchar(50) );

And the employees table contained the following records:

employee_id last_name first_name
1000 Smith Jane
1001 Mark Dave

And we executed the following INSERT statement:

INSERT INTO employees
(employee_id, last_name, first_name)
VALUES
(1002, 'Jones', 'Sarah');

The employees table would now look like this:

employee_id last_name first_name
1000 Smith Jane
1001 Mark Dave
1002 Jones Sarah

And if we executed the last_insert_rowid function as follows:

sqlite> SELECT last_insert_rowid();
Output: 3

The last_insert_rowid function would return 3 since the last INSERT statement inserted a record into the employees table with a ROWID of 3.