SQL Analysis
Use this sub-skill when the task is to query a SQL database with ai-data-science-team, summarize database metadata, validate generated SQL for read-only safety, or operate SQLDatabaseAgent / make_sql_database_agent directly.
Route here for
- SQLAlchemy engine or connection setup for a database that will be queried by
SQLDatabaseAgent.
- Metadata summaries with
ai_data_science_team.tools.sql.get_database_metadata.
- Natural-language-to-SQL workflows where the caller supplies an already configured LLM object.
- Checking that generated SQL is read-only with the package's
_validate_sql implementation or the bundled smoke script.
- Interpreting
SQLDatabaseAgent response fields such as sql_query_code, data_sql, sql_database_function, and sql_database_error.
Route away
- For
SQLDataAnalyst composition, supervisor workflows, or Streamlit app launching, use the sibling multiagent-and-app-workflows sub-skill.
- For charting or Plotly visualization of SQL results, use the sibling
dataframe-code-agents sub-skill after converting the SQL result dict to a DataFrame.
- For file loading, direct DataFrame EDA, or optional EDA reports, use the sibling
data-access-and-eda sub-skill.
- For H2O AutoML, model evaluation, or MLflow tools, use the sibling
modeling-and-mlflow sub-skill.
Safe operating defaults
- Use a read-only database account or a disposable database copy whenever possible.
- Keep
safe_mode=True unless the user explicitly accepts the risk and the database account itself is read-only.
- Start with
n_samples=1 for metadata; increase only when sample values are necessary and safe to share.
- Do not launch Streamlit apps, call external LLM providers, download data, or write generated SQL logs unless the user asked for that side effect.
- Redact hostnames, database names, usernames, and connection URLs before sharing metadata or logs outside the local task.
Core workflow
import pandas as pd
import sqlalchemy as sql
from ai_data_science_team.agents import SQLDatabaseAgent
engine = sql.create_engine("sqlite:///example.db")
llm = ... # caller-provided LangChain-compatible chat model
agent = SQLDatabaseAgent(
model=llm,
connection=engine,
n_samples=1,
safe_mode=True,
log=False,
bypass_recommended_steps=True,
bypass_explain_code=True,
)
agent.invoke_agent("Return sales by month for 2024", max_retries=3, retry_count=0)
query = agent.get_sql_query_code()
data = agent.get_data_sql()
frame = pd.DataFrame(data) if isinstance(data, dict) else data
error = agent.get_response().get("sql_database_error") if agent.get_response() else None
If the user only needs schema understanding, avoid LLM calls and use get_database_metadata as shown in workflows.
References and bundled checks
- API reference: signatures, imports, response fields, and public/private boundary notes.
- Workflows: metadata-only inspection, direct agent use, factory graph use, large-schema handling, and app-derived cues.
- SQL safety and metadata: what
_validate_sql does and does not guarantee, and how metadata is sampled.
- Troubleshooting: symptom/cause/recovery table for imports, validation, token pressure, connection errors, and app/provider issues.
- Smoke script: local in-memory SQLite metadata and read-only safety check with no LLM call or external service.
1---2name: sql-analysis3description: "Operate ai-data-science-team SQL database querying, metadata4 inspection, read-only SQL safety, and SQLDatabaseAgent workflows."5disable-model-invocation: true6metadata:7 source: https://skillmd.com/skills/vectorspacelab/sql-analysis8 disco-role: operating9license: MIT10---1112# SQL Analysis1314Use this sub-skill when the task is to query a SQL database with `ai-data-science-team`, summarize database metadata, validate generated SQL for read-only safety, or operate `SQLDatabaseAgent` / `make_sql_database_agent` directly.1516## Route here for1718- SQLAlchemy engine or connection setup for a database that will be queried by `SQLDatabaseAgent`.19- Metadata summaries with `ai_data_science_team.tools.sql.get_database_metadata`.20- Natural-language-to-SQL workflows where the caller supplies an already configured LLM object.21- Checking that generated SQL is read-only with the package's `_validate_sql` implementation or the bundled smoke script.22- Interpreting `SQLDatabaseAgent` response fields such as `sql_query_code`, `data_sql`, `sql_database_function`, and `sql_database_error`.2324## Route away2526- For `SQLDataAnalyst` composition, supervisor workflows, or Streamlit app launching, use the sibling `multiagent-and-app-workflows` sub-skill.27- For charting or Plotly visualization of SQL results, use the sibling `dataframe-code-agents` sub-skill after converting the SQL result dict to a DataFrame.28- For file loading, direct DataFrame EDA, or optional EDA reports, use the sibling `data-access-and-eda` sub-skill.29- For H2O AutoML, model evaluation, or MLflow tools, use the sibling `modeling-and-mlflow` sub-skill.3031## Safe operating defaults32331. Use a read-only database account or a disposable database copy whenever possible.342. Keep `safe_mode=True` unless the user explicitly accepts the risk and the database account itself is read-only.353. Start with `n_samples=1` for metadata; increase only when sample values are necessary and safe to share.364. Do not launch Streamlit apps, call external LLM providers, download data, or write generated SQL logs unless the user asked for that side effect.375. Redact hostnames, database names, usernames, and connection URLs before sharing metadata or logs outside the local task.3839## Core workflow4041```python42import pandas as pd43import sqlalchemy as sql44from ai_data_science_team.agents import SQLDatabaseAgent4546engine = sql.create_engine("sqlite:///example.db")47llm = ... # caller-provided LangChain-compatible chat model4849agent = SQLDatabaseAgent(50 model=llm,51 connection=engine,52 n_samples=1,53 safe_mode=True,54 log=False,55 bypass_recommended_steps=True,56 bypass_explain_code=True,57)58agent.invoke_agent("Return sales by month for 2024", max_retries=3, retry_count=0)5960query = agent.get_sql_query_code()61data = agent.get_data_sql()62frame = pd.DataFrame(data) if isinstance(data, dict) else data63error = agent.get_response().get("sql_database_error") if agent.get_response() else None64```6566If the user only needs schema understanding, avoid LLM calls and use `get_database_metadata` as shown in [workflows](references/workflows.md#metadata-only-no-llm-call).6768## References and bundled checks6970- [API reference](references/api-reference.md): signatures, imports, response fields, and public/private boundary notes.71- [Workflows](references/workflows.md): metadata-only inspection, direct agent use, factory graph use, large-schema handling, and app-derived cues.72- [SQL safety and metadata](references/sql-safety-and-metadata.md): what `_validate_sql` does and does not guarantee, and how metadata is sampled.73- [Troubleshooting](references/troubleshooting.md): symptom/cause/recovery table for imports, validation, token pressure, connection errors, and app/provider issues.74- [Smoke script](scripts/smoke_sql_safety.py): local in-memory SQLite metadata and read-only safety check with no LLM call or external service.