SQL UPDATE QUERY - The Coding Shala

Home >> Learn SQL >> SQL UPDATE Query

SQL UPDATE QUERY 

SQL 'UPDATE' Query is used to modify the existing record in the table. We generally use 'UPDATE' with the 'WHERE' to specify the certain row, if we miss the where condition then all the rows would be affected.
SQL UPDATE QUERY - The Coding Shala

SQL UPDATE Query Syntax 

The basic SQL UPDATE query syntax is as follows - 

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

SQL UPDATE Query Examples 

The following table 'Persons' contains - 
 PersonID     LastName     FirstName     Address     City  
 1          Saini         Akshay         A-101     Pune  
 1          last          first         A-101     Pune  
 2          null           Akshay         null     null  

The following query will update 'PersonID' = 2 if  'LastName' is "last" - 

 SQL >> update Persons  
        set PersonID = 2   
        where LastName = "last"  
 
Output >>  
  PersonID     LastName     FirstName     Address     City  
 1           Saini           Akshay        A-101     Pune  
 2           last           first           A-101     Pune  
 2           null           Akshay           null     null  
  • Like this, we can update more than one columns - 

SQL >> update Persons  
        set PersonID = 3, FirstName = "Nikhil"  
        where LastName is null  
 
Output >>  
  PersonID     LastName     FirstName     Address     City  
 1           Saini           Akshay        A-101     Pune  
 2           last           first           A-101     Pune  
 3           null           Nikhil      null     null  



Other Posts You May Like
Prev<< SQL SELECT Query                                                                        NEXT >>SQL Delete Query
Please leave a comment below if you like this post or found some error, it will help me to improve my content.

Comments

Popular Posts from this Blog

Anti Diagonals - The Coding Shala

Shell Script to find sum, product and average of given numbers - The Coding Shala

LeetCode - Bulb Switcher Solution - The Coding Shala

New Year Chaos Solution - The Coding Shala

Sorting the Sentence LeetCode Solution - The Coding Shala