SQL UPDATE: How to Use UPDATE Statements in SQL?

Unlock the power of SQL UPDATE statements! Check out this recipe on using UPDATE commands to seamlessly manage your database content. | ProjectPro

Recipe Title - SQL UPDATE: How to Use UPDATE Statements in SQL? 

If you're diving into the world of SQL, mastering the UPDATE statement is essential for maintaining the accuracy and integrity of your database. The SQL UPDATE statement allows you to modify existing records in a table, ensuring that your data stays current and relevant. Check out this recipe to explore the intricacies of the SQL UPDATE statement, covering topics such as basic syntax, updating multiple columns, and using JOIN clauses for more complex updates.

Basic Syntax of SQL UPDATE Statement 

The fundamental structure of an SQL UPDATE statement is as follows:

Syntax:
UPDATE table_name

SET column1 = value1, column2 = value2, …

WHERE condition;

You must be very careful while using the UPDATE statement because if you don’t specify the WHERE clause you will end up updating all the rows of the mentioned column.

SQL Project for Data Analysis using Oracle Database-Part 4 

Examples of Basic SQL UPDATE Statements

SQL UPDATE Column: Let us add an income column to the customers table first.

Code: 

-- adding new column income

ALTER TABLE customers ADD income INTEGER;

-- updating the column values for pre-existing entries
UPDATE customers set income = 90000000 where customer_id=101;
UPDATE customers set income = 10000000 where customer_id=102;
UPDATE customers set income = 78000000 where customer_id=103;
UPDATE customers set income = 56000000 where customer_id=104;
UPDATE customers set income = 78000000 where customer_id=105;

-- viewing the data
SELECT * FROM customers;

SQL Update Columns

Output:

+-------------+----------------+------+------------+---------+----------+

| customer_id | customer_name  | age  | city       | country | income   |

+-------------+----------------+------+------------+---------+----------+

|         101 | Thomas Shelby  |   30 | Birmingham | England | 90000000 |

|         102 | Grace Burgess  |   28 | Dublin     | Ireland | 10000000 |

|         103 | Alfie Solomons |   40 | London     | England | 78000000 |

|         104 | Michael Gray   |   22 | New York   | USA     | 56000000 |

|         105 | May Carleton   |   29 | Sheffield  | England | 78000000 |

+-------------+----------------+------+------------+---------+----------+

5 rows in set (0.00 sec)

SQL Update Multiple Columns: You can also update multiple columns as follows:

Code:

-- updating city column

UPDATE customers SET city = "Manchester" WHERE country = "England";

-- displaying the changes
SELECT * FROM customers;

SQL Update Multiple rows

Output:

+-------------+----------------+------+------------+---------+----------+

| customer_id | customer_name  | age  | city       | country | income   |

+-------------+----------------+------+------------+---------+----------+

|         101 | Thomas Shelby  |   30 | Manchester | England | 90000000 |

|         102 | Grace Burgess  |   28 | Dublin     | Ireland | 10000000 |

|         103 | Alfie Solomons |   40 | Manchester | England | 78000000 |

|         104 | Michael Gray   |   22 | New York   | USA     | 56000000 |

|         105 | May Carleton   |   29 | Manchester | England | 78000000 |

+-------------+----------------+------+------------+---------+----------+

5 rows in set (0.00 sec)

Taking it a Step Further: SQL UPDATE with JOIN

In scenarios where you need to update records in one table based on values from another, the SQL UPDATE with JOIN comes into play. This is particularly useful for complex updates involving multiple tables.

Example of SQL Server UPDATE with JOIN

UPDATE orders

SET orders.status = 'Shipped'

FROM orders

JOIN customers ON orders.customer_id = customers.customer_id

WHERE customers.country = 'USA';

Here, the orders table is updated to set the status to 'Shipped' for all orders placed by customers in the USA.

SQL UPDATE from SELECT: Efficient Updates with Subqueries

Another powerful feature is updating data based on the results of a SELECT statement. This is achieved using the SQL UPDATE from SELECT syntax.

Example of SQL UPDATE from SELECT

UPDATE products

SET stock_quantity = stock_quantity - 10

FROM products

WHERE category = 'Electronics';

Here, the stock quantity of products in the 'Electronics' category is decreased by 10.

GCP Data Ingestion with SQL using Google Cloud Dataflow

SQL Server UPDATE: Tailoring to Your Needs

SQL Server provides additional functionalities for the UPDATE statement. For instance, you can use the OUTPUT clause to capture the results of the update operation.

Example of SQL Server UPDATE with OUTPUT

UPDATE employees

SET salary = salary * 1.1

OUTPUT INSERTED.employee_id, INSERTED.salary

WHERE department_id = 102;

This not only updates the salary but also outputs the employee ID and the updated salary.

Learn more about SQL UPDATE Statements with ProjectPro! 

Mastering the SQL UPDATE statement is crucial for maintaining a well-functioning and up-to-date database. Whether you're making simple changes to a single column or executing complex updates across multiple tables, understanding the nuances of SQL UPDATE will empower you to manage your data effectively. Start experimenting with the provided real world practical projects to enhance your SQL skills and unlock the full potential of database management. Check out ProjectPro to gain access to 270+ projects on data analysis, data science and big data. 

What Users are saying..

profile image

Jingwei Li

Graduate Research assistance at Stony Brook University
linkedin profile url

ProjectPro is an awesome platform that helps me learn much hands-on industrial experience with a step-by-step walkthrough of projects. There are two primary paths to learn: Data Science and Big Data.... Read More

Relevant Projects

Tensorflow Transfer Learning Model for Image Classification
Image Classification Project - Build an Image Classification Model on a Dataset of T-Shirt Images for Binary Classification

Learn Object Tracking (SOT, MOT) using OpenCV and Python
Get Started with Object Tracking using OpenCV and Python - Learn to implement Multiple Instance Learning Tracker (MIL) algorithm, Generic Object Tracking Using Regression Networks Tracker (GOTURN) algorithm, Kernelized Correlation Filters Tracker (KCF) algorithm, Tracking, Learning, Detection Tracker (TLD) algorithm for single and multiple object tracking from various video clips.

A/B Testing Approach for Comparing Performance of ML Models
The objective of this project is to compare the performance of BERT and DistilBERT models for building an efficient Question and Answering system. Using A/B testing approach, we explore the effectiveness and efficiency of both models and determine which one is better suited for Q&A tasks.

Digit Recognition using CNN for MNIST Dataset in Python
In this deep learning project, you will build a convolutional neural network using MNIST dataset for handwritten digit recognition.

Build Deep Autoencoders Model for Anomaly Detection in Python
In this deep learning project , you will build and deploy a deep autoencoders model using Flask.

PyTorch Project to Build a GAN Model on MNIST Dataset
In this deep learning project, you will learn how to build a GAN Model on MNIST Dataset for generating new images of handwritten digits.

Langchain Project for Customer Support App in Python
In this LLM Project, you will learn how to enhance customer support interactions through Large Language Models (LLMs), enabling intelligent, context-aware responses. This Langchain project aims to seamlessly integrate LLM technology with databases, PDF knowledge bases, and audio processing agents to create a comprehensive customer support application.

Build Customer Propensity to Purchase Model in Python
In this machine learning project, you will learn to build a machine learning model to estimate customer propensity to purchase.

Build CNN Image Classification Models for Real Time Prediction
Image Classification Project to build a CNN model in Python that can classify images into social security cards, driving licenses, and other key identity information.

Build a Multi Class Image Classification Model Python using CNN
This project explains How to build a Sequential Model that can perform Multi Class Image Classification in Python using CNN