> For the complete documentation index, see [llms.txt](https://docs.lenses.io/latest/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.lenses.io/latest/user-guide/6.2-user-guide/using/using-sql-to-query-kafka/filter-by-timestamp-or-offset.md).

# Filter by timestamp or offset

Understand using Kafka topics by timestamp or offset in Lenses SQL Snapshot concepts engine for Apache Kafka

A popular question is "how do I see the most recent messages?" or "how do i filter for a specific date"

{% hint style="success" %}
Lenses query editor shows you the oldest events (the earliest) because this is how Kafka is designed.
{% endhint %}

To skip the old events and see the most recent very fast, you need to either filter on time or offset with these filters. A list of functions is available here [SQL reference](https://docs.lenses.io/latest/user-guide/sql-reference)

### View the most recent messages <a href="#view-the-most-recent-messages" id="view-the-most-recent-messages"></a>

#### Filter by timestamp <a href="#filter-by-timestamp" id="filter-by-timestamp"></a>

Use the now() function to view events from the last 5 minutes:

```sql
SELECT * FROM mytopic WHERE _meta.timestamp > now() - "5m" LIMIT 100;
```

#### Filter by offset <a href="#filter-by-offset" id="filter-by-offset"></a>

View the most recent 100 events :

```sql
SELECT * FROM mytopic WHERE _meta.offset >= LAST_OFFSET() - 10 LIMIT 100;
```

### Slice from the past <a href="#slice-from-the-past" id="slice-from-the-past"></a>

#### Filter by timestamp <a href="#filter-by-timestamp-1" id="filter-by-timestamp-1"></a>

```sql
SELECT *
FROM position_reports
WHERE
   _meta.timestamp > "2020-04-01" AND
   _meta.timestamp < "2020-04-02";
```

Same example with time filter :

```sql
SELECT * from mytopic
WHERE 
    _meta.timestamp > '2024-10-16 14:30:00' AND 
    _meta.timestamp < "2025-04-02 14:30:00"
;
```

#### Filter by Offset <a href="#filter-by-offset-1" id="filter-by-offset-1"></a>

the same logic can be applied for filtering by offset :

```sql
SELECT * FROM mytopic 
WHERE 
    _meta.offset >= 100 and
    _meta.offset < 1000;
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.lenses.io/latest/user-guide/6.2-user-guide/using/using-sql-to-query-kafka/filter-by-timestamp-or-offset.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
