×

SQL UPDATE

SQL UPDATE

The SQL UPDATE statement is utilized to update and modify the records present into a database. It is used to change the already existing records stored in the tables in the database. This command is worked along with WHERE clause. The condition specified in the UPDATE statement is used to decide which columns or rows of the table will be affected or modified.

The UPDATE command can also be used to update a table by another table. It can be used to update the date and time of a SQL query as well.

SQL UPDATE JOIN

The SQL UPDATE statement can also be utilized to update one table using another table which are connected by a join. This is known as the SQL UPDATE JOIN statement.

UPDATE table1, table2,
 INNER JOIN table1
 ON table1.column1 = table2.column1 
 SET table1.column1 = table2.column2
 WHERE condition

The following is the basic syntax of the UPDATE statement using the JOIN clause:

Example:

CREATE TABLE TAE1 (Col1 INT, Col2 INT, Col3 VARCHAR (100))  
 INSERT INTO TAE1 (Col1, Col2, Col3)  
 SELECT 1, 11, 'FIRST'  
 UNION ALL  
 SELECT 11,12, 'SECOND'  
 UNION ALL   
 SELECT 21, 13, 'THIRD'  
 UNION ALL   
 SELECT 31, 14, 'FOURTH'   

Let’s first create the first table TAE1.

 CREATE TABLE TAE2 (Col1 INT, Col2 INT, Col3 VARCHAR (100))  
 INSERT INTO TAE2 (Col1, Col2, Col3)  
 SELECT 1, 21, 'TWO-ONE'  
 UNION ALL  
 SELECT 11,22, 'TWO-TWO'  
 UNION ALL   
 SELECT 21, 23, 'TWO-THREE'  
 UNION ALL   
 SELECT 31, 24, 'TWO-FOUR'   

Now, let’s create the second table TAE2.

SELECT *
 FROM TAE1 
Col1Col2Col3
111First
1112Second
2113Third
3114Fourth

Output:

Now let’s see the contents of the table TAE2.

SELECT *
 FROM TAE2

Output:

Col1Col2Col3
121Two-one
1122Two-two
2123Two-three
3124Two-four

Now the following query will update the rows of TAE1 where the value of Col1 is 21 and 31 using the table TAE2 where there are similar rows and where Col1 is 21 and 31. Only the corresponding records of Col2 and Col3 of TAE1 table will be updated.

UPDATE TAE1
 SET Col2 = TAE2.Col2,  
 Col3 = TAE2.Col3  
 FROM TAE1  
 INNER JOIN TAE2 ON TAE1.Col1 = TAE2.Col1  
 WHERE TAE1.Col1 IN (21, 31); 

Output:

Col1Col2Col3
111First
1112Second
2113Two-three
3114Two-four

Now, if the contents of the table TAE1 is checked, the following output will be obtained.

However, the contents of TAE2 remains unchanged.

Col1Col2Col3
121Two-one
1122Two-two
2123Two-three
3124Two-four

This is an example of using the JOIN clause with the UPDATE statement. It has merged the above two tables.

SQL UPDATE DATE

The SQL UPDATE DATE statement is used to update the date and time field in SQL.

UPDATE table_name
 SET data_field = ‘data_value’ 
 WHERE conditions; 

The following is the general syntax of updating the date and time field in SQL:

UPDATE table_name
 SET data_field = getdate(); 

The following is the syntax of updating the date with the current date in SQL:

UPDATE table_name
 SET data_field = CURRENT_TIMESTAMP; 

The following is the syntax of updating the date and time with the current date and time in SQL:

 UPDATE table_name
 SET data_field = ‘YYYY-MM-DD HH:MM:SS’;

The following is the syntax of updating the date and time with a specific date and time in SQL:

UPDATE table_name
 SET data_field = CAST(‘date_value’ AS DATETIME); 

The following is the syntax of updating the date with a specific value when the format of the date is not known:

Example:

UPDATE Employee
 SET DOJ = ‘2021-07-05’ 
 WHERE Dept_ID = 10; 

The following is an example of updating the date in multiple rows in the given Employee table.

Emp_IDEmp_NameDesignationManager_IDDOJSalaryDept_ID
1Emp1Director 2021-07-114500010
2Emp2Director 2021-07-114000020
3Emp3ManagerEmp12021-07-112700010
4Emp4ManagerEmp22021-10-082500020
5Emp5AnalystEmp32021-07-112000010
6Emp6AnalystEmp32021-10-081800010
7Emp7ClerkEmp32021-07-111500010
8Emp8SalesmanEmp42021-09-091400020
9Emp9SalesmanEmp42021-10-081300020

Query:

Output:

Emp_IDEmp_NameDesignationManager_IDDOJSalaryDept_ID
1Emp1Director 2021-07-054500010
3Emp3ManagerEmp12021-07-052700010
5Emp5AnalystEmp32021-07-052000010
6Emp6AnalystEmp32021-07-051800010
7Emp7ClerkEmp32021-07-051500010

Thus, the DOJ column for all the employees having Dept_ID 10 has been updated.

The following is the example of updating the admission date in a student table using the CAST function:

UPDATE student
SET admission_date = CAST(‘2021-04-10’ AS DATETIME)
WHERE id = 42;

Related Topics

Where vs Having

Difference Between Where and Having The WHERE and HAVING clauses in SQL are used to filter records stored in tables in the databases. The dissimilarities between the WHERE and HAVING clause...

3 minutes read.

How to compare date in SQL

In this section, we will learn about how dates can be compared in SQL. We can compare any random date with another date stored in a column of a table.This comparison...

4 minutes read.

Introduction to SQL

SQL Introduction SQL is Standard Query Language. This language is used to communicate or interact with database. In other words, SQL is used to access and manage data or information...

2 minutes read.

Pattern Matching in SQL

The LIKE in the Structured Query Language is a Logical Operator. The SQL LIKE is used within the WHERE clause in the SQL. This Like Operator is used with the...

5 minutes read.

SQL SELECT Database

In this tutorial, we will help you to understand and learn how to select the database in SQL with the help of examples. The database user or the administrator wants to...

3 minutes read.

SQL UNION ALL Operator

This page has all the information about UNION ALL operator, which is used to join all the values from the SELECT queries. Similar records were not considered in the result in...

3 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.

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.

SQL Handling Duplicate

Removing Duplicates using DISTINCT Keyword: By using the DISTINCT keyword in SQL we can remove duplicate characters from tables or databases. A table contains more duplicate values and duplicate values can cause...

4 minutes read.

SQL Create Database

This tutorial will help us understand how to CREATE a Database in SQL with a few examples. The first step for storing structured records in the database is creating the databases. We...

4 minutes read.

SQL TABLE

SQL TABLE Structured Query Language (SQL) is a relational database (RDBMS) where data is stored in the form of tables, that is, in rows and columns. These tables are known as...

3 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 SELECT MIN

The SQL Min() function is an aggregate function in SQL. SQL Min() returns the minimum value of a given condition. The expression may be numerical, or it may be an expression. The syntax...

5 minutes read.

SQL CREATE TABLE

In SQL tutorial, we learned and created different databases. To stores data in databases, we need to create a table. To create the table, we need to use CREATE TABLE...

5 minutes read.

SQL Formatter

As a beginner, one does not focus much on Formatting the queries while writing their code. This leads to a great confusion while referring the code in future. For a...

5 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 SubQuery

The Sub-query in the SQL is the inner query placed or positioned inside another query, which is also known is the outer query. The inner query is embedded in the...

6 minutes read.

WEB SQL

What is Web SQL? In SQL we can create databases, read the data in the databases, insert the records into the databases, and delete the records from the databases. For storing...

4 minutes read.

SQL SET Operator

In this tutorial, we will understand the operator who falls under the SET operator in SQL with the help of different examples. The SQL SET Operator is used to merge the...

5 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.