Antipattern: Reference Non-grouped Columns

Let’s explore how misconception about the GROUP BY clause causes an antipattern by testing out different queries.

The root cause of this antipattern is simple, and it reveals a common misconception that many programmers have about how grouping queries work in SQL.

The single-value rule

The rows in each group consist only of items that have the same value in the column or columns we name after GROUP BY. For example, in the following query, there is a one-row group for each distinct value in product_id.

Get hands-on with 1200+ tech skills courses.