SQL UPDATE Statement (2024)

The SQL UPDATE Statement

The UPDATE statement is used to modify the existing records in a table.

UPDATE Syntax

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

Note: Be careful when updating records in a table! Notice the WHERE clause in the UPDATE statement.The WHERE clause specifies which record(s) that should be updated. If you omit the WHERE clause, all records in the table will be updated!

Demo Database

Below is a selection from the Customers table used in the examples:

CustomerID CustomerName ContactName Address City PostalCode Country
1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany
2 Ana Trujillo Emparedados y helados Ana Trujillo Avda. de la Constitución 2222 México D.F. 05021 Mexico
3 Antonio Moreno Taquería Antonio Moreno Mataderos 2312 México D.F. 05023 Mexico
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
5 Berglunds snabbköp Christina Berglund Berguvsvägen 8 Luleå S-958 22 Sweden

UPDATE Table

The following SQL statement updates the first customer (CustomerID = 1) with a new contact person and a new city.

The selection from the "Customers" table will now look like this:

CustomerID CustomerName ContactName Address City PostalCode Country
1 Alfreds Futterkiste Alfred Schmidt Obere Str. 57 Frankfurt 12209 Germany
2 Ana Trujillo Emparedados y helados Ana Trujillo Avda. de la Constitución 2222 México D.F. 05021 Mexico
3 Antonio Moreno Taquería Antonio Moreno Mataderos 2312 México D.F. 05023 Mexico
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
5 Berglunds snabbköp Christina Berglund Berguvsvägen 8 Luleå S-958 22 Sweden

UPDATE Multiple Records

It is the WHERE clause that determines how many records will be updated.

The following SQL statement will update the ContactName to "Juan" for all records where country is "Mexico":

Example

UPDATE Customers
SET ContactName='Juan'
WHERE Country='Mexico';

The selection from the "Customers" table will now look like this:

CustomerID CustomerName ContactName Address City PostalCode Country
1 Alfreds Futterkiste Alfred Schmidt Obere Str. 57 Frankfurt 12209 Germany
2 Ana Trujillo Emparedados y helados Juan Avda. de la Constitución 2222 México D.F. 05021 Mexico
3 Antonio Moreno Taquería Juan Mataderos 2312 México D.F. 05023 Mexico
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
5 Berglunds snabbköp Christina Berglund Berguvsvägen 8 Luleå S-958 22 Sweden

Update Warning!

Be careful when updating records. If you omit the WHERE clause, ALL records will be updated!

Example

UPDATE Customers
SET ContactName='Juan';

The selection from the "Customers" table will now look like this:

CustomerID CustomerName ContactName Address City PostalCode Country
1 Alfreds Futterkiste Juan Obere Str. 57 Frankfurt 12209 Germany
2 Ana Trujillo Emparedados y helados Juan Avda. de la Constitución 2222 México D.F. 05021 Mexico
3 Antonio Moreno Taquería Juan Mataderos 2312 México D.F. 05023 Mexico
4 Around the Horn Juan 120 Hanover Sq. London WA1 1DP UK
5 Berglunds snabbköp Juan Berguvsvägen 8 Luleå S-958 22 Sweden

W3schools Pathfinder

Track your progress - it's free!

SQL UPDATE Statement (2024)

FAQs

What is the UPDATE statement in SQL? ›

An SQL UPDATE statement changes the data of one or more records in a table. Either all the rows can be updated, or a subset may be chosen using a condition. The UPDATE statement has the following form: UPDATE table_name SET column_name = value [, column_name = value ...]

How to UPDATE in SQL command line? ›

SQL UPDATE Syntax

To use the UPDATE method, you first determine which table you need to update with UPDATE table_name . After that, you write what kind of change you want to make to the record with the SET statement. Finally, you use a WHERE clause to select which records to change.

How to UPDATE two columns in SQL? ›

You'll need to set each column to its respective value, separated by commas: UPDATE table_name SET column1 = value1, column2 = value2, ... columnN = valueN WHERE condition; Notice that the column-value pairs are separated by commas, ensuring that the database system understands which values correspond to which columns.

How to use condition in UPDATE statement SQL? ›

To do a conditional update depending on whether the current value of a column matches the condition, you can add a WHERE clause which specifies this. The database will first find rows which match the WHERE clause and then only perform updates on those rows.

What is the alternative for UPDATE statement in SQL? ›

You can't use UPDATE instead of INSERT, but the UPSERT statement is a good alternative. With the UPSERT statement, the database updates records where they are available and inserts them in the table if the record does not exist. INSERT is faster than an UPDATE statement provided a database is properly optimized.

Which SQL statement is used to UPDATE? ›

The UPDATE statement in SQL is used to update the data of an existing table in the database. We can update single columns as well as multiple columns using the UPDATE statement as per our requirement.

How do I UPDATE all data in SQL? ›

The command syntax UPDATE in SQL is used to update records in a table or dataset. Command has following basic structure: UPDATE [table_name] SET [column1] = [value1], [column2] = [value2], ... WHERE [condition];

How do I insert an UPDATE query in SQL? ›

Syntax: UPDATE TableName SET Column1 = Value1, Column2 = Value2, ..., ColumnN = ValueN WHERE Condition; Here, the WHERE clause specifies which records must be updated. Just in case, you omit the WHERE clause, all the records existing in the table will be updated.

What is the best practice for SQL update? ›

RULES & BEST PRACTICE FOR SQL UPDATE STATEMENT

Do not update all the table rows at once. Always filter rows to update using the WHERE clause. Table relationships, e.g., foreign key constraints, must be considered while updating a table. Major updates should only be performed during low peak usage times.

How to UPDATE 2 rows at a time in SQL? ›

Here's what your query would look like: UPDATE your_table SET column1 = 'new_value' WHERE some_column = 'some_value'; Dive into it, this syntax is saying, "Yo SQL Server, for all rows in your_table where some_column equals some_value , switch the value in column1 to new_value ."

How do I UPDATE all columns at once? ›

The SQL UPDATE statement is widely used to modify existing data in a table. By combining it with the SET clause, we can specify the columns that need to be updated along with their new values. Multiple columns can be updated at once by separating each column-value pair with a comma.

How do I UPDATE each column in SQL? ›

We can update the columns of a table using the UPDATE statement. The SET command is used inside the UPDATE statement to specify the columns to update. The WHERE command is used after the SET command to specify the conditions. The cells that satisfy the conditions are updated.

How to do an UPDATE query? ›

Create and run the update query
  1. On the Create tab, in the Queries group, click Query Design.
  2. Select Add Tables.
  3. Double-click your source and destination tables to add them to the query. ...
  4. In most cases, Access automatically joins related fields in a query. ...
  5. On the Query Design tab, in the Query Type group, click Update.

What is the syntax for UPDATE in SQL? ›

Syntax. UPDATE table_name SET column1 = value1, column2 = value2,..., columnN = valueN WHERE [condition]; You can combine N number of conditions using the AND or the OR operators.

Can we use UPDATE and SELECT together? ›

Performing an UPDATE using a secondary SELECT statement can be accomplished in one of two ways, primarily depending upon which version of SQL Server you are using. We'll briefly explore both options so you can find what works best for you.

What is the purpose of the update command? ›

UPDATE Command is used to update existing records in a database. ALTER Command by default initializes values of all the tuple as NULL. UPDATE Command sets specified values in the command to the tuples. This command make changes with table structure.

What does SQL UPDATE statement return? ›

Returns updated data or expressions based on it as part of the UPDATE operation. The OUTPUT clause is not supported in any DML statements that target remote tables or views. For more information about the arguments and behavior of this clause, see OUTPUT Clause (Transact-SQL).

What is the SQL keyword for update? ›

The basic SQL UPDATE syntax comes down to using keyword UPDATE followed by the name of our object (table or table alias) and the SET column name equals to some values. The FROM clause will come into play when we do joins and we can also have a WHERE clause when we need to update only a portion of data in a table.

What is the formula for update table in SQL? ›

Syntax Copy
  • table_name: Name of the table to update.
  • column1: Name of the column to be updated.
  • value1: The new value to be updated.
  • column2: column name to filter the search.
  • value2: The existing value to be searched to filter the row.

Top Articles
Latest Posts
Article information

Author: Greg Kuvalis

Last Updated:

Views: 5948

Rating: 4.4 / 5 (55 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Greg Kuvalis

Birthday: 1996-12-20

Address: 53157 Trantow Inlet, Townemouth, FL 92564-0267

Phone: +68218650356656

Job: IT Representative

Hobby: Knitting, Amateur radio, Skiing, Running, Mountain biking, Slacklining, Electronics

Introduction: My name is Greg Kuvalis, I am a witty, spotless, beautiful, charming, delightful, thankful, beautiful person who loves writing and wants to share my knowledge and understanding with you.