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
    PostgreSQL · MySQL · ClickHouse

    Patient Count in Ward CARD-B

    The cardiology head nurse is requesting patient-rounding forms for ward CARD-B. Count how many patients are currently in this ward.

  • EasyFree
    PostgreSQL · MySQL · ClickHouse

    Ward Occupancy

    The department head is planning changes to ward assignments and wants to see which wards currently have the most patients. Return each ward code and the number of patients in that ward.

  • EasyFree
    PostgreSQL · MySQL · ClickHouse

    Sent Message Counts by User

    The messaging platform is preparing a summary of sender activity. For each sender_id, count the messages sent and return the count in a column named msg_count.

    VK
  • EasyFree
    PostgreSQL · MySQL · ClickHouse

    Average Driver Rating

    The quality team is calculating the average driver rating across the service. Find the average rating, round it to 2 decimal places, and return the result in a column named avg_rating.

    Yandex
  • EasyFree
    PostgreSQL · MySQL · ClickHouse

    Duplicate Email Addresses

    Find all email addresses that appear in the table more than once.

  • EasyFree
    PostgreSQL · MySQL · ClickHouse

    Unique Friend Request Pairs

    A social media platform is counting unique connections based on friend requests. Count the number of unique pairs of users, ignoring the direction of each request: A→B and B→A count as the same pair.

  • EasyFree
    PostgreSQL · MySQL · ClickHouse

    Each Player's First Login Date

    The game analytics team is studying when players first log in. For each player, return player_id and the date of their first login in a column named first_login.

  • EasyFree
    PostgreSQL · MySQL · ClickHouse

    Each Player's First Device

    The game analytics team is studying which devices players use when they first log in to the game. For each player, find the device they used for their first login.

  • EasyFree
    PostgreSQL · MySQL · ClickHouse

    Percentage of Immediate Deliveries

    The food delivery team is evaluating delivery speed. Find the percentage of orders whose preferred delivery date is the same as the order date, meaning orders where order_date =…

  • EasyFree
    PostgreSQL · MySQL · ClickHouse

    Number of Comments per Post

    A social media platform is building a dashboard to track activity on posts. The table contains both posts and comments. A post is a record where parent_id IS NULL.

  • EasyFree
    PostgreSQL · MySQL · ClickHouse

    Total Quantity Sold by Product

    The finance team needs a breakdown of sales by product. For each product, calculate the total quantity sold and return product_id and total_quantity.

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.