Best Text-to-SQL Agents and Prompting Techniques
Text-to-SQL technology has made significant advances in recent years, allowing users to query databases using natural language instead of writing complex SQL queries. Here's a comprehensive overview of the best agents and techniques as of 2025.
Leading Text-to-SQL Agents
Amazon Bedrock Agents
Amazon Bedrock Agents offers a robust text-to-SQL solution with two key strengths specifically designed for enterprise workloads:
- Automated scalable schema discovery: The schema and table metadata can be dynamically updated when initial query attempts fail[1]
- Automated error handling: Error messages are directly fed back to the agent to improve query success rates[1]
- Built-in support for Amazon Redshift as of December 2024[1]
IBM Granite
IBM's Granite code model recently topped the BIRD benchmark leaderboard with impressive capabilities:
- Achieves 68% accuracy on complex SQL queries, the highest among AI models (though human engineers still reach 93%)[5]
- Uses a sophisticated three-step solution:
- Schema linking: Matching keywords to relevant database tables/columns
- Content linking: Generating SQL code comparing columns with specific values
- Final query generation: Creating optimal SQL queries using the previous steps[5]
- Scored 80 on code execution speed compared to human engineers' 90[5]
Inferable.ai pgsql-adapter
This open-source solution provides:
- Modeling the context getter as a tool, allowing long-running chats to refresh schema if changed[3]
- Privacy mode where the LLM doesn't see your data[3]
- Simple setup via npm package with a chat interface[3]
- Compatible with PostgreSQL and adaptable to other relational databases[3]
Effective Prompting Techniques
Schema Context Integration
One of the most critical techniques is providing proper database schema information:
- Using PostgreSQL catalog tables to dynamically retrieve schema metadata[3]
- Including sample data to help models understand the database structure[3][4]
- Adding specific details about primary/foreign key relationships[4]
Few-Shot Examples
Including examples greatly improves model performance:
- Provide paired examples of natural language questions and their corresponding SQL queries[6]
- Particularly effective for complex queries involving multiple tables and operations[6]
- Can be formatted as a FewShotPromptTemplate in frameworks like LangChain[6]
Divide and Prompt (Chain of Thought)
This advanced technique breaks down complex queries into manageable subtasks:
- Clause by Clause DnP: Builds SQL queries step by step, focusing on one clause at a time[8]
- Schema Linking DnP: Identifies relevant database schema elements before generating SQL[8]
- Generate and Refine DnP: Creates an initial query and refines it through additional prompts[8]
Dialect-Specific Prompting
Adapting prompts to specific SQL dialects improves accuracy:
- LangChain supports dialect-specific prompts for multiple databases including PostgreSQL, SQLite, MySQL, and others[6]
- Ensures generated queries match the syntax requirements of the target database[6]
SQL Formatting Instructions
Adding explicit formatting guidance improves consistency:
- Specify quote style for literals (e.g., single quotes around string values)[10]
- Provide instructions on column and table name delimiters (square brackets for SQL Server, double quotes for Snowflake)[10]
- Include guidance on handling special characters and case sensitivity[4]
Model Selection
The choice of foundation model significantly impacts performance:
- Top performers: Claude 3.5 Sonnet, GPT-4, DeepSeek V2, and IBM Granite[4][5]
- Smaller models like Llama-3-7B struggle with complex schemas (25+ tables)[4]
- Current state-of-the-art reaches about 69% accuracy on the BIRD benchmark[4][5]
Benchmark Performance
The BIRD benchmark has become the standard for evaluating text-to-SQL systems:
- Measures both correctness and efficiency of generated SQL queries[9]
- Includes challenges like "dirty" real-world data formats and external knowledge requirements[9]
- IBM's solution currently tops the leaderboard with 68% accuracy[5]
Implementation Considerations
When implementing a text-to-SQL solution:
- Consider using multi-step agents that can iterate and refine queries[3]
- Implement proper guardrails to prevent security issues with database access[7]
- For enterprise deployments with 100+ tables, specialized approaches are needed to handle complexity[1]
- Automated error handling is crucial for production deployments[1]
With these agents and techniques, organizations can build powerful natural language interfaces to their databases, making data accessible to more users without requiring deep SQL knowledge.