# 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="functions" %}
[functions](https://dailyjournal.gitbook.io/notes/languages/sql/functions)
{% 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.
