SQL Aggregate Functions Practice

Turn individual records into useful metrics with SQL aggregate functions practice. Count events, calculate totals and averages, group results by users or products, and filter groups based on their values. Your SQL runs in a real database, with results automatically checked against hidden data. Problems in this set: 266, free: 70.

Problems in this set: 266, free: 70

Start with a free problem

Aggregation and GROUP BY problems

Problems found: 70

Reset filters
  • EasyFree
    ClickHouse

    Events per day (ClickHouse)

    Group the events table by the start of the day of the ts timestamp (toStartOfDay) and, for each day, count the total number of events. Return columns day (start of day) and events (row count).

  • EasyFree
    ClickHouse

    Events per hour bucket (ClickHouse)

    Group events by the start of the hour of the ts timestamp (toStartOfHour) and, for each hour bucket, count the number of events. Return columns hour (start of hour) and events.

  • EasyFree
    ClickHouse

    Event distribution by hour of day (ClickHouse)

    Extract the hour of day from ts with toHour (an integer 0..23, no date) and, for each hour of day, count the number of events across the whole events table. Return columns hour_of_day and events.

  • EasyFree
    ClickHouse

    Unique Product Count by User

    From the events table (columns user_id, product_id) count, for each user_id, the number of distinct product_id values. Return columns user_id and distinct_products. Order by user_id.

  • EasyFree
    ClickHouse

    Total revenue per user

    In sales(id, user_id, product, amount, ts) compute the total purchase amount for each user. Group rows by user_id and return columns user_id and total_amount (sum of amount).

  • EasyFree
    ClickHouse

    Order count per user

    From sales(id, user_id, product, amount, ts) count how many order rows belong to each user. Group by user_id and return columns user_id and orders (number of rows). Order by user_id ascending.

  • EasyFree
    ClickHouse

    Most expensive purchased product per user

    In sales(id, user_id, product, amount, ts), for each user_id find the product with the highest purchase amount and that amount itself.

  • EasyFree
    ClickHouse

    Cheapest purchased product per user

    In sales(id, user_id, product, amount, ts), for each user_id find the product with the lowest purchase amount and that minimal amount.

  • EasyFree
    ClickHouse

    Median session duration by country

    From the sessions table, for each country compute the median of duration_sec. Return columns country and median_duration ordered by country ascending.

  • EasyFree
    ClickHouse

    Median and average duration by device

    From the sessions table, for each device compute the median of duration_sec (median_duration) and the average duration rounded to 2 decimals (avg_duration).

  • EasyFree
    ClickHouse

    Sessions per device

    From the sessions table, count how many sessions each device has. Group by device and return columns device and sessions (the row count of sessions). Sort by device ascending.

  • EasyFree
    ClickHouse

    Bounce rate per device

    From the sessions table, compute each device's bounce rate: the ratio of sessions with bounced = 1 to the total number of sessions.

What you will practice

  • Understand how COUNT handles rows and missing values
  • Calculate sums and averages for individual groups
  • Choose the correct level of detail for GROUP BY
  • Filter aggregated results using HAVING
  • Compare metrics across users, products, and time periods

Common mistakes

  1. 1. Grouping at the wrong level

    Adding an unnecessary column to GROUP BY can split one expected group into several. Leaving out a required column can combine unrelated records. Define what one output row should represent, such as a user, product, day, or combination of dimensions, before building the aggregation.

  2. 2. Confusing COUNT(*) with COUNT(column)

    COUNT(*) counts rows, while COUNT(column) ignores rows where that column is NULL. The difference matters whenever data is missing. Compare the counts on a small sample and decide whether the task requires every record or only records with a non-NULL value.

  3. 3. Filtering aggregated values with WHERE

    WHERE filters individual rows before aggregation. HAVING filters groups after aggregate values have been calculated. If you need products with a minimum number of purchases, removing rows too early can change the result. Separate conditions on source records from conditions on grouped metrics.

  4. 4. Counting or summing duplicated joined rows

    A JOIN can multiply records before aggregation. For example, an order may appear several times when joined to a table containing multiple related entries. That can inflate totals without producing an obvious error. Check the join keys and the number of underlying records being counted.

Theory and courses

Frequently asked questions

How does the trainer check GROUP BY queries?

Your SQL executes in a real database, and the resulting data is automatically compared with an expected result using hidden data. A correct query must handle more than the obvious cases, including missing values, repeated records, and groups of different sizes.

Can I practice SQL aggregation without a subscription?

Yes. Free problems in this collection: 70. Once signed in, you can solve up to 3 new problems per day on the free plan. Premium problems are listed alongside them, but solving those requires a subscription.

When should I use WHERE instead of HAVING?

Use WHERE for conditions on individual records before grouping, such as restricting events to a particular date range. Use HAVING when the condition depends on an aggregate result, such as total sales. A single query can use both to filter source data and then filter groups.

Why does COUNT return a larger number than expected?

You may be counting events when the problem asks for unique users or orders. Duplicate records and joins can also increase the total. Check what each row represents and whether NULL values matter. Match the counting method to the exact entity described in the question.

How is GROUP BY different from window functions?

GROUP BY combines source rows into aggregated results, typically one row per group. Window functions calculate values across related rows without necessarily collapsing the original rows. Choose GROUP BY when you need a summary and window functions when individual records must remain visible.