20 tested prompts across 4 stages. Works with ChatGPT, Claude, and Gemini.

Most people try to use AI for ChatGPT for SQL with a single vague prompt and get generic results. This guide takes a different approach: 4 targeted stages, from Write SQL queries through Design and schema, each with a prompt that gives the AI exactly the context it needs. Write, debug, and optimize SQL queries faster using ChatGPT for database work across PostgreSQL, MySQL, and SQLite. Works with ChatGPT, Claude, and Gemini.
Stage 1
These prompts help you write efficient SQL queries and work with relational data.
Write a SELECT query
Write a SQL query to retrieve [DESCRIBE WHAT YOU WANT] from these tables: [DESCRIBE TABLES AND COLUMNS]. Use [POSTGRESQL / MYSQL / SQLITE]. Include appropriate JOINs, WHERE filters, and ORDER BY.
Write an aggregation
Write a SQL query that aggregates [WHAT TO AGGREGATE] from [TABLE]. Use GROUP BY, and include HAVING if I need to filter on the aggregated result.
Write a CTE
Write a SQL query using a CTE (Common Table Expression) to [DESCRIBE THE GOAL]. Break it into named steps and explain what each CTE does.
Write INSERT/UPDATE/DELETE
Write a SQL [INSERT / UPDATE / DELETE] statement to [DESCRIBE THE OPERATION] on [TABLE]. Include appropriate WHERE clauses to avoid modifying too many rows.
Write a window function
Write a SQL query using a window function to [DESCRIBE THE GOAL: RUNNING TOTAL, RANK, LAG/LEAD]. Explain the OVER clause: PARTITION BY and ORDER BY.
Stage 2
Start here to diagnose slow or incorrect queries before you attempt any fixes.
Fix a failing query
This SQL query is failing or returning wrong results: [PASTE QUERY]. Expected: [DESCRIBE]. Actual error or result: [DESCRIBE]. What is wrong and how do I fix it?
Fix a JOIN problem
My JOIN is returning [TOO MANY / TOO FEW] rows: [PASTE QUERY]. Explain what each JOIN type does and which I should use here: INNER, LEFT, RIGHT, or FULL OUTER.
Fix NULL handling
My query is not handling NULLs correctly: [PASTE QUERY]. Fix it using IS NULL, IS NOT NULL, COALESCE, or NULLIF as appropriate.
Fix a slow query
This query is running too slowly: [PASTE QUERY]. What indexes should I add and how should I rewrite the query to be faster? I'm using [POSTGRESQL / MYSQL].
Fix a date query
My date/time query is not working: [PASTE QUERY]. I'm using [DATABASE]. Fix it and explain the correct date functions and timezone handling.
Stage 3
These prompts help you improve query performance through better indexing and structure.
Optimize a query
Optimize this slow query: [PASTE QUERY]. Suggest: indexes to add, how to restructure the query, and any anti-patterns to eliminate. Explain the impact of each change.
Design indexes
Design indexes for these queries: [PASTE QUERIES]. For each index: which columns, composite or single, and what the index speeds up.
Fix N+1 queries
I have an N+1 query problem where a query runs inside a loop: [PASTE CODE]. Rewrite to fetch all needed data in a single query.
Use EXPLAIN
Help me understand this EXPLAIN output: [PASTE EXPLAIN RESULT]. What is the query optimizer doing, where is it slow, and what should I change?
Fix slow pagination
My OFFSET-based pagination gets slow on large pages: [PASTE QUERY]. Rewrite using cursor-based pagination with a WHERE clause filter.
Stage 4
Start here to design a clean schema before writing migrations or queries.
Design a schema
Design a database schema for [DESCRIBE THE APPLICATION]. Give me: tables, columns with types, primary keys, foreign keys, and constraints.
Write a migration
Write a migration to [DESCRIBE CHANGE: ADD COLUMN, RENAME TABLE, ADD INDEX]. Include UP and DOWN. Make it safe for production data.
Normalize a schema
Review this schema for normalization: [PASTE SCHEMA]. Identify violations of 1NF/2NF/3NF and suggest how to restructure.
Write an audit query
Write SQL queries to audit the data quality of [TABLE]. Check for: duplicates, NULL in required fields, orphaned foreign keys, and out-of-range values.
Convert to another database
Convert this [MYSQL / SQLITE / POSTGRESQL] query to [TARGET DATABASE]: [PASTE QUERY]. Explain any syntax differences and functions that need to change.
Paste your CREATE TABLE statements or describe the table names, column names, and types. The more schema context you provide, the more accurate the generated queries. For complex queries, share the full schema for relevant tables.
Yes. Tell ChatGPT which database you are using (PostgreSQL, MySQL, SQLite, SQL Server, etc.) and it will use the correct syntax, functions, and features for that database.
Schema structures (table names, column types) are generally fine to share. Avoid sharing actual data, especially rows with personal information or production values. Always follow your organization's data handling policies.
Yes. Paste the query and ask ChatGPT to explain what it does step by step, then ask it to optimize. Understanding the query first helps you verify the optimization is correct.
Writing queries from scratch, debugging wrong results, explaining complex SQL like window functions and CTEs, designing schemas, and optimizing performance. It is particularly good at translating plain-language descriptions of what you want into working SQL.
AI Prompts for Cursor for SQL
Write, debug, and optimize SQL queries faster using Cursor to work with databases more effectively..
See promptsAI Prompts for ChatGPT for Debugging
Diagnose and fix bugs faster by using ChatGPT as a debugging partner across any programming language..
See promptsAI Prompts for ChatGPT for Data Analysis
Extract meaningful insights from data faster by using ChatGPT to analyze, interpret, and communicate findings..
See prompts