Skip to content
AI Skill Index

Write the SQL and explain what it will do

Produces the query plus how it will actually execute at your row counts.

Author: Editorial
Sign in to save
Write a query for this.

Question: [WHAT I WANT TO KNOW]
Schema: [PASTE RELEVANT TABLES]
Row counts: [ROUGH SIZES]

Give me:
1. The query.
2. **In plain English, what it does step by step** — which table it starts from, what it joins, what it groups.
3. **How it will perform** at those row counts, and which index makes the difference. If a needed index does not exist, say so.
4. **What it will do wrong** if the data is messier than the schema implies — nulls where you expected values, duplicates, orphaned rows.

If the question is ambiguous — and counting questions usually are, because "active users" means three different things — ask before writing rather than picking one.

Highlighted parts are placeholders. Replace them before you send.

Worked example

The input is invented for this page. The output shows what the prompt asks for with that input.

Example input (invented)

Question: The total of paid orders per calendar month in 2025, by the date each order was paid, and how many different customers paid in each month.

Schema

customers (
  id bigint primary key,
  email text not null,
  created_at timestamptz not null
)
orders (
  id bigint primary key,
  customer_id bigint references customers(id),
  status text not null,
  total_cents integer not null,
  paid_at timestamptz
)

Row counts: customers about 40,000. orders about 900,000, roughly 300,000 of them paid in 2025.

Example output

SELECT date_trunc('month', paid_at) AS month,
       SUM(total_cents) / 100.0 AS revenue,
       COUNT(DISTINCT customer_id) AS paying_customers
FROM orders
WHERE status = 'paid'
  AND paid_at >= '2025-01-01'
  AND paid_at <  '2026-01-01'
GROUP BY 1
ORDER BY 1;

What it does. It starts from orders and joins nothing, because the customer id is already on the order. It keeps the rows with status paid whose paid_at falls in 2025 and cuts each paid_at down to the first day of its month. Per month it adds up the totals and counts each customer once.

How it will perform. The schema shows no index besides the primary keys, so the query reads all 900,000 orders to keep about 300,000. That is acceptable for a report you run by hand. An index on orders (paid_at) limits the read to 2025. It does not exist yet.

What it will do wrong.

  • A paid order with no paid_at is left out without a warning.
  • customer_id can be null. Such an order adds to revenue and not to paying_customers.
  • A month without paid orders is missing from the result. It does not appear with a zero.
  • paid_at has a time zone, so month edges follow the session's zone. An order paid at 23:30 UTC on 31 January counts as February in a session set to Berlin time.
  • An order that was paid and later set to refunded disappears from its month.
ShareXLinkedIn

3 of the 5 other prompts filed under Data.

Data tools and skills

1 tool and 3 skills are filed under Data, the same category as this prompt.

Tool

Julius AI

Chat with a spreadsheet and get analysis plus charts.

Freemium
Skill

Data cleaner

Profiles a messy dataset and proposes fixes before changing anything.

Claude Code, Any agent
Skill

Data table analyst

Reads a table and reports what changed, with the caveats left in.

Claude.ai, ChatGPT, Any agent
Skill

Migration safety checker

Finds the database migrations that will lock a table or break running code.

Claude Code, Cursor, Any agent

Cookies

We use only the cookies this site needs to work. Optional analytics helps us see which content is worth writing more of — nothing loads until you say yes.

Read the privacy policy