Format a SQL UPDATE with WHERE
UPDATE statements with multiple columns and compound WHERE conditions are easy to misread, increasing the risk of updating the wrong rows. This example updates a user record with multiple field changes. The formatter puts each SET column on its own line and clearly separates WHERE conditions, making the intent of the query obvious before you run it. Always double-check the WHERE clause before executing an UPDATE in production.
Example
UPDATE users SET name = 'Jane Doe', email = '[email protected]', role = 'admin', updated_at = CURRENT_TIMESTAMP WHERE id = 42 AND active = 1 AND deleted_at IS NULL;
FAQ
- How do I prevent accidentally updating all rows?
- Always include a WHERE clause in UPDATE statements. Some databases support a safe-mode option that rejects updates without a WHERE clause.
- Can I UPDATE rows from a JOIN?
- Yes. MySQL supports UPDATE table1 JOIN table2 ON ... SET ... syntax. PostgreSQL uses UPDATE ... FROM ... WHERE ... syntax instead.
- What is a soft delete?
- Instead of using DELETE, soft delete sets a deleted_at timestamp to mark rows as deleted while keeping them in the table. The WHERE deleted_at IS NULL condition filters them out of normal queries.
Related Examples
Format a SQL SELECT with JOIN
Multi-table JOIN queries quickly become hard to read when written on a single li...
Format a SQL GROUP BY with AggregatesAggregate queries with GROUP BY and HAVING are the backbone of reporting and ana...
Format SQL with Foreign Key ConstraintsReferential integrity through foreign keys prevents orphaned records and ensures...