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

Reset filters
  • HardPremium
    PostgreSQL · ClickHouse

    Unpivot a Wide Sales Table into Long Format

    The data arrived in wide format: one row per product, with amounts for three months stored in separate columns m1, m2, and m3.

  • HardPremium
    PostgreSQL

    Inventory of Plan Features

    You're an analyst at Vexcom, a telecom operator. Over the years, each service plan has accumulated its own collection of features in a JSONB object: some keys hold numeric values for gigabytes and…

  • HardPremium
    PostgreSQL

    JSON Menu Feed for a Delivery Aggregator

    You're a backend engineer at ForkHouse, a restaurant group. A delivery aggregator retrieves menus through an API and requires exactly one row per restaurant, containing a JSON array of menu items…

  • HardPremium
    PostgreSQL

    Find Sessions with Rebuffering

    You're a video streaming quality analyst at GlowPlay. At the end of each session, the player sends a JSONB array named events containing objects with an event type stored under the key type (play…

  • HardPremium
    PostgreSQL

    Find Listings with Large Bedrooms

    You're a data engineer at KeyNest, a real estate portal. Each listing has a JSONB document called doc containing details such as price, amenities, and a rooms array of room objects.

  • HardPremium
    PostgreSQL

    Activity by Local Calendar Date

    You're a data analyst at LinguaLoop, an educational platform where people around the world learn languages through short lessons.

  • HardPremium
    PostgreSQL

    Revenue Details and Subtotals in One Query

    You're an analyst at a telecom operator. The finance department is preparing a monthly report on revenue by service plan and is tired of combining three separate exports: a region-by-plan breakdown…

  • HardPremium
    PostgreSQL

    Booking Totals by City and Country

    You're an analyst at an online travel agency. The commercial director reviews tour sales using a country → city hierarchy and wants a single hierarchical report: one row for each city within a…

  • HardPremium
    PostgreSQL

    Property Deals Matrix: District × Property Type

    You're an analyst at a real estate agency. The managing partner is preparing for a quarterly meeting and wants a complete breakdown: deals by district × property type, subtotals for each district…

  • HardPremium
    PostgreSQL

    Salary Bands by Department

    You're an analyst on the compensation and benefits team. The HR director is reviewing salary bands and wants a statistical breakdown for each department: the lower quartile, median, upper quartile…

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.