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:
select dimensionName, dimensionCategory, sum(someFact) 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?
The Order by clause does not affect the ordering of the records in the source table or changing the physical structure of the table. It is just a logical re-structuring of physical data. Next, add the SQL Order By clause in the query.
Note: I would not recommend using column position in Order By clause. You should always use a column name in Order by clause.
The ORDER BY clause is not strictly necessary. It's up to you to decide whether you want the result set ordered. However, given the whole point of GROUP BY , the following is recommended: You probably want the data displayed in the same order as per the GROUP BY clause, to make the grouping more apparent.
Column order does not matter while creating a table. We can arrange the columns while retrieving the data from the database. Primary key can be set to any column or combination of columns.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With