(SQL Tutorials for Citizen Data Scientist)
SQL UPDATE Statement
In this tutorial you will learn how to update records in a database table using SQL.
Updating Table Data
In the previous chapters we’ve learnt how to insert data as well as how to select data from a database table based on various conditions. In this tutorial we’ll learn to perform one more important task which is updating the existing records in a database table.
Syntax
The UPDATE
statement is used to update existing data in a table.
UPDATEtable_name
SETcolumn1_name
= value1,column2_name
= value2,... WHEREcondition
;
Here, column1_name, column2_name,… are the names of the columns or fields of a database table whose values you want to update. You can also combine multiple conditions using the AND
or OR
operators, that you’ve learned in the previous chapters.
Warning: The WHERE
clause in the UPDATE
statement specifies which record or records should be updated. If you omit the WHERE
clause, all the records will be updated.
Let’s check out some examples that demonstrate how it actually works.
Suppose we’ve an employees table in our database that has following records:
+--------+--------------+------------+--------+---------+ | emp_id | emp_name | hire_date | salary | dept_id | +--------+--------------+------------+--------+---------+ | 1 | Ethan Hunt | 2001-05-01 | 5000 | 1 | | 2 | Tony Montana | 2002-07-15 | 6500 | 5 | | 3 | Sarah Connor | 2005-10-18 | 8000 | 3 | | 4 | Rick Deckard | 2007-01-03 | 7200 | 4 | | 5 | Martin Blank | 2008-06-24 | 5600 | NULL | +--------+--------------+------------+--------+---------+
Updating a Single Column
The following SQL statement will update the emp_name field of the employees table and set a new value where the employee id i.e. emp_id is equal to 3.
Example
UPDATE employees SET emp_name = 'Sarah Ann Connor'
WHERE emp_id = 3;
After execution, the resulting table will look something like this:
+--------+------------------+------------+--------+---------+ | emp_id | emp_name | hire_date | salary | dept_id | +--------+------------------+------------+--------+---------+ | 1 | Ethan Hunt | 2001-05-01 | 5000 | 1 | | 2 | Tony Montana | 2002-07-15 | 6500 | 5 | | 3 | Sarah Ann Connor | 2005-10-18 | 8000 | 3 | | 4 | Rick Deckard | 2007-01-03 | 7200 | 4 | | 5 | Martin Blank | 2008-06-24 | 5600 | NULL | +--------+------------------+------------+--------+---------+
Updating Multiple Columns
Similarly, you can update multiple columns using a list of comma-separated column name and value pair. The following example will update the salary and dept_id field of an existing employee in the employees table whose emp_id is 5.
Example
UPDATE employees
SET salary = 6000, dept_id = 2
WHERE emp_id = 5;
After execution, the resulting table will look something like this:
+--------+------------------+------------+--------+---------+ | emp_id | emp_name | hire_date | salary | dept_id | +--------+------------------+------------+--------+---------+ | 1 | Ethan Hunt | 2001-05-01 | 5000 | 1 | | 2 | Tony Montana | 2002-07-15 | 6500 | 5 | | 3 | Sarah Ann Connor | 2005-10-18 | 8000 | 3 | | 4 | Rick Deckard | 2007-01-03 | 7200 | 4 | | 5 | Martin Blank | 2008-06-24 | 6000 | 2 | +--------+------------------+------------+--------+---------+
Beginners Guide to SQL – 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.