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.

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton