Skip to content

SQL and Database Prompts

Copy-and-paste SQL and database prompts: write and explain queries, window functions, joins, schema design, indexes, slow query fixes, migrations and safe data updates.

Takes about a minute. You hear back from me personally.

Prefer Saad Raza SEO on Google Add this site as a preferred source and Google will show more of my SEO and AI search guides when you search.

Add saadrazaseo.com as a preferred source on Google

These SQL and database prompts help you write and explain queries, design schemas, add indexes, speed up slow queries, write migrations and move data safely. They work for MySQL, PostgreSQL, SQL Server, SQLite and BigQuery.

20 prompts on this page. Last updated: 4 December 2026. Written and tested by Saad Raza.

How to use these prompts

  1. Click Copy on any prompt.
  2. Paste it into ChatGPT, Claude, Gemini, Copilot or Perplexity.
  3. Replace everything in [square brackets] with your own details. The more specific you are, the better the answer.
  4. Read and edit the result before you use it. Add your own experience and check every fact, name and number.

Tell the AI which database and version you use, and share the table structure. Test queries on a copy of your data first, especially updates and deletes, and never paste real customer data into an AI tool.

Writing queries

1. Write a SQL query from a question

Write a [database] query to answer: [question]. Tables: [paste CREATE TABLE statements or columns]. Explain how it works.

2. Write a report query

Write a query that shows [metric] by [dimension] per [time period], with totals and percentage change from the previous period.

3. Use window functions

Write a query using window functions to [rank, running total, moving average or compare to previous row] for this table: [paste schema].

4. Write a complex join

Write a query joining [tables] to show [result], handling rows with no match. Explain which join type to use and why.

5. Explain a query

Explain what this SQL query does, step by step, in plain English: [paste].

6. Convert between SQL dialects

Convert this [MySQL] query to [PostgreSQL or BigQuery], keeping the same results: [paste].

Design

7. Design a database schema

Design a relational schema for [application] with these features: [list]. Give tables, columns, types, primary and foreign keys, and constraints.

8. Normalise a table

This table has repeated data: [paste schema and sample]. Normalise it to third normal form and explain the trade-offs.

9. Choose SQL or NoSQL

Compare a relational database and a document or key-value database for [use case], considering queries, consistency, scale and team skills.

Performance

10. Optimise a slow query

This query is slow: [paste]. Here is the EXPLAIN output: [paste] and table sizes: [details]. Find the bottleneck and suggest rewrites and indexes.

11. Suggest indexes

Based on these frequent queries: [paste], suggest indexes, including composite indexes, and explain the cost on writes.

12. Read an execution plan

Explain this execution plan in plain English and point out the expensive steps: [paste].

13. Fix N+1 queries

This code runs a query inside a loop: [paste]. Rewrite it to fetch the data in one or two queries.

Changes and maintenance

14. Write a migration

Write a migration to [change, e.g. add a column, rename a table or split a table] in [database or ORM], with a safe rollback and steps that avoid long locks on large tables.

15. Update data safely

Write an UPDATE to [change] for rows matching [condition]. First give a SELECT that previews affected rows, then run it in a transaction.

16. Clean duplicate data

Write queries to find and remove duplicate rows in [table] based on [columns], keeping the most recent.

17. Write a backup and restore plan

Write a backup and restore plan for a [database] holding [data type], including frequency, retention, testing restores and point-in-time recovery.

18. Secure database access

Review these database users and permissions and suggest least-privilege roles for [application, reporting and admin]: [paste].

19. Write SQL for WordPress

Write a MySQL query for the WordPress database to [task, e.g. find posts without a meta description or count posts by month], using the correct table prefix [prefix].

20. Build a query for analytics

Write a BigQuery query on the GA4 export to calculate [metric, e.g. sessions by landing page from organic search], with notes on the event schema.

What are SQL and database prompts?

SQL prompts help you write and explain queries, use window functions and joins, convert between SQL dialects, design database schemas, add indexes, optimise slow queries, write migrations and update data safely. They work for MySQL, PostgreSQL, SQL Server, SQLite and BigQuery.

How to write better SQL and database prompts

  • Share the schema: table and column names make queries accurate.
  • Name your database: SQL dialects differ.
  • Preview before changing data: run a SELECT first and use transactions.
  • Use EXPLAIN: to see why a query is slow.

Best AI tools to use with these prompts

Use ChatGPT or Claude to write and explain SQL, your database’s EXPLAIN tools to analyse performance, DBeaver, TablePlus or pgAdmin to run queries, and BigQuery for GA4 exports and large analytics data.

Frequently asked questions

Can ChatGPT write SQL queries?

Yes, especially when you give it your table structure and database type. Check the results on test data before using them on production.

How do I speed up a slow SQL query?

Look at the execution plan, add or adjust indexes, avoid functions on indexed columns, fetch only needed columns and reduce the rows processed.

Is it safe to run AI-written UPDATE or DELETE queries?

Only after previewing the affected rows with a SELECT, running inside a transaction and having a backup.

Can ChatGPT write SQL queries?

Yes, especially when you share your table structure and database type. Check results on test data first.

How do I make a SQL query faster?

Check the execution plan, add the right indexes, fetch only needed columns and rows, and avoid queries inside loops.

More software development prompts

Back to all software development prompts or the full prompt library, including SEO prompts.

Data drives decisions. SEO data drives traffic. Request a free audit and I will show you what to fix first.

Ready to grow? Let’s talk.

Tell me about your business and goals, and I will reply with the quickest wins I can see.

Your details are only used to reply to your request. No spam.

Grow my trafficCall
WhatsApp