0
Phase 6

SQL Tool

~2 min read

Concept & How It Works

    Why Does It Exist?

    Business data lives in warehouses and OLTP databases. A SQL tool lets agents answer 'What were Q3 sales by region?' with real numbers, not guesses.

    Real-World Analogy

    A SQL tool is a translator who speaks both English and database — you ask a question, they write the query, and read you the answer.
    Loading diagram...

    Visual Workflows

    What is SQL Tool?

    Loading diagram...

    Example

    Scenario

    The agent generates SELECT region, SUM(revenue) FROM sales WHERE quarter='Q3' GROUP BY region and returns a formatted table.

    Solution

    In Tool Calling & Function Calling, apply SQL Tool to this scenario: The agent generates SELECT region, SUM(revenue) FROM sales WHERE quarter='Q3' GROUP BY region and returns a formatted table. Identify the inputs, run the technique, validate the output, and note one thing you would monitor in production.

    Practice Task

    Do this before moving to the next module — reading alone is not enough.

    Open the Code Walkthrough below and run it locally. Change one parameter related to SQL Tool (e.g. model, temperature, top_k, or tool name), observe the difference in output, and write 2–3 sentences explaining what changed.

    Code Walkthrough

    Highlighted lines show where SQL Tool happens in the code.

    SQL Tool
    1def run_sql(query, connection):  # define a reusable function2    if not query.strip().upper().startswith("SELECT"):3        raise ValueError("Only SELECT queries allowed")4    with connection.cursor() as cur:5        cur.execute(query)6        columns = [d[0] for d in cur.description]7        rows = cur.fetchmany(100)8    return {"columns": columns, "rows": rows}  # return the result

    Commands to Remember

    Commands to Remember

    • client.chat.completions.create(..., tools=[...]) # pass tool schemas to API
    • json.loads(response.choices[0].message.tool_calls[0].function.arguments) # parse tool args
    • pip install pydantic # validate tool inputs with schemas

    Common Mistakes

    • Treating SQL Tool as a black box without evaluation
    • Ignoring cost and latency in production
    • Skipping error handling for sql tool

    Cheat Sheet

    Quick recap — the most important points from this module.

    Cheat Sheet

    quick ref
    • SQL Tool
    • Text-to-SQL
    • Read-Only
    • Schema Context