Skip to content
ohsudo, Home

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

    1. How many?. COUNT(*) counts rows. With no GROUP BY, the whole table becomes one row of result.
    2. Smallest and largest. MIN, MAX, SUM and AVG work the same way, on a column.
    3. Per group. GROUP BY makes one row of result per value. Every column you select must either be in GROUP BY or inside a function like COUNT.
    4. An average per group. ROUND makes a long number readable.
    5. Biggest first. You can sort by the result of a function, or by a name you give it with AS.
    6. Filter the groups. WHERE filters rows before grouping, and HAVING filters the groups after. WHERE COUNT(*) > 3 is an error, because there are no counts yet at that point.

    See also