Prompt engineering for SQL
Practical tips that get better SQL out of Cocobox's AI features.
Models have improved a lot, but prompt structure still moves the needle — especially for SQL where one wrong column reference makes the whole query useless.
Lead with the goal, not the SQL
❌ “Write a query that joins users with orders and groups by month.”
✅ “Top 10 paying customers in March 2026 by total order value, including their email.”
The first prompt forces the model to guess what you actually want. The second is unambiguous.
Name the tables you care about
If the schema is large, the model spends tokens scanning. Pin down the tables:
“Use
users,orders, andorder_items. Top 10 paying customers in March 2026 by total order value.”
The model will still inspect those tables’ columns from the schema digest, but it won’t go wandering.
Be explicit about NULLs and ties
Edge cases dominate SQL bugs.
“If a customer has no orders, don’t include them. Tie-break alphabetically by email.”
Use the dialect’s idioms
Cocobox tells the model your dialect, but you can hint:
“Postgres. Use
DATE_TRUNCandFILTER.”
“MySQL 8. Use a window function.”
This reduces the chance of getting a query that runs but is slower than necessary.
”Fix this” works better with the error
When something breaks, paste the error verbatim. Cocobox already attaches the failed statement; the error message is what disambiguates “wrong column” from “wrong join type”.
Schema redaction for safety
If your schema has columns like users.email or payments.card_last4, consider Settings → AI → Schema redaction with *.email, *.card_*. The model sees the column name pattern but the redacted name is masked, so it won’t accidentally embed PII into example queries it generates.
When the model is stuck
If you’ve tried twice and the SQL still isn’t right:
- Switch to
tambor(Opus) — it’s slower and more expensive but reasons better over big schemas. - Ask for a plan first: “Don’t write SQL. Just explain in 3 bullets how you’d compute this.”
- Then ask for the SQL informed by the plan.
This two-step pattern is the single biggest quality win for hard queries.
In Chats: keep context tight
Long conversations dilute attention. When a chat turns into a wandering investigation:
- Open a new chat.
- Paste the one or two important results from the old chat into the new one.
- Continue.
Short, focused chats outperform long ones, model-for-model.