Beginners Guide to SQL – SQL UPDATE Statement

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

UPDATE table_name SET column1_name = value1, column2_name = value2,... WHERE condition;

Here, column1_namecolumn2_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

 

Personal Career & Learning Guide for Data Analyst, Data Engineer and Data Scientist

Applied Machine Learning & Data Science Projects and Coding Recipes for Beginners

A list of FREE programming examples together with eTutorials & eBooks @ SETScholars

95% Discount on “Projects & Recipes, tutorials, ebooks”

Projects and Coding Recipes, eTutorials and eBooks: The best All-in-One resources for Data Analyst, Data Scientist, Machine Learning Engineer and Software Developer

Topics included: Classification, Clustering, Regression, Forecasting, Algorithms, Data Structures, Data Analytics & Data Science, Deep Learning, Machine Learning, Programming Languages and Software Tools & Packages.
(Discount is valid for limited time only)

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

Please do not waste your valuable time by watching videos, rather use end-to-end (Python and R) recipes from Professional Data Scientists to practice coding, and land the most demandable jobs in the fields of Predictive analytics & AI (Machine Learning and Data Science).

The objective is to guide the developers & analysts to “Learn how to Code” for Applied AI using end-to-end coding solutions, and unlock the world of opportunities!