# The Prompt That Writes SQL I Do Not Have To Check Twice

> Give it the schema and one rule about NULLs.

- **Author:** [Sanne de Vries (@sanne_devries)](https://www.promptabide.com/sanne_devries)
- **Published:** 2026-08-17
- **Updated:** 2026-08-23
- **Tags:** `data`, `sql`, `analytics`, `postgres`
- **Views:** 5370
- **Likes:** 410

## Prompt

```
Two things fixed most of my generated-SQL problems.

**One: paste the actual DDL.** Not a description of the tables — the CREATE TABLE statements, with constraints. The constraints tell it which joins can fan out and which cannot, and that is where the wrong-row-count bugs come from.

**Two: state the NULL policy.** "Treat NULL revenue as excluded, not as zero." Every aggregation question has this ambiguity and the model has to guess unless you say. It will pick the wrong one about a third of the time, and the result looks completely plausible.

The rest of my prompt is just:

"Write one query. Use CTEs, not nested subqueries. Comment any join that could change the row count. Show me the expected column list before the SQL."

That last clause is a cheap correctness check — if the column list is wrong you know without reading the query.
```

## Output

*Produced by ChatGPT · gpt-4-turbo.*

```
Expected columns: month (date), active_customers (int), net_revenue (numeric)

WITH paid AS (
  -- one row per invoice; no fan-out, invoice_id is unique
  SELECT customer_id, date_trunc('month', paid_at) AS month, amount
  FROM invoices
  WHERE paid_at IS NOT NULL AND amount IS NOT NULL
)
SELECT month, count(DISTINCT customer_id) AS active_customers, sum(amount) AS net_revenue
FROM paid GROUP BY month ORDER BY month;
```

---

Canonical HTML: https://www.promptabide.com/bides/sql-i-dont-check-twice-sanne-devries-2
Agent guide: https://www.promptabide.com/llms.txt · https://www.promptabide.com/agent-instructions.md
Sitemap: https://www.promptabide.com/sitemap.xml
