Upsert order status
SQL
Medium
3 views
Problem Description
If an order exists update its status, otherwise insert it. Write an upsert pattern.
Output Format
DML statement(s)
Sample Test Case
Input:
Orders(order_id, status)
Output:
Row inserted/updated
Constraints
DB-specific; show MERGE style
Official Solution
MERGE INTO Orders o USING (SELECT 5001 AS order_id, 'SHIPPED' AS status) s ON (o.order_id = s.order_id) WHEN MATCHED THEN UPDATE SET status = s.status WHEN NOT MATCHED THEN INSERT (order_id, status) VALUES (s.order_id, s.status);
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!