---
title: "lastval()"
url: "https://docs.yugabyte.com/stable/api/ysql/exprs/sequence_functions/func_lastval/"
---

# lastval()

Returns the value returned from the last call to nextval(), for any sequence, in the current session.

## Synopsis

Use the `lastval()` function to return the value returned from the last call to `nextval()`, for any sequence, in the current session.

## Semantics

- An error is raised if `nextval()` has not been called in the current session.

## Examples

Create two sequences and call `nextval()` for each of them.

```plpgsql
yugabyte=# CREATE SEQUENCE s1;
```

```output
CREATE SEQUENCE
```

```plpgsql
yugabyte=# CREATE SEQUENCE s2 START -100 MINVALUE -100;
```

```output
CREATE SEQUENCE
```

```plpgsql
yugabyte=# SELECT nextval('s1');
```

```output
 nextval
---------
       1
(1 row)
```

```plpgsql
yugabyte=# SELECT nextval('s2');
```

```output
 nextval
---------
    -100
(1 row)
```

Call `lastval()`.

```plpgsql
yugabyte=# SELECT lastval()
```

```output
 lastval
---------
    -100
(1 row)
```

## See also

- [`CREATE SEQUENCE`](/stable/api/ysql/the-sql-language/statements/ddl_create_sequence "CREATE SEQUENCE")
- [`DROP SEQUENCE`](/stable/api/ysql/the-sql-language/statements/ddl_drop_sequence "DROP SEQUENCE")
