
Understanding `GROUP BY` in SQL
GROUP BY is one of those SQL clauses that starts out feeling simple and then becomes confusing as soon as real queries get involved. The short version is that it lets you take many rows and treat some of them as belonging to the same group, usually so you can calculate something for each group. That idea matters more than the syntax. GROUP BY is not mainly about sorting, filtering, or removing duplicates. It is about collapsing rows into groups based on shared values, then producing one result row per group. What GROUP BY does Imagine a table called orders : order_id | user_id | status | amount ---------|---------|---------|------- 1 | 10 | paid | 50 2 | 10 | paid | 20 3 | 11 | pending | 15 4 | 12 | paid | 40 5 | 11 | paid | 30 If you run this: SELECT status FROM orders ; you get one row per order. If instead you run this: SELECT status FROM orders GROUP BY status ; you get one row per distinct status value: paid pending At first glance, that can look similar to DISTINCT , and in this
Continue reading on Dev.to
Opens in a new tab




