Question presented to candidate:
"A supplier sends a file of new prices, which we load into a price_updates table keyed by SKU. Write the statement that updates products.price from it. Then tell me what happens to products that are not in the file, and what happens if the file lists the same SKU twice."
What a strong answer should cover:
- The portable form:
SET col = (correlated subquery)together withWHERE EXISTS (same match), so unmatched rows are left alone - The join forms per engine:
UPDATE ... FROM(PostgreSQL, SQLite 3.33+, and SQL Server with its own variant),UPDATE t JOIN s ON ... SET(MySQL), andMERGE(PostgreSQL 15+, SQL Server, Oracle) - Without the
WHERE EXISTSguard the correlated form sets every unmatched row to NULL - Duplicate source rows:
UPDATE ... FROMsilently picks an arbitrary match,MERGEraises an error in PostgreSQL; de-duplicate the source first (ROW_NUMBER or DISTINCT ON) - Safety: run it in a transaction, check the affected row count or
RETURNING, and skip no-op updates withIS DISTINCT FROM
Clarifying questions expected:
- Which database and version? The syntax differs more here than almost anywhere else in SQL
- Can a SKU appear more than once in the file, and if so which row wins?
- Should SKUs missing from
productsbe inserted (an upsert) or ignored?
Code / implementation expected: Yes β one portable UPDATE and one join-style UPDATE, with the result checked by a SELECT.
Schema used by the examples: