GROUP BY and HAVING in SQL

前端 未结 1 1288
野的像风
野的像风 2021-01-27 02:41

If I write

SELECT continent FROM world GROUP BY continent HAVING sum(population) >= 100000000

it will return all continents that have a to

相关标签:
1条回答
  • 2021-01-27 03:20

    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.

    0 讨论(0)
提交回复
热议问题