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: 99

Reset filters
  • MediumPremium
    PostgreSQL · MySQL · ClickHouse

    Most Popular Email Domains

    The growth team wants to know which email providers passengers use to better target email campaigns. Group passenger email addresses by domain, meaning everything after the @ symbol.

  • MediumPremium
    PostgreSQL · MySQL · ClickHouse

    Monthly Booking Counts

    The finance team needs a monthly breakdown of bookings. For each calendar month with at least one booking based on the booking start date start_date, return the year as year, the month as month, and…

  • MediumPremium
    PostgreSQL · MySQL · ClickHouse

    Average Booking Cost

    To measure the platform's average booking value, calculate the average of total in Bookings across all bookings and return it in the avg_price column. Round the result to two decimal places.

  • MediumPremium
    PostgreSQL · MySQL · ClickHouse

    Cabins with an Odd Number of Bookings

    The cleaning team schedules deep cleanings for cabins in cycles of two stays: a cabin receives a full cleaning after every second booking.

  • MediumPremium
    PostgreSQL · MySQL · ClickHouse

    User Distribution by 2022 Tweet Count

    Twitter is analyzing the distribution of user activity in 2022. Group users by their annual tweet count and return that count in a column named tweet_bucket.

    Twitter (X)
  • MediumPremium
    PostgreSQL · MySQL · ClickHouse

    Daily Active Users Over the Last Month

    The product analytics team tracks daily user activity. Filter events to the specified window and, for each day with activity, count distinct users in a column named active_users.

    Dodo Brands
  • MediumPremium
    PostgreSQL · MySQL · ClickHouse

    Category with the Most Promo Codes

    The marketing team is analyzing promo code entries to find the most popular category. Find the category with the most entered codes. Return category and code_count.

    Otkritie Bank
  • MediumPremium
    PostgreSQL · MySQL

    Active Warehouses by City

    The logistics team is preparing a report on active warehouses. For each city, count the warehouses where is_active = true. Return city and warehouse_count.

    Samokat
  • MediumPremium
    PostgreSQL · MySQL · ClickHouse

    City with the Most Users

    The growth team wants to identify the city with the most registered users. Find that city and return city and user_count.

    VK
  • MediumPremium
    PostgreSQL · MySQL · ClickHouse

    Course Completion Rate

    An online learning platform is analyzing course progress. For each course, calculate the completion percentage using the formula 100 * number of completions / number of records.

    Skypro

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.