Convert Persian question to SQL query
Takes your table structures and question to write a correct, optimized, and commented SQL query.
Prompt text
Parts to replace
- [Database type]: MySQL, PostgreSQL, SQLite, SQL Server…
- [Table structure]: Table and column names with their data types (or CREATE TABLE statement)
- [Question]: e.g., "10 customers who have made the most purchases in the last three months"
Why does this prompt work well?
Models guess column names when they lack the actual table structure. By providing the structure and prohibiting non-existent columns, direct queries can be executed.
Pro tips
Run the query on a database test version first, especially if it contains UPDATE or DELETE.
Execution result on models
Executed prompt example (with variables filled in):
These are English translations of the original Persian lab outputs. The models were not rerun.
The prototype written by Claude Opus 5.5 when building the site. Regenerated under standard conditions by running the lab (python -m sitegen lab --force).
SELECT
c.id,
c.name,
COUNT(o.id) AS order_count,
SUM(o.total_amount) AS total_spent
FROM customers AS c
JOIN orders AS o
ON o.customer_id = c.id
WHERE o.status = 'paid'
AND o.created_at >= NOW() - INTERVAL '90 days'
GROUP BY c.id, c.name
ORDER BY total_spent DESC, c.id
LIMIT 10;
Line-by-Line Explanation
SELECT ...: Returns the customer ID and name, order count, and total amount.FROM customers AS c JOIN orders AS o ON ...: Connects each customer to their orders. Since a regularJOINis used, customers with no orders are automatically excluded.WHERE o.status = 'paid': Only successful orders are counted.AND o.created_at >= NOW() - INTERVAL '90 days': Only orders from the last 90 days.GROUP BY c.id, c.name: Aggregates the results for each customer.ORDER BY total_spent DESC, c.id: Highest amount first; in case of a tie, the smaller ID comes first to keep the result order consistent.LIMIT 10: Only the first 10 customers.
Assumptions
- "Last 90 days" is calculated based on the order creation time (
created_atin theorderstable), as there is no separate payment time column. NOW()depends on the timezone configured on the database server.
Index Suggestion
If the orders table is large, this partial index speeds up the status and date filter and includes the necessary columns:
CREATE INDEX idx_orders_paid_created
ON orders (created_at)
INCLUDE (customer_id, total_amount)
WHERE status = 'paid';
(INCLUDE is supported starting from PostgreSQL 11.)
Outputs are saved unedited and directly from the models themselves.