AI Prompts for ChatGPT for SQL

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

AI Prompts for ChatGPT for SQL
Scroll to explore

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

Write SQL queries

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 SQL queries

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 SQL queries

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 SQL queries

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 SQL queries

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.

Write SQL queries

Stage 2

Debug SQL

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?

Debug SQL

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.

Debug SQL

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.

Debug SQL

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].

Debug SQL

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.

Debug SQL

Stage 3

Optimize SQL

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.

Optimize SQL

Design indexes

Design indexes for these queries: [PASTE QUERIES]. For each index: which columns, composite or single, and what the index speeds up.

Optimize SQL

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.

Optimize SQL

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?

Optimize SQL

Fix slow pagination

My OFFSET-based pagination gets slow on large pages: [PASTE QUERY]. Rewrite using cursor-based pagination with a WHERE clause filter.

Optimize SQL

Stage 4

Design and schema

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.

Design and schema

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.

Design and schema

Normalize a schema

Review this schema for normalization: [PASTE SCHEMA]. Identify violations of 1NF/2NF/3NF and suggest how to restructure.

Design and schema

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.

Design and schema

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.

Design and schema

Frequently asked questions

How do I describe my database schema to ChatGPT?+

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.

Can ChatGPT help me with database-specific SQL?+

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.

Is it safe to share my database schema with ChatGPT?+

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.

Can ChatGPT help me optimize queries I don't fully understand?+

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.

What SQL topics is ChatGPT most useful for?+

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.