Open In App

SQL UPDATE Statement

Last Updated : 10 Jun, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

SQL UPDATE Statement modifies the existing data from the table.

UPDATE Statement in SQL

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.

In a very simple way, we can say that SQL commands(UPDATE and DELETE) are used to change the data that is already in the database. The SQL DELETE command uses a WHERE clause.

Update Syntax

The syntax for SQL UPDATE Statement is :

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

Where,

  • table_name: name of the table
  • column1: name of first, second, third column….
  • value1: new value for first, second, third column….
  • condition: condition to select the rows for which the 

Parameter Explanation

  1. UPDATE: Command is used to update the column value in the table.
  2. WHERE: Specifies the condition which we want to implement on the table.

Note: In the above query the SET statement is used to set new values to the particular column and the WHERE clause is used to select the rows for which the columns are needed to be updated. If we have not used the WHERE clause then the columns in all the rows will be updated. So the WHERE clause is used to choose the particular rows. 

SQL UPDATE Statement Examples

Let’s see the SQL update statement with examples.

First we will create a table, on which we will use the UPDATE Statement. To create the table, write the following query:

Query:

SQL
CREATE TABLE Customer(
    CustomerID INT PRIMARY KEY,
    CustomerName VARCHAR(50),
    LastName VARCHAR(50),
    Country VARCHAR(50),
    Age int(2),
  Phone int(10)
);

-- Insert some sample data into the Customers table
INSERT INTO Customer (CustomerID, CustomerName, LastName, Country, Age, Phone)
VALUES (1, 'Shubham', 'Thakur', 'India','23','xxxxxxxxxx'),
       (2, 'Aman ', 'Chopra', 'Australia','21','xxxxxxxxxx'),
       (3, 'Naveen', 'Tulasi', 'Sri lanka','24','xxxxxxxxxx'),
       (4, 'Aditya', 'Arpan', 'Austria','21','xxxxxxxxxx'),
       (5, 'Nishant. Salchichas S.A.', 'Jain', 'Spain','22','xxxxxxxxxx'); 
       
       Select * from Customer;

The created table will look like this:

demo sql table

Update Single Column Using UPDATE Statement Example

Update the column NAME and set the value to ‘Nitin’ in the rows where the Age is 22.

Query:

UPDATE Customer SET CustomerName  
= 'Nitin' WHERE Age = 22;

Output:

update single column using update statement example output

Updating Multiple Columns using UPDATE Statement Example

Update the columns NAME to ‘Satyam’ and Country to ‘USA’ where CustomerID is 1.

Query:

UPDATE Customer SET CustomerName = 'Satyam', 
Country = 'USA' WHERE CustomerID = 1;

Output:

updating multiple column using update statement example output

Note: For updating multiple columns we have used comma(,) to separate the names and values of two columns.

Omitting WHERE Clause in UPDATE Statement

If we omit the WHERE clause from the update query then all of the rows will get updated.

Query:

UPDATE Customer SET CustomerName = 'Shubham';

Output: 

The table Customer will now look like this,

omit where clause in update statement example output

Important Points About SQL UPDATE Statement

  • SQL UPDATE Statement is used to update data in an existing table in the database.
  • The UPDATE statement can update single or multiple columns using the SET clause.
  • The WHERE clause is used to specify the condition for selecting the rows to be updated.
  • Omitting the WHERE clause in an UPDATE statement will result in updating all rows in the table.


Previous Article
Next Article

Similar Reads

How to Update Multiple Columns in Single Update Statement in SQL?
In this article, we will see, how to update multiple columns in a single statement in SQL. We can update multiple columns by specifying multiple columns after the SET command in the UPDATE statement. The UPDATE statement is always followed by the SET command, it specifies the column where the update is required. UPDATE for Multiple ColumnsSyntax: U
3 min read
How to Update Two Tables in One Statement in SQL Server?
To update two tables in one statement in SQL Server, use the BEGIN TRANSACTION clause and the COMMIT clause. The individual UPDATE clauses are written in between the former ones to execute both updates simultaneously. Here, we will learn how to update two tables in a single statement in SQL Server. SyntaxUpdating two tables in one statement in SQL
3 min read
PL/SQL UPDATE Statement
The UPDATE statement in the PL/SQL(Procedural Language/ Structural Query Language) is the powerful SQL (Structured Query Language) command used to modify the existing data in the database table. In this article, we will explain the PL/SQL UPDATE Statement, its syntax, and examples in detail. PL/SQL UPDATE StatementThe UPDATE statement in the PL/SQL
7 min read
How to Use PL/SQL Insert, Update, Delete And Select Statement?
PL/SQL is a powerful extension of SQL specifically designed for Oracle databases. It enables developers to create procedural logic and execute SQL commands within Oracle database environments. In this article, we will explore the usage of fundamental PL/SQL statements such as INSERT, UPDATE, DELETE, and SELECT with the help of various examples that
6 min read
SQLite Update Statement
SQLite is a database engine. It is a software that allows users to interact with relational databases, Basically, it is a serverless database which means it does not require any server to process queries. With the help of SQLite, we can develop embedded software without any configurations. SQLite is preferable for small datasets. Update StatementSo
3 min read
MariaDB UPDATE Statement
MariaDB uses SQL (Structured Query Language) and it is an open-source relational database management system (RDBMS) for managing and manipulating data. MariaDB is known for its high performance, even on large datasets. This makes it a good choice for applications that require fast data access. MariaDB can be used to handle large amounts of data wit
6 min read
MySQL INSERT ON DUPLICATE KEY UPDATE Statement
MySQL INSERT ON DUPLICATE KEY UPDATE statement is an extension to the INSERT statement, that if the row being inserted already exists in the table, it will perform a UPDATE operation instead. INSERT ON DUPLICATE KEY UPDATE in MySQLINSERT ON DUPLICATE KEY UPDATE statement in MySQL is used to handle duplicate entries on a primary key or unique column
3 min read
MySQL UPDATE Statement
MySQL is a popular relational database management system used in applications ranging from small projects to large enterprises. The UPDATE statement in MySQL is essential for modifying existing data in a table. It's commonly used to correct errors, update values, and make other necessary changes. This article explores the structure and use cases of
5 min read
PostgreSQL UPDATE Statement
In PostgreSQL, the UPDATE statement is important for modifying existing data in a table. It allows users to change specific values based on conditions, making it a powerful tool for maintaining accurate and updated information in any database. In this article, we will cover the syntax and practical examples of the UPDATE statement in PostgreSQL. Po
4 min read
Difference between Structured Query Language (SQL) and Transact-SQL (T-SQL)
Structured Query Language (SQL): Structured Query Language (SQL) has a specific design motive for defining, accessing and changement of data. It is considered as non-procedural, In that case the important elements and its results are first specified without taking care of the how they are computed. It is implemented over the database which is drive
2 min read
Configure SQL Jobs in SQL Server using T-SQL
In this article, we will learn how to configure SQL jobs in SQL Server using T-SQL. Also, we will discuss the parameters of SQL jobs in SQL Server using T-SQL in detail. Let's discuss it one by one. Introduction :SQL Server Agent is a component used for database task automation. For Example, If we need to perform index maintenance on Production ser
7 min read
Reverse Statement Word by Word in SQL server
To reverse any statement Word by Word in SQL server we could use the SUBSTRING function which allows us to extract and display the part of a string. Pre-requisite :SUBSTRING function Approach : Declared three variables (@Input, @Output, @Length) using the DECLARE statement. Use the WHILE Loop to iterate every character present in the @Input. For th
2 min read
Multi-Statement Table Valued Function in SQL Server
In SQL Server, a multi-statement table-valued function (TVF) is a user-defined function that returns a table of rows and columns. Unlike a scalar function, which returns a single value, a TVF can return multiple rows and columns. Multi-statement function is very much similar to inline functions only difference is that in multi-statement function we
4 min read
SQL Server MERGE Statement
MERGE is a powerful SQL statement that allows you to perform INSERT/UPDATE/DELETE operations depending on the conditions you specify. It makes it easy to synchronize data between source and destination tables. This makes it efficient to handle data changes in one single statement. MERGE makes it easier to manage complex operations such as data ware
6 min read
SQL | INSERT IGNORE Statement
We know that a primary key of a table cannot be duplicated. For instance, the roll number of a student in the student table must always be distinct. Similarly, the EmployeeID is expected to be unique in an employee table. When we try to insert a tuple into a table where the primary key is repeated, it results in an error. However, with the INSERT I
2 min read
SQL | DESCRIBE Statement
Prerequisite: SQL Create Clause As the name suggests, DESCRIBE is used to describe something. Since in a database, we have tables, that's why do we use DESCRIBE or DESC(both are the same) commands to describe the structure of a table. Syntax: DESCRIBE one; OR DESC one; Note: We can use either DESCRIBE or DESC(both are Case Insensitive). Suppose our
2 min read
SQL INSERT INTO SELECT Statement
In SQL, the INSERT INTO statement is used to add or insert records to the specified table. We can use this statement to add data directly to the table. We use the VALUES keyword along with the INSERT INTO statement. VALUES keyword is accompanied by column names in a specific order in which we want to insert values in them. SELECT statement is used
5 min read
SQL Server SELECT INTO Statement
SQL Server is a relational database management system. SQL Server offers robust security features to protect data integrity and confidentiality. It includes authentication, authorization, encryption, and various mechanisms to secure the database environment. It is designed to scale from small applications to large, enterprise-level databases. It pr
6 min read
What is Nested Select Statement in PL/SQL?
PL/SQL is a Procedural Language/Structured Query Language and it enables users to write procedural logic directly within the database, including procedures, functions, triggers, and packages. In this article, we will understand Nested select statements in PL/SQL along with the examples and so on. Understanding Nested Select Statement in PL/SQLThe n
4 min read
What is the CASE statement in SQL Server with or condition?
In SQL Server, the CASE statement cannot directly support the use of logical operators like OR with its structure. Instead of CASE it operates based on the evaluation of multiple conditions using the WHEN keyword followed by specific conditions. In this article, we will learn about the OR is not supported with CASE Statement in SQL Server with a de
4 min read
What is Nested Select Statement in SQL Server
SQL Server is a powerful relational database management system. Sql Server is very good with its robustness and scalability. SQL Server operates as a client-server structure, imparting centralized management and scalability for huge-scale applications and enterprise-stage solutions. It offers advanced features which include excessive availability,
3 min read
SQL CREATE VIEW Statement
The SQL CREATE VIEW statement is a very powerful feature in RDBMSs that allows users to create virtual tables based on the result set of a SQL query. Unlike regular tables, these views do not store data themselves rather they provide a way of dynamically retrieving and presenting data from one or many underlying tables. Views help simplify complex
4 min read
PL/SQL INSERT Statement
The PL/SQL INSERT statement is vital for adding new records to a database table. By specifying the table's name and providing values for its columns, users can populate their database with essential information. This functionality enables efficient data entry and ensures the completeness of datasets, facilitating various database operations and ana
3 min read
How to Combine LIKE and IN in SQL Statement
Combining 'LIKE' and 'IN' operators in SQL can greatly enhance users' ability to retrieve specific data from a database. By Learning how to use these operators together users can perform more complex queries and efficiently filter data based on multiple criteria, making queries more precise and targeted. This article explores how the LIKE and IN op
4 min read
SQL MERGE Statement
SQL MERGE Statement combines INSERT, DELETE, and UPDATE statements into one single query. MERGE Statement in SQLMERGE statement in SQL is used to perform insert, update, and delete operations on a target table based on the results of JOIN with a source table. This allows users to synchronize two tables by performing operations on one table based on
4 min read
SQL SELECT INTO Statement
The SQL SELECT INTO statement is used to copy data from one table into a new table. Note: The queries are executed in SQL Server, and they may not work in many online SQL editors so better use an offline editor. SyntaxSQL INSERT INTO Syntax is: SELECT column1, column2... INTO NEW_TABLE from SOURCE_TABLEWHERE Condition; To copy the entire data of th
3 min read
SQL DROP INDEX Statement
The SQL DROP INDEX statement removes an existing Index from a database table. SQL DROP INDEXThe SQL DROP INDEX Command is used to remove an index from the table. Indexes occupy space, which can cause extra time consumption on table modification operations. Benefits of Using DROP INDEX:Removing unused indexes can improve INSERT, UPDATE, and DELETE o
3 min read
DELETE Statement in MS SQL Server
The DELETE statement in MS SQL Server deletes specified records from the table. SyntaxMS SQL Server DELETE statement syntax is: DELETE FROM table_name WHERE condition; Note: Always use the DELETE statement with WHERE clause. The WHERE clause specifies which record(s) need to be deleted. If you exclude the WHERE clause, all records in the table will
1 min read
SQL Server MERGE Statement
SQL Server MERGE statement combines INSERT, UPDATE, and DELETE operations into a single transaction. MERGE in SQL ServerThe MERGE statement in SQL provides a convenient way to perform INSERT, UPDATE, and DELETE operations together, which helps handle the large running databases. But unlike INSERT, UPDATE, and DELETE statements MERGE statement requi
2 min read
SQL SELECT IN Statement
SQL SELECT IN Statement allows to specify multiple values in the WHERE clause. It is similar to using multiple OR conditions. It is particularly useful for filtering records based on a list of values or the results of a subquery. The IN operator compares a value with a set of values, and it returns a TRUE if the value belongs to that given set, els
3 min read
Article Tags :
three90RightbarBannerImg