SQL | UPDATE Statement - GeeksforGeeks (2024)

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.

Syntax

UPDATE table_name SET column1 = value1, column2 = value2,…

WHERE condition;

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

values of columns needs to be updated.

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.

Let’s see the SQL update statement with examples.

Query:

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;

Output:

SQL | UPDATE Statement - GeeksforGeeks (1)

Update Single Column

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

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

Output:

SQL | UPDATE Statement - GeeksforGeeks (2)

Updating Multiple Columns

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

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

Output:

SQL | UPDATE Statement - GeeksforGeeks (3)

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

Omitting WHERE Clause

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

UPDATE Customer SET CustomerName = 'Shubham';

Output:

The table Customer will now look like this,

SQL | UPDATE Statement - GeeksforGeeks (4)


Last Updated : 30 Oct, 2023

Like Article

Save Article

Share your thoughts in the comments

Please Login to comment...

SQL | UPDATE Statement - GeeksforGeeks (2024)

FAQs

What is SQL statement example? ›

An SQL SELECT statement retrieves records from a database table according to clauses (for example, FROM and WHERE ) that specify criteria. The syntax is: SELECT column1, column2 FROM table1, table2 WHERE column2='value';

What is a basic SQL statement? ›

A SQL statement is the most fundamental building block of SQL. A statement is a command to be understood by the interpreter and executed by the SQL engine.

What is %s in SQL statement? ›

Hey!! They're just placeholders for the values that follow in the command (eg. In db_query ) You must use %d for integer values and %s for string values.

What are the four basic SQL statements? ›

In Data Manipulation Language(DML), we have four different SQL statements, Select, Insert, Update, and Delete.

What is the most common SQL statement? ›

SELECT is probably the most commonly-used SQL statement. You'll use it pretty much every time you query data with SQL. It allows you to define what data you want your query to return. For example, in the code below, we're selecting a column called name from a table called customers .

What is single SQL statement? ›

Colloquially, it generally refers to a SELECT statement that is executed to return a result set. With this definition, subqueries are part of a single query.

What is the most basic SQL statement? ›

The most basic form of the SQL SELECT statement must be include SELECT, FROM clauses. In addition, if we want to filter the result set of the query, we should use the WHERE clause.

Is SQL hard to learn? ›

Learning SQL is easy

You can learn the basics of SQL in as little as a few weeks. SQL uses many terms that you are already familiar with—Select, Where, Delete, Update, etc. However, there are more complex SQL commands you'll need to learn to reach a mastery level.

How do I display a SQL statement? ›

During an edit or browse session, you can enter the SHOW SQL command to display a generalized form of the SQL that was used to fetch the rows from any displayed tables. By default, the SQL for the lowest-level table is displayed. The table name is shown in the window heading.

WHERE to write SQL code? ›

The best way to write SQL commands is in SQL Server Management Studio. Luckily, it's pretty easy, even if you're a beginner to writing SQL. We'll show you some basic commands, like the SELECT statement, INSERT statement, and more!

What does SELECT a * mean in SQL? ›

A query includes a list of columns to include in the final result, normally immediately following the SELECT keyword. An asterisk (" * ") can be used to specify that the query should return all columns of all the queried tables.

Top Articles
Latest Posts
Article information

Author: Rueben Jacobs

Last Updated:

Views: 6738

Rating: 4.7 / 5 (57 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Rueben Jacobs

Birthday: 1999-03-14

Address: 951 Caterina Walk, Schambergerside, CA 67667-0896

Phone: +6881806848632

Job: Internal Education Planner

Hobby: Candle making, Cabaret, Poi, Gambling, Rock climbing, Wood carving, Computer programming

Introduction: My name is Rueben Jacobs, I am a cooperative, beautiful, kind, comfortable, glamorous, open, magnificent person who loves writing and wants to share my knowledge and understanding with you.