Back to Developer Roadmap

Group By Mistakes

src/data/question-groups/sql-queries/content/group-by-mistakes.md

4.01002 B
Original Source

The common mistakes people encounter when using the GROUP BY clause include:

  • Selecting non-aggregated columns not in the GROUP BY clause: This is a common mistake made my beginners and experts. An example query of this looks like this:
sql
SELECT day, amount FROM Sales
GROUP BY day

In the query above, the amount column is not part of the GROUP BY clause and will throw an error that it must appear in the GROUP BY clause. To fix this, you should add an aggregate function to the amount column.

sql
SELECT day, MAX(amount) FROM Sales
GROUP BY day
  • Not using aggregate functions: It is also a common mistake to use GROUP BY without aggregate functions. GROUP BY usually goes with aggregate functions like MAX, MIN, COUNT, etc.
  • Grouping by multiple columns: Grouping by multiple columns can make the query meaningless. It is not common to group by many columns, and when this happens, you should check if you really need to group by those columns.