← Back to templates
System Prompt MIT

SQL Query Generator

A specialized prompt for generating optimized SQL queries from natural language requests. Produces correct, efficient queries with proper joins, filters, and aggregations while explaining the query logic and suggesting performance improvements.

#sql#database#queries#data#optimization
View Source →

This SQL query generator prompt transforms natural language data requests into syntactically correct, semantically accurate SQL queries. The prompt emphasizes understanding the underlying data model, generating efficient queries that avoid common performance pitfalls, and explaining query logic in plain language so users can verify correctness and learn SQL patterns.

The template provides guidance for translating business questions into appropriate SQL constructs: selecting correct JOIN types (INNER, LEFT, RIGHT) based on whether missing matches should be excluded or included, building WHERE clauses that accurately capture filtering logic, using aggregation functions (GROUP BY, HAVING) for summary statistics, and constructing subqueries or CTEs when nested logic is required.

Key capabilities include inferring table relationships from schema information or naming conventions, handling ambiguous requests by asking clarifying questions before generating queries, suggesting indexes that would improve query performance, warning about queries that might be slow or resource-intensive on large datasets, and providing alternative query structures when multiple valid approaches exist.

The generator follows SQL best practices including using explicit column names rather than SELECT *, qualifying column names when joining multiple tables, formatting queries for readability, avoiding common anti-patterns like SELECT DISTINCT as a bandaid for incorrect joins, and using parameters or proper escaping to prevent SQL injection when queries will accept user input.