Skip to main content
Coding Prompts

Generate SQL Query

Write SQL queries from plain English descriptions of what data you need.

beginnerWorks with any modelCoding
Prompt
Write a SQL query for [DATABASE_TYPE].

**Schema:**
[TABLES_AND_SCHEMA]

**What I need:**
[WHAT_YOU_NEED]

**Performance constraints (if any):**
[PERFORMANCE_CONSTRAINTS]

Output:
- The complete SQL query
- A comment above each major clause (SELECT, FROM, JOIN, WHERE, GROUP BY, etc.) explaining what it does
- A brief note on any indexes that would improve performance for this query

How to Use

Describe your tables and what data you want in plain English — no need to know the exact SQL syntax. The more detail you give about your schema (table names, column names, data types, and relationships), the more accurate the generated query will be. After getting the query, always review it against your actual schema before running it in production.

Variables

VariableDescription
[DATABASE_TYPE]Your database system: PostgreSQL, MySQL, SQLite, BigQuery, SQL Server, Snowflake, etc. Syntax varies between dialects.
[TABLES_AND_SCHEMA]Describe the relevant tables and their key columns. Example: users (id, email, created_at, plan) and orders (id, user_id, total, status, created_at) — joined on users.id = orders.user_id
[WHAT_YOU_NEED]Plain-English description of the result you want. Example: "Monthly revenue for the last 12 months, broken down by plan type, excluding cancelled orders"
[PERFORMANCE_CONSTRAINTS]Optional: mention if the table is large (e.g., "orders has 50M rows"), if you need an index hint, or if query time must be under a certain threshold

Tips

  • Include sample data or row counts if you have them — the model can suggest whether a subquery, CTE, or window function is more appropriate for your data volume.
  • Specify if you need the query to run in a BI tool (e.g., Looker, Metabase) since some tools have restrictions on certain SQL constructs like CTEs or subqueries in FROM.