query-writing
langchain-ai/deepagents
Write and execute SQL queries from simple SELECTs to complex JOINs and aggregations.
What is query-writing?
Constructs and runs SQL queries against databases, handling everything from basic single-table SELECTs to multi-table JOINs, aggregations, and subqueries. Use this when you need to retrieve, filter, or aggregate data from database tables.
- Writes SELECT queries for single and multiple tables
- Executes JOINs to combine data across related tables
- Performs aggregations and GROUP BY operations
- Handles WHERE filters, ORDER BY sorting, and LIMIT constraints
- Plans complex queries using task breakdown before execution
- Validates schema and column names before query construction
How to install query-writing
npx skills add https://github.com/langchain-ai/deepagents --skill query-writingHow to use query-writing
- 1.Identify which table(s) contain the data you need
- 2.Use sql_db_schema to examine column names and types
- 3.For complex queries, use write_todos to plan the JOIN structure and aggregations
- 4.Write the SELECT query with appropriate WHERE, GROUP BY, and ORDER BY clauses
- 5.Execute the query using sql_db_query
- 6.Review results and refine if needed
Use cases
- Retrieve customer data filtered by specific criteria
- Generate revenue reports aggregated by country or region
- Join invoice and customer tables to analyze spending patterns
- Query multiple related tables to answer business questions
- Validate data by checking for NULL values or unexpected results
- Data analysts querying business databases
- Developers building database-driven applications
- Business users generating ad-hoc reports
- Anyone needing to retrieve or aggregate data from SQL databases
query-writing FAQ
No. This skill only supports SELECT queries for data retrieval. DML statements are not allowed.
Verify column names and WHERE conditions match the schema, check for case sensitivity issues, and confirm that matching data actually exists in the tables.
Use INNER JOIN or LEFT JOIN with ON conditions matching foreign keys to primary keys. Examine each table's schema first to identify the correct join columns.
Queries default to LIMIT 5 rows to avoid overwhelming result sets. Adjust as needed for your use case.
Add stricter WHERE filters to reduce the result set, lower the LIMIT, or refine your query to select only necessary columns.
Full instructions (SKILL.md)
Source of truth, from langchain-ai/deepagents.
name: query-writing description: 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.
Query Writing Skill
Workflow for Simple Queries
For straightforward questions about a single table:
- Identify the table - Which table has the data?
- Get the schema - Use
sql_db_schemato see columns - Write the query - SELECT relevant columns with WHERE/LIMIT/ORDER BY
- Execute - Run with
sql_db_query - 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
SELECT
c.Country,
ROUND(SUM(i.Total), 2) as TotalRevenue
FROM Invoice i
INNER JOIN Customer c ON i.CustomerId = c.CustomerId
GROUP BY c.Country
ORDER BY TotalRevenue DESC
LIMIT 5;
Error Recovery
If a query fails or returns unexpected results:
- Empty results — Verify column names and WHERE conditions against the schema; check for case sensitivity or NULL values
- Syntax error — Re-examine JOINs, GROUP BY completeness, and alias references
- 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)
Related skills
More from langchain-ai/deepagents and the wider catalog.

schema-exploration
Explore database schema, discover tables, columns, data types, and entity relationships.

skill-creator
Guide for creating effective skills that extend agent capabilities with specialized knowledge, workflows, or tool integrations.

social-media
Draft engaging social media posts with research-backed content and generated images for LinkedIn, Twitter/X, and other platforms.

web-research
Delegate web research across multiple subagents, synthesize findings, and produce cited reports.

deep-agents-core
Framework for building multi-step AI agents with built-in planning, memory, and skill management

deep-agents-memory
Pluggable memory and file backends for Deep Agents: ephemeral, persistent, or hybrid routing.