MyRelay

← Back to the notebook

I Check One SQL Query Before Paying for the Next

Start with one question you can check yourself

For a class project, generating SQL from plain English is useful when it helps you understand a database, not when it replaces that understanding. A small library database makes a manageable example. Suppose your question is: Which members borrowed at least three books during March, and how many did each borrow?

Write down the expected result before asking a model anything. You want one row per qualifying member, with their identifier, name, and borrowing count. Decide whether “three books” means three loan records or three distinct titles. That distinction can change the query even when both versions look reasonable.

Prepare a small schema instead of uploading everything

Give the model the database dialect and only the tables needed for this question. For this walkthrough, use SQLite and describe a members table with member identifiers and names, plus a loans table with loan identifiers, member identifiers, book identifiers, and borrowing dates. Explain that the member identifier connects the two tables.

Describe dates precisely. For example, borrowing dates are stored as consistently formatted ISO date text, and March means March of a specified year. You do not need real names, a full database export, or unrelated tables. A compact schema reduces input tokens and gives the model fewer opportunities to guess.

Send one tightly scoped request through MyRelay

If you need an API connection for your project, create a MyRelay account and use its OpenAI-compatible interface with your chosen SDK. MyRelay gives an individual developer one key and one pay-as-you-go budget across supported models, with per-request token and cost visibility. Keep that key outside anything you submit or share with classmates.

Start with a model such as Gemini 2.5 Flash and measure its cost rather than assuming every request will be negligible. Ask for one SQLite SELECT query and a brief explanation, not several alternatives or a complete application. Include your schema, the reporting year, and your definition of a borrowing count in the same request.

Your request should also ask the model to state assumptions and avoid invented columns. Specify that members with fewer than three qualifying loans should be excluded, and that equal counts should have a predictable secondary ordering, such as member identifier.

Read the generated query before running it

Check that every table and column exists. Confirm that the join uses member identifiers rather than names, since two members can share a name. Look for grouping that keeps different members separate and a group-level condition that applies the minimum borrowing count after aggregation.

Inspect the date boundaries carefully. A range starting at the beginning of March and ending before the beginning of April is often clearer than matching a month number alone. Matching only the month can accidentally include records from multiple years. If the explanation is unclear, ask one focused follow-up rather than requesting a complete rewrite.

Test boundary cases in a disposable database

Run the query against a small local test database first. Include a member with two March loans, another with exactly three, and another with four. Add a February loan, an April loan, and two members with identical names. Include a repeated borrowing of the same title to test your counting definition.

Calculate the expected rows manually and compare them with the output. A query that executes successfully can still answer the wrong question. Use a database copy or a connection that technically prevents writes; asking the model for read-only SQL is not itself a security control.

Save the verified result and stop the request loop

Once the result matches your test cases, save the query, its assumptions, and a short explanation in your project notes. Follow your course policy on disclosing AI assistance, and make sure you can explain the join, grouping, and date filter yourself.

Review the request’s tokens and cost in MyRelay before continuing. Reuse your compact schema description, but avoid resending an entire conversation for every question. The spending habit is simple: generate one query, verify it locally, and pay for another request only when you have a specific unresolved problem.

More to read