Skip to main content
All AI tricks
Guided workflowDevelopmentIntermediate

Run AI-Written SQL Safely: SELECT First, Then Change

A wrong WHERE clause can wipe a table. Test on a copy, turn UPDATE or DELETE into a SELECT to count rows, then run inside a transaction you can roll back.

12 min practicePlatform independentUpdated · Last checked Oct 24, 2026
Jump to the lesson ↓
Abstract illustration of a database cylinder with an undo arrow, in the colours of the Development topic.
Original conceptual illustration made for this lesson. Not a screenshot, product test or endorsement.

The useful result

Run an AI-written UPDATE or DELETE with a preview count, a copy to test on and a rollback option.

  • Run an AI-written UPDATE or DELETE with a preview count, a copy to test on and a rollback option.

An assistant writes an UPDATE that looks right. If the WHERE clause is missing or too broad, it changes every row, and on a live database that can be a very bad afternoon. SQL from a model is a draft: it may be syntactically fine and still touch the wrong rows.

Three habits make it safe. Work on a copy first. Convert any change statement to a SELECT with the same conditions and compare the row count with your expectation. Then run the real change inside a transaction so you can inspect the result and roll back. PostgreSQL’s documentation explains how a transaction block groups statements until you COMMIT or ROLLBACK.

The examples below are illustrative exercises, not recorded results from any tool. Sources were checked on 24 October 2026; menus, limits and prices change, so confirm details on the linked pages.

Before you start

  • A database with a development copy or a recent backup
  • A SQL client
  • The query the assistant wrote and a table description

Review evidence

Documentation-based review · 2026-10-24

Primary documentation and official guidance checked on 24 October 2026. Examples, checks and exercises are original illustrations, not recorded tool tests. Cover artwork is a conceptual illustration.

1

Test on a copy, never on the live data first

Take a backup or use a development copy of the database. Run the assistant’s statement there first. If you cannot get a copy, create a small test table with ten rows that has the same columns and try it there. The goal is to see the effect before the real data is involved.

Check your result

You have a copy or test table, and the first run happened there, not on live data.

2

Turn the change into a SELECT and count

Rewrite the UPDATE or DELETE as a SELECT that uses the same WHERE clause and count the rows it returns. Compare the count with what you expect. If you expected 12 rows and get 12,000, stop and fix the condition. Ask the assistant to explain the WHERE clause in plain words.

Try this prompt
Here is a table description and an SQL statement. Rewrite it as a SELECT with the same WHERE conditions that returns the rows it would change, and a COUNT query. Explain the WHERE clause in plain words, and tell me what rows would be affected if a condition were missing.

Table: [paste]
Statement: [paste]

Example / expected result

Illustrative example; not a recorded model result.

UPDATE orders SET status = 'void' WHERE created_at < '2025-01-01' becomes SELECT COUNT(*) FROM orders WHERE created_at < '2025-01-01'.

Check your result

The SELECT returns the number of rows you expected, and you can explain the WHERE clause.

3

Run inside a transaction and look before you commit

In PostgreSQL, start the transaction with BEGIN, run the statement, check the results with a SELECT and only then type COMMIT. If anything looks wrong, type ROLLBACK and nothing is kept. Other databases behave differently, and some commands cannot be rolled back, so check how yours works.

Check your result

You inspected the result before committing, and you know how to roll back in your database.

When it goes wrong

Not every statement can be rolled back in every database. Triggers and other connected systems may react as soon as a change is made. Always keep a backup before bulk changes, and never paste real customer rows into an AI chat.

Make it your own

On a ten-row test table, write an AI-generated UPDATE, preview it with a SELECT, run it in a transaction, check the result and practise a ROLLBACK once.

Useful next resources

Sources and review

Last checked Oct 24, 2026. Features and account limits can change. Verify important results against your original material.

Did this help you complete your task?

Tell us what needs improving. No account or private output required.

Was this useful?
Report incorrect information or a broken link

Genius Forges Editorial

Practical AI lessons with checked sources, labelled examples and human verification.

Keep building your AI habits

Keep learning useful AI tricks

Get practical lessons and resources when we publish them.

Turn this idea into a daily skill

Practice with a small input and check the result before sharing.

We use optional cookies for analytics and referral tracking. Learn more about our privacy policy