> 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/devx/5.5/user-guide/cli/data/querying-data.md).

# Querying data

The CLI allows you to browse and subscribe to topic data with SQL. Please refer to the Lenses SQL documentation for more details.

Use these flags to control the data that’s returned:

* **keys** print message keys
* **keys-only** print the keys only, not the value of the message
* **meta** print the message metadata, partition number and offsets
* **stats** print query statistics as the last message

## Browsing <a href="#browsing" id="browsing"></a>

Browse data via the query command, optionally validate and output stats:

```bash
lenses-cli query \
    --keys \
    --meta "SELECT score, author FROM reddit_posts LIMIT 50"
```

### Execute with stats and show offsets <a href="#execute-with-stats-and-show-offsets" id="execute-with-stats-and-show-offsets"></a>

```bash
lenses-cli query \
    --stats "SELECT score, author FROM reddit_posts LIMIT 50"
```

## Live <a href="#live" id="live"></a>

Live continuous queries update according to the query and never stop until terminated:

{% code fullWidth="false" %}

```bash
lenses-cli \
    query \
    --live-stream "SELECT score, author FROM reddit_posts WHERE _sample=2 AND _sampleWindow=200"
```

{% endcode %}

## Interactive shell <a href="#interactive-shell" id="interactive-shell"></a>

The interactive shell allows you to have an interactive experience and issue Lenses SQL queries from the command line.

To start the interactive shell, issue the following command:

```bash
lenses-cli shell
```

The available options are the following:

* **keys** print message keys
* **keys-only** print the keys only, not the value of the message
* **meta** print the message metadata, partition number and offsets
* **stats** print query statistics as the last message
* **pretty** pretty print the JSON output
* **live-stream** run as a continuous query
* **!options** prints the current options

1. Options are set using the **!** prefix
2. Queries are executed by **;**

Multiline queries are possible but not available in the history inside the shell. After a restart, the multiline queries are concatenated into one line and can be selected.

Query history is stored in **.lenses/history** in the home directory of the user.

```
    __                                 ________    ____
   / /   ___  ____  ________  _____   / ____/ /   /  _/
  / /   / _ \/ __ \/ ___/ _ \/ ___/  / /   / /    / /
 / /___/  __/ / / (__  )  __(__  )  / /___/ /____/ /
/_____/\___/_/ /_/____/\___/____/   \____/_____/___/
Docs at https://docs.lenses.io
Connected to [https://master.lenses.io:443] as [andrew], context [master]
Use "!" to set output options [!keys|!keysOnly|!stats|!meta|!pretty]
Crtl+D to exit

lenses-sql>
```


---

# 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 dynamically by asking a question.

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

```
GET https://docs.lenses.io/latest/devx/5.5/user-guide/cli/data/querying-data.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
