Update your data

Applying SQL to Real-World Problems

Dmitriy (Dima) Gorenshteyn

Lead Data Scientist, Memorial Sloan Kettering Cancer Center

UPDATE syntax

UPDATE table_name
SET column1 = value1, column2 = value2, ...;
Applying SQL to Real-World Problems

UPDATE a column

Desired Update: Emails of customers must be lowercase.

UPDATE customer
SET email = LOWER(email);
Applying SQL to Real-World Problems

UPDATE & WHERE

Desired Update: Emails of customers must be lowercase for customers who are still active.

UPDATE customer
SET email = LOWER(email)
WHERE active = TRUE;
Applying SQL to Real-World Problems

UPDATE using subqueries

Desired Update: Emails of customers must be lowercase for customers reside in city of Woodridge.

UPDATE customer
SET email = LOWER(email)
WHERE address_id IN 
  (SELECT address_id 
   FROM address
   WHERE city = 'Woodridge');
Applying SQL to Real-World Problems

Be careful when modifying tables

  • Ensure you CAN modify the table.
  • Ensure you know how this table is used and how your changes will impact those who use it.
  • Test a modification by using a SELECT statement first.
Applying SQL to Real-World Problems

Let's UPDATE

Applying SQL to Real-World Problems

Preparing Video For Download...