D8LooPFocus modeCODE WORKSPACE
Return c.customer_id and c.country for customers who have at least one shipped order. Each customer should appear once. Order by customer_id.
WHERE EXISTS (SELECT 1 FROM orders o WHERE o.buyer_id = c.customer_id AND o.status = 'shipped').
Sample input
| customer_id | country |
|---|---|
| 1 | US |
| 2 | null |
| 3 | GB |
| 4 | IN |
| 5 | US |
| 6 | GB |
| order_id | buyer_id | status |
|---|---|---|
| 1004 | 1 | shipped |
| 1008 | 4 | shipped |
| 1012 | 3 | shipped |
Expected output
| customer_id | country |
|---|---|
| 1 | US |
| 3 | GB |
| 4 | IN |
Constraints
Use EXISTS with a correlated subquery on orders (a semi-join) so a customer with several shipped orders still appears once.
Expected skills
Semi-join with EXISTS for existence questions, returning each left row at most once and avoiding fan-out.
Run the SQL query to inspect preview rows.