Jeff Meatball Yang Jeff Meatball Yang -4 years ago 138
SQL Question

Does the order of columns matter in a group by clause?

If I have two columns, one with very high cardinality and one with very low cardinality (unique # of values), does it matter in which order I group by?

Here's an example:

from SomeFact f
join SomeDim d on f.dimensionKey = d.dimensionKey
group by
d.dimensionName, -- large number of unique values
d.dimensionCategory -- small number of unique values

Are there situations where it matters?

Answer Source

No, the order doesn't matter for the GROUP BY clause.

MySQL and SQLite are the only databases I'm aware of that allow you to select columns which are omitted from the group by (non-standard, not portable) but the order doesn't matter there either.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download