Query Writing

by langchain-ai6a3a12bc5b3aNo license30K starsListed Oct 8, 2026Updated Oct 8, 2026Repository updated today

Writes and executes SQL queries from simple SELECTs to complex multi-table JOINs, aggregations, and subqueries. Use when the user asks to query a database, write SQL, run a SELECT statement, retrieve data, filter records, or generate reports from database tables.

Instructions onlyData & Analytics
AI-generated overview

Guides writing and executing SQL queries, from simple SELECTs to multi-table JOINs and aggregations.

What it does
This skill provides a step-by-step workflow for turning data questions into SQL: identify tables, inspect schemas, write the query, execute it, and present results. It covers simple single-table queries and complex multi-table queries with JOINs, GROUP BY aggregations, subqueries, filters, ordering, and limits. It also includes error-recovery guidance for empty results, syntax errors, and timeouts, plus quality rules such as avoiding SELECT * and never using DML statements.
When to use it
Use it when a user asks to query a database, write SQL, run a SELECT statement, retrieve or filter records, or generate reports from database tables. It fits both straightforward single-table lookups and questions that require joining several tables.
Requirements
Requires a database connection and the sql_db_schema and sql_db_query tools, plus write_todos for planning complex queries. Ships no scripts; it is instructions only.

Query Writing Skill

Workflow for Simple Queries

For straightforward questions about a single table:

  1. Identify the table - Which table has the data?
  2. Get the schema - Use sql_db_schema to see columns
  3. Write the query - SELECT relevant columns with WHERE/LIMIT/ORDER BY
  4. Execute - Run with sql_db_query
  5. Format answer - Present results clearly

Workflow for Complex Queries

For questions requiring multiple tables:

1. Plan Your Approach

Use write_todos to break down the task:

  • Identify all tables needed
  • Map relationships (foreign keys)
  • Plan JOIN structure
  • Determine aggregations

2. Examine Schemas

Use sql_db_schema for EACH table to find join columns and needed fields.

3. Construct Query

  • SELECT - Columns and aggregates
  • FROM/JOIN - Connect tables on FK = PK
  • WHERE - Filters before aggregation
  • GROUP BY - All non-aggregate columns
  • ORDER BY - Sort meaningfully
  • LIMIT - Default 5 rows

4. Validate and Execute

Check all JOINs have conditions, GROUP BY is correct, then run query.

Example: Revenue by Country

sql
SELECT    c.Country,    ROUND(SUM(i.Total), 2) as TotalRevenueFROM Invoice iINNER JOIN Customer c ON i.CustomerId = c.CustomerIdGROUP BY c.CountryORDER BY TotalRevenue DESCLIMIT 5;

Error Recovery

If a query fails or returns unexpected results:

  1. Empty results — Verify column names and WHERE conditions against the schema; check for case sensitivity or NULL values
  2. Syntax error — Re-examine JOINs, GROUP BY completeness, and alias references
  3. Timeout — Add stricter WHERE filters or LIMIT to reduce result set, then refine

Quality Guidelines

  • Query only relevant columns (not SELECT *)
  • Always apply LIMIT (5 default)
  • Use table aliases for clarity
  • For complex queries: use write_todos to plan
  • Never use DML statements (INSERT, UPDATE, DELETE, DROP)

Source and attribution

Source:langchain-ai/deepagentsinexamples/text-to-sql-agent/skills/query-writingat commit6a3a12b

License: No license

Content belongs to its original authors. SourceWeft indexes it from a public repository.

Report or request removal

Query Writing Agent Skill | SourceWeft