Below is the query I'm running. Does the `DISTINCT` get applied before or after the `GROUP BY`?

The table looks like this: `id, state`

The `id` is not unique, and an entity can have many entries with the same state or different states.

This behavior could drastically change my results. If `DISTINCT` is applied before `GROUP BY`, then it will only count each `entity` once throughout the entire set ( will only show up in one `state` ). If it happens after, then each `entity` will only be counted once per `state`, but could be counted in many `states`.

```
SELECT
    state,
    COUNT( DISTINCT entity_id ) AS count
FROM entities
GROUP BY state
ORDER BY count DESC;

```

My goal is to count each entity only once per `state` but to allow an `entity` to be counted in more than one `states` ... which is the behavior I would expect if `DISTINCT` is applied after `GROUP BY`.