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

Write, debug, and optimize SQL queries faster using Cursor to work with databases more effectively. This guide walks you through every stage of Cursor for SQL, from Write SQL queries all the way through Design and manage databases, with a tested, copy-ready prompt at each step. Each stage targets a specific phase of the process so you always know exactly what to ask and what output to expect. Works with ChatGPT, Claude, and Gemini and any other major AI tool.
Stage 1
These prompts help you write efficient SQL queries and work with relational data.
Write a SELECT query
Write a SQL query that [DESCRIBE WHAT YOU WANT TO RETRIEVE] from [DESCRIBE THE TABLES]. Include: appropriate JOINs, WHERE filters, and ORDER BY. Database: [POSTGRESQL / MYSQL / SQLITE / OTHER].
Write an aggregation query
Write a SQL query that aggregates [DESCRIBE WHAT TO AGGREGATE] from [TABLES]. Use GROUP BY, HAVING if needed, and appropriate aggregate functions (COUNT, SUM, AVG, MAX, MIN).
Write a subquery
Write a SQL query using a subquery or CTE to [DESCRIBE THE GOAL]: [DESCRIBE THE TABLES AND DESIRED RESULT]. When should I use a subquery versus a CTE versus a JOIN here?
Write an INSERT/UPDATE/DELETE
Write a SQL [INSERT / UPDATE / DELETE] statement that [DESCRIBE THE OPERATION] on [TABLE NAME]. Include appropriate WHERE clauses for UPDATE and DELETE to avoid modifying too many rows.
Write a window function
Write a SQL query using window functions to [DESCRIBE THE GOAL: RUNNING TOTALS, RANKING, LAG/LEAD COMPARISONS]. Explain the PARTITION BY and ORDER BY clauses.
Stage 2
Start here to diagnose slow or incorrect queries before you attempt any fixes.
Debug a failing query
This SQL query is returning an error or wrong results: [PASTE QUERY]. Expected result: [DESCRIBE]. Actual result or error: [DESCRIBE]. What is wrong and how do I fix it?
Fix a JOIN
My SQL JOIN is returning [TOO MANY ROWS / TOO FEW ROWS / WRONG DATA]: [PASTE QUERY]. Explain what each JOIN type does and which one I should use here: INNER, LEFT, RIGHT, FULL OUTER, CROSS.
Fix NULL handling
My query is not handling NULLs correctly: [PASTE QUERY]. Describe the NULL behavior and fix it using IS NULL, IS NOT NULL, COALESCE, or NULLIF as appropriate.
Debug a slow query
This query is running slowly: [PASTE QUERY]. What are the most likely causes? Suggest how to add indexes, rewrite the query, or use EXPLAIN/ANALYZE to diagnose the performance issue.
Fix date/time queries
My date or time query is not working as expected: [PASTE QUERY]. I am using [POSTGRESQL / MYSQL / SQLITE]. Fix the query and explain the correct date functions and timezone handling for my database.
Stage 3
These prompts help you improve query performance through better indexing and structure.
Optimize query performance
Optimize this slow SQL query: [PASTE QUERY]. Suggest: index additions, query restructuring, replacing correlated subqueries, or pushing filters earlier. Explain the impact of each optimization.
Design indexes
Design indexes for these queries that need to be fast: [PASTE QUERIES]. For each index, explain: which columns to index, whether to use a composite index, and the tradeoff with write performance.
Rewrite with CTEs
Rewrite this complex query using CTEs (Common Table Expressions) to make it more readable: [PASTE QUERY]. Break it into named steps that are easier to understand and debug.
Analyze query plan
Help me read this EXPLAIN output: [PASTE EXPLAIN RESULT]. What is the query optimizer doing? Where is it slow? What changes would improve it?
Optimize pagination
I'm paginating through a large table with OFFSET/LIMIT and it gets slow on later pages: [PASTE QUERY]. Rewrite it using cursor-based pagination with a WHERE clause filter instead.
Stage 4
Use these prompts to design schemas and manage your database as it grows.
Design a schema
Design a database schema for [DESCRIBE THE APPLICATION]. Create tables with proper columns, data types, primary keys, foreign keys, and indexes. Explain the key design decisions.
Write migrations
Write a database migration to [DESCRIBE THE CHANGE: ADD COLUMN, RENAME TABLE, CHANGE TYPE, ADD INDEX]. Include both the UP migration and the DOWN rollback. Use [POSTGRESQL / MYSQL SYNTAX].
Normalize a schema
Review this database schema and suggest normalization improvements: [PASTE SCHEMA]. Identify violations of 1NF, 2NF, or 3NF and suggest how to restructure the tables.
Write stored procedures
Write a stored procedure or function in [POSTGRESQL / MYSQL] that [DESCRIBE WHAT IT SHOULD DO]. Include: parameters with types, error handling, and a usage example.
Audit data quality
Write SQL queries to audit the data quality in [TABLE NAME]. Check for: duplicates, NULL values in required columns, orphaned foreign keys, and out-of-range values.
Cursor can read your database schema files, migrations, and ORM models to understand your data model. When you ask it to write a query, it knows your table names, column types, and relationships without you having to explain them.
Yes. PostgreSQL, MySQL, SQLite, and SQL Server each have different functions and syntax. Tell Cursor which database you are using and it will generate compatible SQL.
Paste the query, describe what you expected, and describe what happened. Include the relevant table schema if Cursor doesn't have it. Cursor will identify the most likely causes and suggest fixes.
Yes. Cursor works with Prisma, TypeORM, SQLAlchemy, Sequelize, and other ORMs. Ask it to write ORM queries instead of raw SQL and it will follow your ORM's syntax and patterns.
Missing indexes on columns used in WHERE clauses and JOIN conditions. Cursor can analyze your queries and suggest which indexes to add. N+1 query patterns (running a query for each row in a result set) are the second most common issue.
AI Prompts for Cursor for Backend Development
Build scalable, maintainable backend systems faster using Cursor's AI assistance for APIs, services, and data layers..
See promptsAI Prompts for Cursor for Database Design
Design scalable, well-structured databases using Cursor to make better decisions about schemas, relationships, and performance..
See promptsAI Prompts for ChatGPT for SQL
Write, debug, and optimize SQL queries faster using ChatGPT for database work across PostgreSQL, MySQL, and SQLite..
See prompts