BBampaa

Lessons › SQL › Deep Dive: Database Admin & ETL

SQL lesson

UPDATE SET for correcting values in place

UPDATE SET overwrites existing values directly inside a table - no need to delete and re-add a row.

Try this lesson free →
About 5–10 minutes · no sign-up needed

Example

Correcting Retail Product Values with UPDATE SET

UPDATE products
SET category = 'Electronics'
WHERE product_id = 'P2001'

Result:

product_idbrandcategorypricestatus
P2001NovaTechElectronics199.99active
P2002GlowUpBeauty34.5activee
P2003FitGearSprots89.0active
P2004HomeNestHome149.95inactive
P2005NovaTechElectronics0.0active

Step by step

  1. Start with UPDATE and the table whose rows you want to change.
    UPDATE products;
  2. SET names the column and the value you want it to become. Without WHERE this updates every row.
    UPDATE products
    SET category = 'Electronics';
  3. WHERE limits the update to rows that match your condition - usually what you want.
    UPDATE products
    SET category = 'Electronics'
    WHERE product_id = 'P2001';

Practice

Try each one in your head first, then open the answer.

1. Write an UPDATE statement on the products table to set category to 'updated' where product_id equals NULL.
UPDATE products SET category = 'updated' WHERE product_id = NULL
2. Write an UPDATE statement on the customers table to set email to 'updated' where customer_id equals 'C101'.
UPDATE customers SET email = 'updated' WHERE customer_id = 'C101'
3. Write an UPDATE statement on the orders table to set customer_id to 'updated' where order_id equals NULL.
UPDATE orders SET customer_id = 'updated' WHERE order_id = NULL
4. Write an UPDATE statement on the inventory table to set product_id to 'updated' where warehouse equals NULL.
UPDATE inventory SET product_id = 'updated' WHERE warehouse = NULL
5. Write an UPDATE statement on the suppliers table to set supplier_name to 'updated' where supplier_id equals 'SUP001'.
UPDATE suppliers SET supplier_name = 'updated' WHERE supplier_id = 'SUP001'

Learn it by doing. This lesson is a short hands-on level on Bampaa: see it work, then build and write it yourself, with instant feedback.

Try this lesson free →
About 5–10 minutes · no sign-up needed