Lesson 26 / 37 · Grouping

DISTINCT is GROUP BY without aggregates

DISTINCT runs after SELECT and only sees the selected columns. It gives the same rows as grouping by all of them, without the chance to count. The "copies" column shows how many identical rows collapsed into each survivor.

The query

SELECT DISTINCT c.country, o.status
FROM orders o
JOIN customers c ON c.id = o.customer_id
ORDER BY c.country, o.status

Try GROUP BY c.country, o.status instead of DISTINCT: the same 9 rows, now with room for count(*).

Next: A CTE is a named step that runs first · All lessons