Left join to find missing match
SQL
Easy
2 views
Problem Description
List all customers and their latest order_id if any. Customers without orders should still show.
Sample Test Case
Output:
Customers with optional order
Constraints
Use left join and max
Official Solution
SELECT c.customer_id, c.name, MAX(o.order_id) AS latest_order_id FROM Customers c LEFT JOIN Orders o ON o.customer_id = c.customer_id GROUP BY c.customer_id, c.name;
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!