Gemini Database Queries Prompt Template
You get a correct query for your actual schema and database, with an explanation of how it works and an index if it needs one. This version is set up for Gemini: attach files or images the prompt refers to, or paste the text below the instructions. Below you'll find the prompt, how to run it in Gemini, a worked example and answers to common questions.
Write a database query for me. ## Database Engine and version: [e.g. PostgreSQL 16] Relevant tables and columns: [paste CREATE TABLE statements or a list of tables with column names and types] Approximate row counts: [e.g. orders: 5 million] ## What I need [plain description, e.g. top 5 customers by paid revenue in the last 30 days] ## Rules - Only use the tables and columns I listed. If something is missing, ask. - Handle NULLs and duplicates explicitly. - Use syntax for the engine named above. - No SELECT *. ## Output format 1. The query, formatted for readability 2. A short explanation of each clause 3. A sample of what the result looks like 4. Index suggestions if the query would be slow at the row counts I gave Context - Reference material: [paste it below, or attach the file]
How to use this prompt
- 1
Open Gemini
Start a new chat in Gemini and paste the prompt.
- 2
Give it context
Attach files or images the prompt refers to, or paste the text below the instructions.
- 3
Review and iterate
Reply in the same chat to refine the result.
- 4
Save what you repeat
For a prompt you run often, save it as a Gem with custom instructions.
Working in Gemini
- •Gemini accepts images and files as well as text, so attach a screenshot when the task is visual.
- •Ask for the exact output format; otherwise answers tend to come with extra headings and summaries.
- •If you use Google Docs or Sheets, ask for output you can paste straight in, such as a table.
Worked example
What you give it
Engine: PostgreSQL 16 Tables: customers(id bigint, name text) orders(id bigint, customer_id bigint, total_cents integer, status text, created_at timestamptz) Rows: orders about 5 million. Need: top 5 customers by paid revenue in the last 30 days, in dollars. Ignore refunded and pending orders.
What a good answer looks like (shortened)
1. Query: SELECT c.id, c.name, SUM(o.total_cents) / 100.0 AS revenue FROM orders o JOIN customers c ON c.id = o.customer_id WHERE o.status = 'paid' AND o.created_at >= now() - interval '30 days' GROUP BY c.id, c.name ORDER BY revenue DESC LIMIT 5; 2. The WHERE clause keeps only paid orders from the last 30 days. GROUP BY totals them per customer, and dividing by 100.0 converts cents to dollars as a decimal. 3. Result: id 482, name "Acme Co", revenue 12840.50 4. Index: CREATE INDEX orders_paid_recent ON orders (status, created_at) INCLUDE (customer_id, total_cents); This lets Postgres find recent paid orders without reading the whole table.
Tips for better results
- •Paste real CREATE TABLE statements in the Database section. Types matter: cents stored as integers, timestamps with or without time zones, and text versus enum all change the query.
- •Give row counts. They decide whether the index suggestions in item 4 matter or can be skipped.
- •If you use Google Docs or Sheets, ask for output you can paste straight in, such as a table.
- •Avoid this: Describing tables loosely instead of pasting the schema, so the AI invents column names that do not exist.
Get the free prompt pack
Our best coding prompts in one Markdown file, delivered by email. The templates here stay free either way — the pack just saves you the copying.
Variations
Optimize an existing query
Replace What I need with: This query is slow [paste query and EXPLAIN ANALYZE output]. Explain why and rewrite it.
Schema design
Replace What I need with: Design tables for [feature], with primary keys, foreign keys, indexes and a reversible migration.
ORM version
Add this line to Output format: 5. The same query written with [Prisma, Drizzle, ActiveRecord or SQLAlchemy].
Save it as a Retext snippet
Retext is a free text expander for Mac. Save this prompt once, then type ;sqlquery in any app (ChatGPT, Claude, your editor, a browser tab) and the full prompt is pasted in.
- Click Add to Retext. The prompt installs as a snippet with the trigger ;sqlquery.
- Change the trigger in Retext if you prefer a different one.
- Type the trigger wherever you want the prompt, then fill in the brackets.
Frequently asked questions
How do I stop AI from making up column names in SQL?
Paste the exact schema and tell it to use only those columns and to ask if something is missing. Running the query against a test database catches anything it still gets wrong.
Can I save this prompt in Gemini?
Yes, as a Gem with the prompt as its instructions. A Retext snippet also works if you switch between Gemini and other tools.
Is it safe to run AI-written SQL on production?
Run it on a copy or staging database first, especially UPDATE and DELETE statements. Wrap writes in a transaction so you can roll back if the row count looks wrong.
How do I reuse this prompt without copying it every time?
Click Add to Retext to save it as a snippet. Then type ;sqlquery in any Mac app, including your AI tool, and the full prompt is pasted in.
Related Templates
Get the free prompt pack
Our best coding prompts in one Markdown file, delivered by email. The templates here stay free either way — the pack just saves you the copying.