Sign in to run and submit your work
Reading is open to everyone. Running code and saving drafts need an account so your work is yours and comes back on your next visit.
or
CODE 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.
Result columns · in this order
customer_id | Stable customer identifier. |
country | Customer country. Can be NULL when unknown. |
How to approach it
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 |
6 rows — all rows shown.
| order_id | buyer_id | status |
|---|---|---|
| 1004 | 1 | shipped |
| 1008 | 4 | shipped |
| 1012 | 3 | shipped |
3 rows — all rows shown.
Expected output
| customer_id | country |
|---|---|
| 1 | US |
| 3 | GB |
| 4 | IN |
3 rows — all rows shown.
Constraints
Use EXISTS with a correlated subquery on orders (a semi-join) so a customer with several shipped orders still appears once. Do not use a JOIN that would multiply the customer rows.
Expected skills
Semi-join with EXISTS for existence questions, returning each left row at most once and avoiding fan-out.
Submit for review to find out what your query gets right, what it gets wrong, and how it compares with the best working query for this exercise.
This scenario runs a full workspace — editor, canvas and results side by side. It needs a laptop or desktop to be usable. Open this page on a bigger screen to start building.