> ## Documentation Index
> Fetch the complete documentation index at: https://phidatainc-studio-tools-doc.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Querying your data

> Give an agent read-only SQL access and schema introspection

`SQLTools` connects an agent to a database. Point it at a read-only connection and the agent can introspect the schema and run queries.

```python theme={null}
from agno.agent import Agent
from agno.db.postgres import PostgresDb
from agno.models.openai import OpenAIResponses
from agno.tools.sql import SQLTools

agent = Agent(
    model=OpenAIResponses(id="gpt-5.5"),
    db=PostgresDb(db_url="postgresql+psycopg://ai:ai@localhost:5532/ai"),
    tools=[
        SQLTools(db_url="postgresql+psycopg://readonly@warehouse/analytics")
    ],
    instructions=(
        "Introspect the schema before writing SQL. Answer with the numbers "
        "and the exact query you ran. Never guess a column name."
    ),
)

agent.print_response("How many active subscriptions are on the Pro plan?")
```

The agent's `db` and the `SQLTools` connection are separate. `db` stores the agent's own sessions and learnings. `SQLTools` points at the warehouse you are answering questions about. Keep them distinct.

## Introspect before generating

A data agent that guesses column names is wrong confidently. `SQLTools` ships `list_tables` and `describe_table` so the agent grounds SQL in the real schema. The instruction to introspect first is what makes it reliable.

| Tool             | Use                                  |
| ---------------- | ------------------------------------ |
| `list_tables`    | Discover what exists before querying |
| `describe_table` | Get exact column names and types     |
| `run_sql_query`  | Execute the generated SQL            |

## Scope the connection

The cleanest boundary is the connection itself. The `readonly` Postgres role has no write grant, so the connection physically cannot write, regardless of what the model is prompted to do. For the full read vs write split, see [Safe data access](/use-cases/data-agents/safe-data-access).

## Next steps

| Task                         | Guide                                                                   |
| ---------------------------- | ----------------------------------------------------------------------- |
| Ground SQL in business rules | [Grounding in context](/use-cases/data-agents/grounding-in-context)     |
| Stop repeating query errors  | [Self-correcting agents](/use-cases/data-agents/self-correcting-agents) |
| Allow controlled writes      | [Safe data access](/use-cases/data-agents/safe-data-access)             |

## Developer Resources

* [SQL tools cookbook](https://github.com/agno-agi/agno/tree/main/cookbook/91_tools)
* [Dash: the multi-agent data team](/tutorials/dash/overview)
