← All Exercises

SQL GROUP BY with Aggregates

basicsql~15min

Background

GROUP BY groups rows by column values, allowing aggregate functions to compute summaries for each group.

Problem

Write a SQL query to count employees and calculate average salary per department.

Input

An employees table with id, name, department, salary columns.

Output

One row per department with employee count and average salary.

Sample tests

Sample 1

Input: employees in 3 departments
Expected: 3 rows with department, count, avg_salary

Your solution

Learning objective

Learn GROUP BY, aggregate functions, column aliasing, and grouping semantics.