ClickHouse SQL Exercises

These ClickHouse SQL exercises focus on the kinds of data used in analytics: user events, purchases, logs, timestamps, and arrays. Practice calculating metrics, grouping events into time intervals, and using ClickHouse-specific functions alongside window functions. Your SQL runs in a real ClickHouse database and is automatically checked using hidden data. Problems: 535, free: 153.

Problems in this set: 535, free: 153

Start with a free problem

ClickHouse problems

FiltersHard · Premium

Problems found: 98

Reset filters
  • HardPremium
    ClickHouse

    First and last purchase amount per user

    In table sales (id, user_id, product, amount, ts) for each user_id output the amount of its first purchase (smallest ts) as first_amount and the amount of its last purchase (largest ts) as…

  • HardPremium
    ClickHouse

    Difference from the user's previous purchase

    In table sales (id, user_id, product, amount, ts) output, for each row, the amount of this user_id's previous purchase by ts as prev_amount (0 for the first purchase), and the difference diff =…

  • HardPremium
    ClickHouse

    Top purchase (ClickHouse, argMax)

    For each user_id, find the product of their single most expensive purchase (top_product) and its amount (top_amount). Use argMax. Order by user_id.

  • HardPremium
    ClickHouse

    Interquartile range of duration by country

    From the sessions table, for each country compute the interquartile range of duration_sec: the 75th percentile minus the 25th percentile (iqr_duration).

  • HardPremium
    ClickHouse

    Countries above the overall median duration

    From the sessions table, compute the median of duration_sec per country and keep only countries whose median is strictly greater than the overall median of duration_sec across the whole table.

  • HardPremium
    ClickHouse

    Running share of a user's total

    In table sales (id, user_id, product, amount, ts) compute, for each row, the running share: the cumulative amount (by ts up to and including the current row) divided by the user's full purchase…

  • HardPremium
    ClickHouse

    First and last event type per product by time

    From the events table (columns product_id, event_type, ts), for each product_id determine the type of the earliest event (first_event_type) and the latest event (last_event_type) by timestamp ts.

  • HardPremium
    ClickHouse

    Product share of all views

    From the events table (columns product_id, event_type), for each product_id compute views (number of 'view' events) and view_share — the product's share of all 'view' events across the dataset…

  • HardPremium
    ClickHouse

    User's product list as a string

    From the events table (columns user_id, product_id) build, for each user_id, a string of the distinct product_id values sorted ascending and joined by a comma (no spaces).

  • HardPremium
    ClickHouse

    Most expensive purchase per user (argMax)

    From sales(id, user_id, product, amount, ts) find, for each user, the maximum purchase amount and the product of that purchase.

  • HardPremium
    ClickHouse

    Viewed vs purchased products per user

    From the events table (columns user_id, product_id, event_type) build, for each user_id, two sorted arrays of distinct product_id: viewed for events with event_type = 'view', and purchased for…

What you will practice

  • Count and sum matching events with countIf and sumIf
  • Process arrays and expand their elements using arrayJoin
  • Use uniq for estimates and argMax for associated values
  • Group timestamped events into intervals with toStartOfHour
  • Apply window functions to sequences of user activity

Common mistakes

  1. 1. Forgetting that arrayJoin multiplies rows

    arrayJoin expands array elements into separate rows. One original event can therefore become several rows, changing subsequent counts and sums. If you need to count original events rather than array elements, check how the transformation affects the data, including events with empty arrays.

  2. 2. Treating uniq as an exact count

    ClickHouse's uniq function returns an approximate number of distinct values. That may be appropriate for analytics, but it is not interchangeable with an exact count when precision matters. Check the problem's requirements before selecting a distinct-counting function.

  3. 3. Assuming argMax resolves ties predictably

    argMax returns a value associated with the maximum of another expression. If several rows share that maximum, the selected value may not be deterministic. Consider whether tied timestamps or amounts are possible and whether the problem needs an additional way to choose between them.

  4. 4. Grouping events into the wrong time buckets

    Functions such as toStartOfHour group timestamps by interval boundaries. If the expected reporting timezone differs from the timestamp interpretation, events can land in the wrong hour or day. Inspect records near interval boundaries, especially around midnight, to catch these mistakes.

Theory and courses

Frequently asked questions

Can I run these queries in real ClickHouse?

Yes. Every query executes in a real ClickHouse database, and its result is automatically checked against expected output using hidden data. The exercises cover event aggregation, arrays, time-based analysis, and window functions, so you can practice both syntax and analytical reasoning.

Are there free ClickHouse SQL exercises?

Yes. Free problems in this collection: 153. Once signed in, you can solve up to 3 new problems per day on the free plan. Premium problems are visible but require a subscription. Hints and an AI mentor are available in the problems.

What does arrayJoin do in ClickHouse?

arrayJoin expands the elements of an array into separate rows. This is useful when an event contains a list of products or other values that must be analyzed individually. Because it changes row counts, decide whether your later calculations should count original events or expanded elements.

When should I use countIf or sumIf?

Use countIf when you need to count rows that meet a condition, and sumIf when you need to sum values only for matching rows. Both are useful for event analytics, such as calculating views and purchases separately within the same grouping. Check which records qualify for each metric.

Do I need to understand MergeTree first?

MergeTree is a family of ClickHouse table engines that matters when studying data storage and query performance. You do not need to understand every storage detail before practicing basic analytical queries. Start with selections and aggregates, then explore table engines as the problems become more advanced.