---
title: Query first and last values
description: Use first() or last() to return the first or last point in an input table.
url: https://docs.influxdata.com/influxdb/v2/query-data/flux/first-last/
estimated_tokens: 2125
product: InfluxDB OSS v2
version: v2
---

# Query first and last values

This page documents an earlier version of InfluxDB OSS. [InfluxDB 3 Core](/influxdb3/core/) is the latest stable version.

#### API token hashing is enabled by default in InfluxDB OSS 2.9.0

Stronger token security: tokens are stored as hashes on disk, so a copy of the database file doesn’t expose usable tokens. Existing tokens are hashed on first startup and the original strings can’t be recovered afterward — **capture any plaintext tokens you still need before you upgrade**.

For more information, see [Token hashing](/influxdb/v2/admin/tokens/#token-hashing).

Use [`first()`](/flux/v0/stdlib/universe/first/) or [`last()`](/flux/v0/stdlib/universe/last/) to return the first or last record in an input table.

```js
data
    |> first()

// OR

data
    |> last()
```

By default, InfluxDB returns results sorted by time, however you can use the [`sort()` function](/flux/v0/stdlib/universe/sort/) to change how results are sorted. `first()` and `last()` respect the sort order of input data and return records based on the order they are received in.

### first

`first()` returns the first non-null record in an input table.

**Given the following input:**

| _time | _value |
| --- | --- |
| 2020-01-01T00:01:00Z | 1.0 |
| 2020-01-01T00:02:00Z | 1.0 |
| 2020-01-01T00:03:00Z | 2.0 |
| 2020-01-01T00:04:00Z | 3.0 |

**The following function returns:**

```js
|> first()
```

| _time | _value |
| --- | --- |
| 2020-01-01T00:01:00Z | 1.0 |

### last

`last()` returns the last non-null record in an input table.

**Given the following input:**

| _time | _value |
| --- | --- |
| 2020-01-01T00:01:00Z | 1.0 |
| 2020-01-01T00:02:00Z | 1.0 |
| 2020-01-01T00:03:00Z | 2.0 |
| 2020-01-01T00:04:00Z | 3.0 |

**The following function returns:**

```js
|> last()
```

| _time | _value |
| --- | --- |
| 2020-01-01T00:04:00Z | 3.0 |

## Use first() or last() with aggregateWindow()

Use `first()` and `last()` with [`aggregateWindow()`](/flux/v0/stdlib/universe/aggregatewindow/) to select the first or last records in time-based groups. `aggregateWindow()` segments data into windows of time, aggregates data in each window into a single point using aggregate or selector functions, and then removes the time-based segmentation.

**Given the following input:**

| _time | _value |
| --- | --- |
| 2020-01-01T00:00:00Z | 10 |
| 2020-01-01T00:00:15Z | 12 |
| 2020-01-01T00:00:45Z | 9 |
| 2020-01-01T00:01:05Z | 9 |
| 2020-01-01T00:01:10Z | 15 |
| 2020-01-01T00:02:30Z | 11 |

**The following function returns:**

<!-- Tabbed content: Select one of the following options -->

**first:**

```js
|> aggregateWindow(every: 1h, fn: first)
```

| _time | _value |
| --- | --- |
| 2020-01-01T00:00:59Z | 10 |
| 2020-01-01T00:01:59Z | 9 |
| 2020-01-01T00:02:59Z | 11 |

**last:**

```js
|> aggregateWindow(every: 1h, fn: last)
```

| _time | _value |
| --- | --- |
| 2020-01-01T00:00:59Z | 9 |
| 2020-01-01T00:01:59Z | 15 |
| 2020-01-01T00:02:59Z | 11 |

<!-- End tabbed content -->

#### Related

-   [first() function](/flux/v0/stdlib/universe/first/)
-   [last() function](/flux/v0/stdlib/universe/last/)

[query](/influxdb/v2/tags/query/)
