Files
supabase/apps/docs/content/guides/database/postgres/first-row-in-group.mdx
Kimaswa Emmanuel Yusufu 64085b2d0c docs: fix broken code examples and wrong identifiers in guides (#46755)
A handful of code samples in the guides either don't run or contradict
the surrounding text. I found these reading through the docs.

- `database/debugging-performance`: `insert into books` targets a table
that's never created. The table made just above is `instruments`.
- `database/drizzle`: the `db.ts` snippet references an undefined
`host`. The variable in scope is `connectionString`.
- `database/postgres/column-level-security`: the `create table` is
missing a comma after `created_at ... now()`, so it won't parse.
- `database/postgres/first-row-in-group`: `distinct on (team)` with
`order by id, ...` is rejected by Postgres (the DISTINCT ON column has
to lead the ORDER BY). Ordered by `team, points desc` so it returns one
row per team.
- `database/postgres/data-deletion`: reversed markdown link
`(text)[url]`, plus "parititioning" misspelled.
- `database/extensions/pg_plan_filter`: prose says
`statement_cost_filter`, but the real parameter (used everywhere else in
the file) is `statement_cost_limit`.
- `auth/auth-hooks/mfa-verification-hook`: the insert and on-conflict
update use `last_refreshed_at`, but the table column is
`last_failed_at`.
- `telemetry/advanced-log-filtering`: the "ends with" example writes
`'$port=12345'`. The `$` anchor needs to come after the literal:
`'port=12345$'`.
- `ai/examples/headless-vector-search`: uses `${projectURL}` but the
const is `projectUrl`.
- `getting-started/quickstarts/redwoodjs`: prose says
`scripts/seeds.ts`, but the code block and Redwood use
`scripts/seed.ts`.
- `getting-started/tutorials/with-flutter`: two code-fence headers have
a stray trailing `"`.
- `local-development/cli/testing-and-linting`: stray backtick in "Edge`
Functions".


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **Documentation**
* Corrected code examples across multiple guides including vector
search, authentication hooks, database guides, and quickstarts
* Fixed SQL syntax errors, variable names, and table references in
example snippets
* Resolved typos, broken links, and formatting inconsistencies in guide
text
  * Clarified parameter names and script references in documentation
  * Updated code fence syntax in tutorials for proper rendering

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

Co-authored-by: Chris Chinchilla <chris.ward@supabase.io>
2026-06-12 16:08:41 +00:00

46 lines
1.2 KiB
Plaintext

---
title: 'Select first row for each group in Postgres'
description: 'Postgres snippet for grabbing the first row in each distinct group by group'
footerHelpType: 'postgres'
---
Given a table `seasons`:
| id | team | points |
| --- | :-------: | -----: |
| 1 | Liverpool | 82 |
| 2 | Liverpool | 84 |
| 3 | Brighton | 34 |
| 4 | Brighton | 28 |
| 5 | Liverpool | 79 |
We want to find the rows containing the maximum number of points _per team_.
The expected output we want is:
| id | team | points |
| --- | :-------: | -----: |
| 3 | Brighton | 34 |
| 2 | Liverpool | 84 |
From the [SQL Editor](/dashboard/project/_/sql), you can run a query like:
```sql
select distinct
on (team) id,
team,
points
from
seasons
order by
team,
points desc;
```
The important bits here are:
- The `desc` keyword to order the `points` from highest to lowest.
- The `distinct` keyword that tells Postgres to only return a single row per team.
This query can also be executed via `psql` or any other query editor if you prefer to [connect directly to the database](/docs/guides/database/connecting-to-postgres#direct-connections).