Update contact names in an accounts table to match the currently assigned
- salesmen:
+ salespeople:
UPDATE accounts SET (contact_first_name, contact_last_name) =
- (SELECT first_name, last_name FROM salesmen
- WHERE salesmen.id = accounts.sales_id);
+ (SELECT first_name, last_name FROM employees
+ WHERE employees.id = accounts.sales_person);
A similar result could be accomplished with a join:
UPDATE accounts SET contact_first_name = first_name,
contact_last_name = last_name
- FROM salesmen WHERE salesmen.id = accounts.sales_id;
+ FROM employees WHERE employees.id = accounts.sales_person;
However, the second query may give unexpected results
- if salesmen.id is not a unique key, whereas
+ if employees.id is not a unique key, whereas
the first query is guaranteed to raise an error if there are multiple
id matches. Also, if there is no match for a particular
- accounts.sales_id entry, the first query
+ accounts.sales_person entry, the first query
will set the corresponding name fields to NULL, whereas the second query
will not update that row at all.