Customers whose every order is fully paid
SQL
Hard
2 views
Problem Description
Return customers for which there is no order with unpaid balance. Unpaid means Orders.total_amount > sum(Payments.amount).
Constraints
Use NOT EXISTS on unpaid orders
Official Solution
SELECT c.customer_id, c.name FROM Customers c WHERE NOT EXISTS (SELECT 1 FROM Orders o WHERE o.customer_id = c.customer_id AND o.total_amount > COALESCE((SELECT SUM(p.amount) FROM Payments p WHERE p.order_id = o.order_id), 0));
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!