The UPDATE statement in SQL is used to update the data of an existing table in database. We can update single columns as well as multiple columns using UPDATE statement as per our requirement.
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.
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.
- Updating single column: Update the column NAME and set the value to ‘PRATIK’ in all the rows where Age is 20.
UPDATE Student SET NAME = 'PRATIK' WHERE Age = 20;
This query will update two rows(third row and fifth row) and the table Student will now look like,
ROLL_NO NAME ADDRESS PHONE Age 1 Ram Delhi XXXXXXXXXX 18 2 RAMESH GURGAON XXXXXXXXXX 18 3 PRATIK ROHTAK XXXXXXXXXX 20 4 SURESH Delhi XXXXXXXXXX 18 3 PRATIK ROHTAK XXXXXXXXXX 20 2 RAMESH GURGAON XXXXXXXXXX 18
- Updating multiple columns: Update the columns NAME to ‘PRATIK’ and ADDRESS to ‘SIKKIM’ where ROLL_NO is 1.
UPDATE Student SET NAME = 'PRATIK', ADDRESS = 'SIKKIM' WHERE ROLL_NO = 1;
The above query will update two columns in the first row and the table Student will now look like,
ROLL_NO NAME ADDRESS PHONE Age 1 PRATIK SIKKIM XXXXXXXXXX 18 2 RAMESH GURGAON XXXXXXXXXX 18 3 PRATIK ROHTAK XXXXXXXXXX 20 4 SURESH Delhi XXXXXXXXXX 18 3 PRATIK ROHTAK XXXXXXXXXX 20 2 RAMESH GURGAON XXXXXXXXXX 18
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 Student SET NAME = 'PRATIK';
The table Student will now look like,
ROLL_NO NAME ADDRESS PHONE Age 1 PRATIK Delhi XXXXXXXXXX 18 2 PRATIK GURGAON XXXXXXXXXX 18 3 PRATIK ROHTAK XXXXXXXXXX 20 4 PRATIK Delhi XXXXXXXXXX 18 3 PRATIK ROHTAK XXXXXXXXXX 20 2 PRATIK GURGAON XXXXXXXXXX 18
This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.
- Difference between Deferred update and Immediate update
- Difference between Structured Query Language (SQL) and Transact-SQL (T-SQL)
- Performing Database Operations in Java | SQL CREATE, INSERT, UPDATE, DELETE and SELECT
- Difference between ALTER and UPDATE Command in SQL
- SQL | UPDATE with JOIN
- Update Action in MS SQL Server
- SQL | INSERT INTO Statement
- SQL | DELETE Statement
- SQL | INSERT IGNORE Statement
- SQL | Case Statement
- SQL | DESCRIBE Statement
- SQL | MERGE Statement
- MERGE Statement in SQL Explained
- Delete statement in MS SQL Server
- Reverse Statement Word by Word in SQL server
- SELECT INTO statement in SQL
- Select statement in MS SQL Server
- Insert statement in MS SQL Server
- Insert Into Select statement in MS SQL Server
- CREATE and DROP INDEX Statement in SQL