Counting and grouping: GROUP BY and HAVING
So far, each row of the result was one row of the table. Now you'll sum them up: how many people per department, and the average pay.
● A real database, running in your browser. Nothing you type leaves this page.
Exercise 1 of 6
How many?
COUNT(*) counts rows. With no GROUP BY, the whole table becomes one row of result.
Your task
Count the rows in employees.
Tables and columns
or Ctrl+Enter
What you'll practice
- How many?.
COUNT(*)counts rows. With noGROUP BY, the whole table becomes one row of result. - Smallest and largest.
MIN,MAX,SUMandAVGwork the same way, on a column. - Per group.
GROUP BYmakes one row of result per value. Every column you select must either be inGROUP BYor inside a function likeCOUNT. - An average per group.
ROUNDmakes a long number readable. - Biggest first. You can sort by the result of a function, or by a name you give it with
AS. - Filter the groups.
WHEREfilters rows before grouping, andHAVINGfilters the groups after.WHERE COUNT(*) > 3is an error, because there are no counts yet at that point.