Pick one row per group using PostgreSQL's `DISTINCT ON` extension.
Section: Querying, Joins, and Aggregation
Use DISTINCT ON
sql
sql
SELECT DISTINCT ON (customer_id)
customer_id, order_id, created_at
FROM orders
ORDER BY customer_id, created_at DESC;Explanation
See summary for usage details.
Learn the surrounding workflow
Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.
Related commands
Same sheet · prioritizing Querying, Joins, and Aggregation