Interview Q&A · SQL · 10 questions in this topic

UPDATE, DELETE & Safe Writes — SQL Interview Q&A

All SQL topics

Changing data without disasters — guarded writes, transactions, upserts and bulk patterns. Part of Modifying Data · 192 questions in the SQL bank.

How do UPDATE and DELETE work?

Answer

UPDATE SET assigns new values to matching rows; DELETE removes matching rows. Both take WHERE — without it, every row changes. Both run inside transactions, so wrapping in BEGIN ... COMMIT/ROLLBACK makes them reviewable and reversible.

The safety ritual: SELECT the WHERE first, then convert to UPDATE/DELETE.

Example

Example.sqlsql
-- look before you leap:
SELECT * FROM employees WHERE id = 99;
UPDATE employees SET salary = 95000 WHERE id = 99;
DELETE FROM employees WHERE id = 99;

💡 SELECT-first is the safety habit interviewers want to hear, not just the syntax.

How do you update safely in production (guarded writes)?

Answer

Preview with SELECT COUNT(*) on the same WHERE, run inside an explicit transaction, verify ROW_COUNT, then COMMIT — with a ROLLBACK path rehearsed. LIMIT on UPDATE/DELETE (MySQL) adds a blast-radius cap; staging the change on a copy first proves the predicate.

No WHERE, no run: the best UPDATE is the one you previewed.

Key points

  1. 1Preview — SELECT the predicate, check the count.
  2. 2Transact — BEGIN, verify, then COMMIT.
  3. 3Cap — LIMIT guards where supported.

Example

Example.sqlsql
BEGIN;
UPDATE employees SET salary = salary * 1.1 WHERE department = 'Sales';
-- check row count, spot-check rows...
COMMIT;  -- or ROLLBACK

How do you update from another table (UPDATE ... JOIN / FROM)?

Answer

PostgreSQL/SQLite use UPDATE ... FROM (join the source in FROM, correlate in WHERE); MySQL/SQL Server use UPDATE ... JOIN. Both must correlate exactly — a missing join condition cross-updates every row.

Correlated-subquery form (SET col = (SELECT ...)) is the portable fallback where JOIN-update is unsupported.

Example

Example.sqlsql
-- PostgreSQL form:
UPDATE employees e SET salary = b.new_pay
FROM pay_bands b WHERE e.band = b.band;

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits