ProgrammingIntermediate

Convert Persian question to SQL query

Takes your table structures and question to write a correct, optimized, and commented SQL query.

Prompt text

My database is [Database type] and has these tables: [Table structure] Write an SQL query to answer this question: [Question] - Only use the tables and columns I provided. - Provide the SQL query with line-by-line Persian explanations. - If the question is ambiguous, state your assumption explicitly. - If an index improves the speed of this query, suggest it.

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):

My database is PostgreSQL and has these tables: customers(id serial primary key, name text, city text, created_at timestamp) orders(id serial primary key, customer_id int references customers(id), total_amount numeric, status text, created_at timestamp) Write an SQL query to answer this question: The top 10 customers with the highest successful purchase amount (status = 'paid') in the last 90 days, along with the order count and total amount. - Only use the tables and columns I provided. - Provide the SQL query with line-by-line Persian explanations. - If the question is ambiguous, state your assumption explicitly. - If an index improves the speed of this query, suggest it.

These are English translations of the original Persian lab outputs. The models were not rerun.

Claude Opus 5.5claude-opus-5-5Oct 28, 2026

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 regular JOIN is 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_at in the orders table), 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.