> For the complete documentation index, see [llms.txt](https://dailyjournal.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dailyjournal.gitbook.io/notes/languages/sql/aggregations.md).

# Aggregations

> Aggregators only aggregate vertically - the values of a column.
>
> SQL evaluates the aggregations before the `LIMIT` clause.

## `COUNT`

```sql
SELECT COUNT (*) AS account_count
FROM accounts
```

```sql
SELECT COUNT(primary_poc) AS account_primary_poc_count
FROM accounts
```

{% hint style="info" %}
**COUNT** does not consider rows that have **NULL** values. Therefore, this can be useful for quickly identifying which rows have missing data.
{% endhint %}

## `SUM` <a href="#sum" id="sum"></a>

Unlike **`COUNT`**, you can only use **`SUM`** on numeric columns. However, **`SUM`** will ignore **`NULL`** values.

## `MIN`, `MAX` & `AVG`

{% hint style="info" %}
&#x20;**MIN** and **MAX** are aggregators that again ignore **NULL** values.
{% endhint %}

## `GROUP BY`

* used to aggregate data within subsets of the data.
* Any column in the **`SELECT`** statement that is not within an aggregator must be in the **`GROUP BY`** clause.
* You can **GROUP BY** multiple columns at once, The order of column names in your **GROUP BY** clause doesn’t matter—the results will be the same regardless.

## `DISTINCT`

{% hint style="warning" %}
using **`DISTINCT`**, particularly in aggregations, can slow your queries down quite a bit.
{% endhint %}

## HAVING

## DATE Functions

{% content-ref url="/pages/Xj5jN0a6e61CgjV7EXje" %}
[Functions](/notes/languages/sql/functions.md)
{% endcontent-ref %}

## CASE Statements

* The CASE statement always goes in the SELECT clause.
* CASE must include the following components: WHEN, THEN, and END. ELSE is an optional component to catch cases that didn’t meet any of the other previous CASE conditions.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://dailyjournal.gitbook.io/notes/languages/sql/aggregations.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
