Insert or update (UPSERT) in SQL

SQL interview exercise on Modifying Data, difficulty 3 of 3. Solve it in the editor beside a worked example; the tests run the moment you do.

Add one Wireless Mouse (product 2, unit price 40) to order 1, in a single statement that works either way: if the order already has that product, raise its quantity by 1; if not, add a new line with quantity 1. The primary key of order_items is (order_id, product_id).

Each exercise pairs a worked example with a twin problem to solve in a real editor; the tests run the moment you do. Progress is saved in your browser, and in your account if you sign in.

More Modifying Data in SQL

All SQL exercises · Other languages and frameworks