×

Update Query in SQL

Update is an SQL command that is used to modify the data that in already present in database. Update is a command of DML. DML means Data Manipulation Language. Basically, the update command is used to update the data which is present in the table. We can update the single column as well as multiple columns.

In other word, we can say that it is basically used to change the data which is previously present in database. The update command can be returned in the folllowing format:

UPDATE table_name SET [column_name1= value1,... column_nameN = valueN] [WHERE condition]  

Syntax:

UPDATE table_name
SET col_name = Value
 WHERE Conditon;

With the help of this command, we can update the data inside the table. The UPDATE statement helps to let the database system know that we wish to update the database present in table in specific table name_parameter. The column we want to update are listed after the SET keyword and assigned with new updated values. We can separate the column with the help of commas. With the help of WHERE clause, we can mention the column or rows, we want to update.

Example:

UPDATE students  
SET User_Name = 'beinghuman'  
WHERE Student_Id = '3'  

Table:

Student_IdFirstNameLastNameUser_Name
1AdaSharmasharmili
2RahulMauryaso famous
3JamesWalkerjonny

Output:

Student_IdFirstNameLastNameUser_Name
1AdaSharmasharmili
2RahulMauryasofamous
3JamesWalkerbeinghuman

Updating Multiple Data

In update query, we can update multiple values, if we want to update multiple values, we need to separate field assignments with a comma.

SQL command, for multiple fields:

UPDATE students  
SET User_Name = 'beserious', First_Name = 'Johnny'  
WHERE Student_Id = '3'  

Output:

Student_IdFirst NameLast NameUser_Name
1AdaSharmasharmili
2RahulMauryasofamous
3JohnnyWalkerbe serious

Syntax of MySQL:

UPDATE table_name
SET field_1= new value 1, field_2= new value 2,
WHERE Clause

SQL UPDATE with SELECT Command

In SQL, we can update the specific value or row with the help of select statement. In other words, we can use select command to update records through UPDATE statement.

Syntax:

UPDATE table Destination
SET table Destination.col = value
WHERE EXISTS (
SELECT col2.value
FROM  tblSource
WHERE tblSource.join_col = tblDestination. Join_col
AND tblSource.Constraint = value)

Or you can try this one:

UPDATE   
Table   
SET  
Table.column1 = othertable.column 1,  
Table.column2 = othertable.column 2  
FROM   
Table  
INNER JOIN  
Other_table  
ON  
Table.id = other_table.id  

SQL UPDATE Column

In SQL, we can also update the column and we can update the single or multiple columns with the help of update statement.

Let’s take an Example, we have an employee named table as shown below:

Employee IDNameCitySalary
1TomDelhi25000
2HarryDelhi40000
3HannahBangalore50000
4SnehaMumbai20000
5ArunKolkata28000
6VanyaNULL35000

Before updating, first we need to check all the data types of all column because it is very important. Additionally, if any column has constraint then we need to update the values according to constraints.

We can check that with the help of DESC statement:

FieldTypeNull    Key Default
Employee IDintno primarynull
NameVarchar(255)no null
CityVarchar(255)yes null
Salaryintyes null

As you can see, our table has primary key constraint in employee ID column, so it is very important that every values of employee ID should be unique and not null. For example, if we change the address of Harry, we have to update the city attribute of our table.

UPDATE Employee
SET city='Chennai'
WHERE Employee ID =2;

After the execution, the query above will create the following table:

Output:

Employee IDNameCitySalary
1TomDelhi25000
2HarryChennai40000
3HannahBangalore50000
4SnehaMumbai20000
5ArunKolkata28000
6VanyaNULL35000

We can see that these records have been updated. We can also update the multiple records in single column

Example:

If we want to update the employee salary less than 30000 then we will use the following query:

UPDATE Employee
SET salary= 30000
WHERE salary<30000;

Output:

Employee IDNameCitySalary
1TomDelhi30000
2HarryChennai40000
3HannahBangalore50000
4SnehaMumbai30000
5ArunKolkata30000
6VanyaNULL35000

Here, we can see that these records have been updated.


Related Topics

How to use the BETWEEN operator in SQL

In this entire SQL article, we will understand and learn about the BETWEEN operator concept and how to use it in SQL. What is the BETWEEN operator in SQL? The Between operator...

4 minutes read.

SQL Operators

Arithmetic Operators Arithmetic operators are +, -, *, /, % performs addition, subtraction, multiplication, division, modulo respectively. Example:   Select 100+222; Select salary+100 From teacher Where teacher_id=1; Following output screen shows different arithmetic operations. We...

2 minutes read.

SQL Data Manipulation Language

Data Manipulation Language manipulates/make changes in data present in a table. It only affects data/records of table, not on the schema/structure of table. INSERT, UPDATE, DELETE are the commands of DML. INSERT: Stores...

2 minutes read.

What is SQL Injection?

Introduction to SQL Injection SQL injection is a vulnerability or a technique that might destroy the database of a website or a web application. It is one of the most widely...

4 minutes read.

SQL JOIN

As the name says, joins mean to merge something or to combine something. But in the case of SQL, joins mean to merge or combine two different tables. The Join clause...

12 minutes read.

Truncate function in SQL

The TRUNCATE is a numeric function in SQL which truncates the number according to the particular decimal points. Syntax of TRUNCATE Function SELECT TRUNCATE(X, D) AS Alias_Name; In the TRUNCATE syntax, X...

4 minutes read.

Grant Command in SQL

What is DCL (Data Control Language)? Data Control Language (DCL) is a computer programming language with syntax intended to manage access to data kept in databases. It is a part of...

3 minutes read.

How to Update Table in SQL?

How to Update Table in SQL Introduction UPDATE query is used to update a record in a table. UPDATE is a DML command, which operates on the data of the table and not...

4 minutes read.

Codd’s Rules in SQL

Codd’s Rules Dr. Edgar F. Codd, in 1985, laid down 13 fundamental rules after doing large-scale research on the Relational Model of databases. According to him, every database must follow these...

3 minutes read.

SQL Alter Table

In Structured Query Language, if you want to add columns in an existing table, then modify the table, or delete columns from the table. All these operations are allowed only...

7 minutes read.

SET Operators in SQL

The operator used to join or combine two queries is none other than SET operators. Operators categorized into SET operators are as follows: UNION Operator.UNION ALL’ Operator.INTERSECT Operator.MINUS Operator. Rules to be...

8 minutes read.

Types of SQL JOIN

The SQL JOIN combines one or more than one tables based on their relationship. The SQL JOIN involves a parent table and a child table relationship. There are different types of...

10 minutes read.

SQL Right Join

The SQL Right Join query displays all the table records and similar records from the left table. The query display zero records if it doesn’t find any similar records. If...

4 minutes read.

SQL NOT Operator

SQL NOT is a Boolean operator used with the WHERE clause. NOT operator shows the records if the expression is false. When we use the NOT operator, we fetch only...

7 minutes read.

How to drop a column in SQL?

How to drop a column in SQL Introduction To delete a column from an already created table, one needs to use the ALTER command along with the DROP COLUMN clause. Syntax: ALTER TABLE tablename...

4 minutes read.

Update Query in SQL

Update is an SQL command that is used to modify the data that in already present in database. Update is a command of DML. DML means Data Manipulation Language. Basically,...

3 minutes read.

SQL SELECT AND Operator

This SQL tutorial explains and helps us understand how to use the AND Operator in the SELECT query with examples. The AND Operator is used to fetch the table’s records if...

2 minutes read.

SQL FOREIGN KEY

In this article, we will learn about the FOREIGN KEY constraints and how to define a FOREIGN KEY constraint to build the relationship between two tables. In a Relational Databases Management...

4 minutes read.

SQL Count

Structured Query Language Count() Function is used with Structured Query Language SELECT Statement. SQL Count() function returns the number of items that match the specified criteria in the SELECT statement. Count()...

2 minutes read.

SQL Truncate

This command deletes all records from table. Truncate is a DDL command. Syntax: TRUNCATE table table_name; Example: Truncate table teacher; ORDER BY The ORDER BY clause arranges the table or column in ascending...

5 minutes read.