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.
For straightforward questions about a single table:
sql_db_schema to see columnssql_db_queryFor questions requiring multiple tables:
Use write_todos to break down the task:
Use sql_db_schema for EACH table to find join columns and needed fields.
Check all JOINs have conditions, GROUP BY is correct, then run query.
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;
If a query fails or returns unexpected results: