Files
supabase/apps/docs/content/guides/database/postgres/first-row-in-group.mdx
Miranda Limonczenko f5b00a5237 docs: use the standard run snippet for run-this-query steps (#51974)
## Problem

Many pages tell readers to run SQL in the SQL Editor only. An agent
can't use the Dashboard, so it has no way to run the query.
supabase/supabase#51973 adds a standard snippet that also offers `psql`.
Existing pages don't use it yet.

## Solution

- **Snippet swap:** each simple run-SQL instruction now uses the
standard snippet, placed before the SQL block. Where the old sentence
said what the SQL does, that part stays as its own sentence before the
snippet.


## Preview links

On each page, search for `over a direct connection`.

| Page | Live | Preview |
| --- | --- | --- |
| `ai/automatic-embeddings` |
[Live](https://supabase.com/docs/guides/ai/automatic-embeddings) |
[Preview](https://docs-git-docs-sql-run-sentence-sweep-supabase.vercel.app/docs/guides/ai/automatic-embeddings)
|
| `database/connecting-to-postgres/pooling-and-limits` |
[Live](https://supabase.com/docs/guides/database/connecting-to-postgres/pooling-and-limits)
|
[Preview](https://docs-git-docs-sql-run-sentence-sweep-supabase.vercel.app/docs/guides/database/connecting-to-postgres/pooling-and-limits)
|
| `database/extensions/pg_net` |
[Live](https://supabase.com/docs/guides/database/extensions/pg_net) |
[Preview](https://docs-git-docs-sql-run-sentence-sweep-supabase.vercel.app/docs/guides/database/extensions/pg_net)
|
| `database/postgres/dropping-all-tables-in-schema` |
[Live](https://supabase.com/docs/guides/database/postgres/dropping-all-tables-in-schema)
|
[Preview](https://docs-git-docs-sql-run-sentence-sweep-supabase.vercel.app/docs/guides/database/postgres/dropping-all-tables-in-schema)
|
| `database/postgres/first-row-in-group` |
[Live](https://supabase.com/docs/guides/database/postgres/first-row-in-group)
|
[Preview](https://docs-git-docs-sql-run-sentence-sweep-supabase.vercel.app/docs/guides/database/postgres/first-row-in-group)
|
| `database/postgres/row-level-security` |
[Live](https://supabase.com/docs/guides/database/postgres/row-level-security)
|
[Preview](https://docs-git-docs-sql-run-sentence-sweep-supabase.vercel.app/docs/guides/database/postgres/row-level-security)
|
| `database/postgres/which-version-of-postgres` |
[Live](https://supabase.com/docs/guides/database/postgres/which-version-of-postgres)
|
[Preview](https://docs-git-docs-sql-run-sentence-sweep-supabase.vercel.app/docs/guides/database/postgres/which-version-of-postgres)
|
| `deployment/managing-environments` |
[Live](https://supabase.com/docs/guides/deployment/managing-environments)
|
[Preview](https://docs-git-docs-sql-run-sentence-sweep-supabase.vercel.app/docs/guides/deployment/managing-environments)
|
| `getting-started/quickstarts/hono` |
[Live](https://supabase.com/docs/guides/getting-started/quickstarts/hono)
|
[Preview](https://docs-git-docs-sql-run-sentence-sweep-supabase.vercel.app/docs/guides/getting-started/quickstarts/hono)
|
| `getting-started/quickstarts/refine` |
[Live](https://supabase.com/docs/guides/getting-started/quickstarts/refine)
|
[Preview](https://docs-git-docs-sql-run-sentence-sweep-supabase.vercel.app/docs/guides/getting-started/quickstarts/refine)
|
| `getting-started/quickstarts/spring-boot` |
[Live](https://supabase.com/docs/guides/getting-started/quickstarts/spring-boot)
|
[Preview](https://docs-git-docs-sql-run-sentence-sweep-supabase.vercel.app/docs/guides/getting-started/quickstarts/spring-boot)
|
| `platform/database-size` |
[Live](https://supabase.com/docs/guides/platform/database-size) |
[Preview](https://docs-git-docs-sql-run-sentence-sweep-supabase.vercel.app/docs/guides/platform/database-size)
|
| `platform/postgres-connection-logging` |
[Live](https://supabase.com/docs/guides/platform/postgres-connection-logging)
|
[Preview](https://docs-git-docs-sql-run-sentence-sweep-supabase.vercel.app/docs/guides/platform/postgres-connection-logging)
|
| `platform/upgrading` |
[Live](https://supabase.com/docs/guides/platform/upgrading) |
[Preview](https://docs-git-docs-sql-run-sentence-sweep-supabase.vercel.app/docs/guides/platform/upgrading)
|

## Manual testing

1. Open [Which version of
Postgres](https://docs-git-docs-sql-run-sentence-sweep-supabase.vercel.app/docs/guides/database/postgres/which-version-of-postgres).
The snippet appears before the query.
2. Click **Direct connection** in the snippet. It goes to the **Direct
connection** section of the connection guide.
3. Open [Row Level
Security](https://docs-git-docs-sql-run-sentence-sweep-supabase.vercel.app/docs/guides/database/postgres/row-level-security).
The snippet comes before the SQL, and the migration option follows it.
2026-10-20 16:45:08 +00:00

44 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 |
Run the following SQL using the [SQL Editor](/dashboard/project/_/sql) in the Supabase Dashboard or with `psql` over a direct connection. To connect with `psql`, see [Direct connection](/docs/guides/database/connecting-to-postgres#direct-connection).
```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.