Correlated delete using subquery
SQL
Hard
2 views
Problem Description
Delete Products that were created more than 365 days ago and never appeared in OrderItems. Use NOT EXISTS to avoid NULL issues.
Output Format
DML statement(s)
Sample Test Case
Input:
Products, OrderItems
Constraints
Assume PostgreSQL-style interval
Official Solution
DELETE FROM Products p WHERE p.created_at < (CURRENT_DATE - INTERVAL '365 days') AND NOT EXISTS (SELECT 1 FROM OrderItems oi WHERE oi.product_id = p.product_id);
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!