Comment Comment by continuational Quite often what you want isn't an arbitrary row (for which exists is correct), but a specific one.In that case, PostgreSQL has a feature where you can do this: select distinct on (customer_id) customer_id, order_id from orders order by customer_id, created_date desc And get all customer IDs with their latest order ID. parent continuational 3 years ago
In that case, PostgreSQL has a feature where you can do this:
And get all customer IDs with their latest order ID.