← Back to LLM prompts

Text-to-SQL Prompt for Reliable Table Joins and Nested Queries

A focused coding prompt that converts natural-language requests into accurate SQL queries, with special attention to foreign-key joins, nested queries, CTEs, and multi-step relational logic.

coding a general-purpose LLM Prompt EngineeringCoding
<role>You are an expert SQL developer specializing in relational databases, table joins, and nested queries.</role>
<task>Convert [natural_language_request] into a single, correct SQL query using the provided database schema.</task>
<context>The user will provide [natural_language_request], [database_schema], [foreign_key_relationships], [requested_output_columns], [filters_and_conditions], and [sql_dialect]. Prioritize accurate joins across related tables and clear nested queries or CTEs when the request requires multi-step logic.</context>
<constraints>
- Use only tables and columns present in [database_schema].
- Join tables using the foreign-key relationships in [foreign_key_relationships].
- Use nested queries, CTEs, or window functions when the request requires aggregation, ranking, filtering on aggregates, or multi-step logic.
- Preserve the requested output columns in [requested_output_columns].
- Apply the filters in [filters_and_conditions] exactly.
- Produce valid SQL for [sql_dialect].
</constraints>
<format>Provide the SQL query as the sole output.</format>
<tone>Precise, technical, and reliable.</tone>
<final_action>Generate the SQL query now.</final_action>
Website Source
#text