If I write
SELECT continent FROM world GROUP BY continent HAVING sum(population) >= 100000000
it will return all continents that have a to
When you don't have GROUP BY
, aggregate functions like SUM()
operate over the entire table, treating it all as one big group. That's why you just get one row of results.
When you use an aggregate function, it's not technically valid to return any columns in the SELECT
list other than those in the GROUP BY
clause, so your query isn't valid SQL. Some databases, such as MySQL, allow returning other columns as an extension; in that case, it selects the values from arbitrary rows in the group. And if there's no GROUP BY
clause at all, the entire table is one group, so you get the continent
column from some random row in the table.