(SQL Example for Citizen Data Scientist & Business Analyst)
SQL | UPDATE Statement
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.
Basic 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.
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.
Example Queries
-
- 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;
Output:
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 single column: Update the column NAME and set the value to ‘PRATIK’ in all the rows where Age is 20.
-
- 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;
Output:
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.
- Updating multiple columns: Update the columns NAME to ‘PRATIK’ and ADDRESS to ‘SIKKIM’ where ROLL_NO is 1.
- 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';
Output:
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
Learn to Code SQL Example – SQL | UPDATE Statement
Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.
Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners
Latest end-to-end Learn by Coding Projects (Jupyter Notebooks) in Python and R:
All Notebooks in One Bundle: Data Science Recipes and Examples in Python & R.
End-to-End Python Machine Learning Recipes & Examples.
End-to-End R Machine Learning Recipes & Examples.
Applied Statistics with R for Beginners and Business Professionals
Data Science and Machine Learning Projects in Python: Tabular Data Analytics
Data Science and Machine Learning Projects in R: Tabular Data Analytics
Python Machine Learning & Data Science Recipes: Learn by Coding
R Machine Learning & Data Science Recipes: Learn by Coding
Comparing Different Machine Learning Algorithms in Python for Classification (FREE)
There are 2000+ End-to-End Python & R Notebooks are available to build Professional Portfolio as a Data Scientist and/or Machine Learning Specialist. All Notebooks are only $29.95. We would like to request you to have a look at the website for FREE the end-to-end notebooks, and then decide whether you would like to purchase or not.