SQL
  • Tips
  • SQL
    • Database Basics
    • SQL Basics
    • SQL Syntax
    • Retrieve Data: SELECT
    • Sort Data: ORDER BY
    • Filter Data: WHERE
    • Calculated Fields
    • Aggregate Functions
    • Group Data: GROUP BY
    • Subqueries
    • Join Tables
    • Combine Queries: UNION
    • Control Flow Statements
    • IF function
    • Handle NULL
    • Date
    • Numeric
    • String
    • Notes
  • Table/Database
    • Insert
    • Delete
    • Update
    • Table
    • Database
    • Stored Procedure
  • Misc
    • SQL vs NoSQL
    • 大数据
    • Why SQL instead of Excel + VBA?
  • sqlzoo
    • world table
    • nobel table
    • football data
    • movie data
    • Teacher Department Data
    • Edinburgh Buses data
  • Leetcode
    • 175_Combine Two Tables
    • 176_Second Highest Salary
    • 177_Nth Highest Salary
    • 178_Rank Scores
    • 180_Consecutive Numbers
    • 181_Employees Earning More Than Their Managers
    • 182_Duplicate Emails
    • 183_Customers Who Never Order
    • 184_Department Highest Salary
    • 185_Department Top Three Salaries
    • 196_Delete Duplicate Emails
    • 197_Rising Temperature
    • 570_Managers with at Least 5 Direct Reports
    • 578_Get Highest Answer Rate Question
    • 579_Find Cumulative Salary of an Employee
    • 584_Find Customer Referee
    • 586_Customer Placing the Largest Number of Orders
    • 595_Big Countries
    • 596_Classes More Than 5 Students
    • 597_Friend Requests I: Overall Acceptance Rate
    • 601_Human Traffic of Stadium
    • 602_Friend Requests II: Who Has the Most Friends
    • 603_Consecutive Available Seats
    • 607_Sales Person
    • 608_Tree Node
    • 610_Triangle Judgement
    • 612_Shortest Distance in a Plane
    • 613_Shortest Distance in a Line
    • 619_Biggest Single Number
    • 620_Not Boring Movies
    • 626_Exchange Seats
    • 627_Swap Salary
  • Facebook 面经题
    • spam filter
    • marketplace
    • instagram
    • session
    • message confirmation
Powered by GitBook
On this page
  • Update Single Column
  • Update Multiple Columns
  • Update Under Different Conditions
  • Cross-table Update: UPDATE JOIN

Was this helpful?

  1. Table/Database

Update

To update (modify) data in a table the UPDATE statement is used. UPDATE can be used in two ways:

  1. To update specific rows in a table

  2. To update all rows in a table

The basic format of an UPDATE statement is made up of three parts:

  1. The table to be updated

  2. The column names and their new values

  3. The filter condition that determines which rows should be updated

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

Update Single Column

Assume customer 1000000005 has no e-mail address on file and now has an address, and so that record needs updating. The following statement performs this update:

UPDATE Customers
SET cust_email = "kim@thetoystore.com"
WHERE cust_id = "1000000005";

If you don't use WHERE clause, all the rows will be updated.

Update Multiple Columns

UPDATE Customers
SET cust_contact = 'Sam Roberts', cust_email = 'sam@toyland.com'
WHERE cust_id = '1000000006';

Update Under Different Conditions

UPDATE salary
SET sex = (CASE sex WHEN 'f' THEN 'm' WHEN 'm' THEN 'f' END);

Cross-table Update: UPDATE JOIN

If we need information in another table when we update values, we need to JOIN the tables.

UPDATE T1, T2,
[INNER JOIN | LEFT JOIN] T1 ON T1.C1 = T2. C1
SET T1.C2 = T2.C2, 
    T2.C3 = expr
WHERE condition

In the following code, we want to update salary in employees table and we need percentage in merits table.

UPDATE employees
    INNER JOIN merits 
    ON employees.performance = merits.performance 
SET 
    salary = salary + salary * percentage;

If join three tables:

UPDATE TABLE_A a 
    JOIN TABLE_B b ON a.join_col = b.join_col AND a.column_a = b.column_b 
    JOIN TABLE_C c ON [condition]
SET a.column_c = a.column_c + 1
PreviousDeleteNextTable

Last updated 5 years ago

Was this helpful?