Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

UPDATE statement in MySQL | UPDATE JOIN in MySQL

Ujjwal Abhishek
Ujjwal Abhishek

UPDATE 

The UPDATE statement is used to update or modify data in a table. It is used to change the values in one or more columns of a single row or multiple rows.

Syntax:

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

In the above syntax:

  • table_name is the name of the table that will be updated,
  • name of the columns that we want to modify is written after the SET clause,
  • condition is written after the WHERE clause to specify the rows that we want to modify.
  • The condition can be omitted but in that case, all the rows will be modified.

1. Using UPDATE statement to update values in a single column

customers   
customer_idnamecitymobile
1AnujPune7643323XXX
2AvinashBangalore8234134XXX
3VikashHyderabad9974636XXX

The above table contains the details of the customers.

UPDATE customers
SET city='Mumbai'
WHERE customer_id=1;

The modified table after the execution of the above query will be:

customers   
customer_idnamecitymobile
1AnujMumbai7643323XXX
2AvinashBangalore8234134XXX
3VikashHyderabad9974636XXX

2. Using UPDATE statement to update values in multiple columns

The UPDATE statement can be used to modify or update multiple columns at a time.

For example:

UPDATE customers
SET city='Mumbai',mobile='8342552XXX'
WHERE customer_id=1;

The above query will update records of two columns of the row having customer_id=1. The modified table after the execution of the above query will be:

customers   
customer_idnamecitymobile
1AnujMumbai8342552XXX
2AvinashBangalore8234134XXX
3VikashHyderabad9974636XXX

UPDATE with JOIN 

Joins are often used to query rows from a table that have or may not have matching rows in another table. UPDATE with JOIN clause is used in MySQL to update data in one table using another table and Join condition.

Syntax:

UPDATE table1, table2
[INNER JOIN/ LEFT JOIN] table1 ON table1.column1=table2.column2
SET table1.column2=table2.column2
	table2.column3=expression
WHERE condition

In the above syntax:

  • table1 is the main table and table 2 is another table that will be joined with table1. We can use only one table after the update clause if we want to update the data of that table only.
  • After that, the type of JOIN is specified,
  • then new values are assigned to the columns of the tables whichever we want to update.
  • After that, the condition is associated with the WHERE clause to limit the rows in which we want to update the data.

For example:

Suppose there are two tables ‘customers’ and ‘purchases’. ‘customers’ have the details of the customers and their bill_amount and purchases have the details of the percentage of discounts offered on a particular amount of purchase.

customers  
customer_idbill_amountdiscount_offered
120000
240000
350000

 

purchases 
bill_amountpercentage
20005
400010
500015

The above table contains the purchase details.

UPDATE customers
INNER JOIN
purchases ON customers.bill_amount=purchase.bill_amount
SET 
discount_Offered = purchase.percentage/100*customers.bill_amount;

The above-written query takes the percentage of discount from the purchase table by matching the bill_amount from the customers and Purchase tables and updates the discount_offered column in the ‘customers’ table.

The updated ‘customers’ table after the execution of the above query will be:

customers  
customer_idbill_amountdiscount_offered
12000100
24000400
35000750
Ujjwal Abhishek
Ujjwal Abhishek
Ujjwal is final-year CSE Undergraduate, a competitive coder, and a web developer passionate about problem-solving and data structures and algorithms.
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet