UPDATE

SQL · Modifying Data

Theory

UPDATE changes values in rows that already exist. SET lists the columns and their new values, and WHERE picks which rows to change: UPDATE employees SET salary = salary + 5000 WHERE dept = 'HR' gives every HR employee a raise and leaves everyone else alone. The new value can be a constant, an expression using the old value, or a subquery.

Without WHERE, every row in the table is updated, which is one of the most common and costly SQL mistakes. A safe habit is to run the same WHERE as a SELECT first and check which rows it returns. All SET expressions use the old row values, so SET a = b, b = a swaps two columns in standard SQL. Constraints are checked after the change, and updating a row from another table uses UPDATE ... FROM (PostgreSQL, SQL Server) or a JOIN in UPDATE (MySQL).

Why we use it

Data changes over time: salaries rise, addresses move, order statuses progress. UPDATE edits the existing rows in place so the table always reflects the current facts.

When to use it

Use UPDATE to correct or change values in existing rows. To add new rows use INSERT, and to insert-or-update in one step use MERGE or an upsert. Run it inside a transaction when changing many rows, so you can roll back.

How it works

  1. UPDATE employees SET ... WHERE ...
  2. Read the next row
  3. Matches the WHERE condition? Yes: Compute new values from old ones. No: Leave the row unchanged.
  4. Check constraints and write the new values
  5. Repeat for every row
  6. Report how many rows were updated

Syntax

UPDATE table
SET column = value
WHERE condition;

Example

UPDATE employees SET salary = 70000
WHERE name = 'Asha Rao';

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic