AI Prompts for Cursor for SQL

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

AI Prompts for Cursor for SQL
Scroll to explore

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

Write SQL queries

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

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

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

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

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.

Write SQL queries

Stage 2

Debug SQL

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?

Debug SQL

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.

Debug SQL

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 SQL

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.

Debug SQL

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.

Debug SQL

Stage 3

Optimize SQL

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.

Optimize SQL

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.

Optimize SQL

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.

Optimize SQL

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 SQL

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.

Optimize SQL

Stage 4

Design and manage databases

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.

Design and manage databases

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

Design and manage databases

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.

Design and manage databases

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.

Design and manage databases

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.

Design and manage databases

Frequently asked questions

How does Cursor help with SQL writing?+

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.

Can Cursor help with database-specific SQL syntax?+

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.

How do I debug a SQL query with Cursor?+

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.

Can Cursor help me write ORM queries too?+

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.

What is the most common SQL performance mistake?+

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.

More Cursor prompt guides