# Filter by timestamp or offset

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: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

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

The question should be specific, self-contained, and written in natural language.
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.
