# Counting Records With Ecto

Sometimes you want to know how many records there are in a table. Ecto gives us a couple ways to approach this.

We can use the [`count\1`](https://hexdocs.pm/ecto/Ecto.Query.API.html#count/1) function that the Ecto query API provides.

```elixir
> Repo.one(from p in "people", select: count(p.id))

16:09:52.759 [debug] QUERY OK source="people" db=1.6ms
SELECT count(p0."id") FROM "people" AS p0 []
168
```

Alternatively, we can use the [`fragment/1`](https://hexdocs.pm/ecto/Ecto.Query.API.html#fragment/1) function to use PostgreSQL's `count` function.

```elixir
> Repo.one(from p in "people", select: fragment("count(*)"))

16:11:19.818 [debug] QUERY OK source="people" db=1.5ms
SELECT count(*) FROM "people" AS p0 []
168
```

Lastly, `Ecto.Repo` has the [`aggregate/4`](https://hexdocs.pm/ecto/Ecto.Repo.html#c:aggregate/4) function which provides a `:count` option.

```elixir
> Repo.aggregate(from(p in "people"), :count, :id)

16:11:23.786 [debug] QUERY OK source="people" db=1.7ms
SELECT count(p0."id") FROM "people" AS p0 []
168
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ploegert.gitbook.io/til/programmy/elixir/counting-records-with-ecto.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
