Skip to main content

ChatGPT/Copilot Prompt to Build and Optimize SQL Queries

Use GitHub Copilot to write complex SQL queries, optimize slow ones, and build reporting queries with proper joins and indexing recommendations for any schema.

Fill in the placeholders

Edit the values, then copy your finished prompt.

Your Prompt
prompt.txt

                                

What this prompt does

This prompt uses Copilot to write and optimize SQL for your [database], given a schema described by [schema_description]. It generates a query for [primary_query] using proper joins rather than subqueries where joins perform better, plus a reporting query that aggregates [report_metrics] grouped by [dimensions] with correct NULL handling. The point is not just to draft SQL but to reason about why one shape is faster than another.

The structure works because it covers the whole lifecycle from writing through tuning. It takes a [slow_query_pattern] and rewrites it for performance with index suggestions, generates window-function queries for [window_tasks] covering ROW_NUMBER, LAG/LEAD, and running totals, builds a CTE-based query for the hierarchical [recursive_task], and writes data-migration queries that safely perform [migration_task] with transaction safety and rollback. It also explains how to read EXPLAIN ANALYZE output so you can spot bottlenecks yourself, and produces the [orm] equivalent of each query so the raw SQL stays consistent with your application code. Reading EXPLAIN ANALYZE together is how a slow correlated subquery becomes a fast join, and generating the ORM equivalent keeps everything aligned with how the app actually talks to the database.

When to use it

  • You need a complex analytical or reporting query written correctly the first time.
  • You have a slow query and want it rewritten with index recommendations.
  • You need window functions for rankings, month-over-month growth, or running totals.
  • You are querying hierarchical data and need a recursive CTE.
  • You are writing a data migration and want transaction safety and a rollback path.
  • You want the ORM equivalent so raw SQL and application code stay consistent.

Example output

Expect a query toolkit rather than a single statement. You get the primary join-based query, an aggregation and reporting query with NULL handling, a rewritten version of your slow pattern plus suggested indexes, window-function queries, a recursive CTE for the tree, and a transaction-safe migration with rollback. Each often comes with a guide to reading the EXPLAIN ANALYZE plan and an [orm] equivalent, so you can both run the SQL and understand why it performs the way it does before committing it.

Pro tips

  • Describe [schema_description] including rough table sizes, since "millions of rows in orders" changes which plan is actually fast.
  • Give the real shape of [slow_query_pattern], such as a correlated subquery in a WHERE clause, so the rewrite targets the genuine bottleneck.
  • Treat index suggestions as hypotheses and validate them with EXPLAIN ANALYZE on real data before adding them to production.
  • Match [orm] to what you actually use so the generated equivalent fits your code style and naming.
  • For [migration_task], always keep the transaction and rollback guidance, because backfills on large tables can lock or fail midway.
  • Read EXPLAIN ANALYZE together with the query, since the plan, not intuition, tells you whether the rewrite genuinely helped.

Frequently Asked Questions

Will the suggested indexes definitely speed up my queries?
Not guaranteed. The prompt suggests indexes as hypotheses based on the query shape, but the only way to confirm is running EXPLAIN ANALYZE on your real data. Indexes also cost write performance and storage, so validate before adding them in production.
Can it rewrite a slow correlated subquery?
Yes. You provide the slow pattern in `[slow_query_pattern]`, and the prompt rewrites it for performance, often turning a correlated subquery into a join. Reading the EXPLAIN ANALYZE plan together is how you confirm the rewrite is actually faster, not just different.
Does it generate the ORM version too?
Yes. It produces the equivalent in the ORM you set in `[orm]`, such as Prisma, so the raw SQL and your application code stay consistent. Verify the ORM version, since complex queries with window functions or CTEs may need raw fragments even in an ORM.
Is it tied to a specific database?
It adapts to whatever you set in `[database]`, like PostgreSQL 16. SQL dialects differ in window functions, recursive CTE syntax, and EXPLAIN output, so naming your exact database and version produces queries that run without dialect surprises.
Engr Mejba Ahmed

Need this built for real?

Engr Mejba Ahmed

AI Developer · Software Engineer

I'm Mejba — I design and ship production AI systems, automations, and full-stack apps. If you want this turned into a working solution for your team, let's talk.

More in GitHub Copilot Prompts

Engr Mejba Ahmed

Engr Mejba Ahmed

Claude Code Expert · Online

👋

Hey there!

Quick Actions

WhatsApp Instant reply

Chat on WhatsApp

+880 1723 741224 · Instant reply

Popular Questions

Engr Mejba Ahmed is connected
Engr Mejba Ahmed is typing...
Engr Mejba Ahmed avatar

✉ Want me to follow up? Drop your email

Engr Mejba Ahmed avatar

📞 Connect Directly

Choose how you'd like to reach me

WhatsApp

+880 1723 741224

Email

[email protected]

✓ Details sent! I'll get back to you shortly.

Powered by OpenAI

335+

Blog Posts

25

AI Courses

63

Projects

Services & Expertise

Pricing & Process

Learning & Resources

Connect & Support