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

Problems found: 272

Reset filters
  • MediumPremium
    PostgreSQL · ClickHouse

    Users with Multiple Orders

    The marketing team wants to find users who have placed more than one order and might qualify for a loyalty program.

  • MediumPremium
    PostgreSQL · ClickHouse

    Monthly Employee Salaries with Changing Hourly Rates

    Avito's finance team is calculating monthly payroll for contact center employees. Hourly rates can change within a month, so use the rate in effect on each workday.

    Avito
  • MediumPremium
    PostgreSQL · ClickHouse

    Average Time Between Orders per User

    At SberMarket, a product analyst is calculating a retention metric called average time between orders. The metric is used for triggered push notifications and win-back campaigns.

    SberMarket
  • MediumPremium
    PostgreSQL · ClickHouse

    VTB Client Ranks by Quarterly Transaction Volume

    VTB's CRM analytics team is building a quarterly client leaderboard for its rewards program. Include only non-refund transactions (is_refund = FALSE) in transaction volume.

    VTB
  • MediumPremium
    PostgreSQL · MySQL · ClickHouse

    Employees with Missing HR Information

    The HR system is identifying incomplete records so the team can correct the employee directory.

  • MediumPremium
    PostgreSQL · MySQL · ClickHouse

    Tour Departures Within a Time Window

    To check driver schedules, the tour coordinator needs a list of tour departures that left on January 15, 2024, between 10:00 and 14:00, inclusive of both times.

  • MediumPremium
    PostgreSQL · MySQL · ClickHouse

    Invalid IP Addresses in Logs

    The security team is cleaning up logs and looking for IP addresses with an invalid format. Find IP addresses that do not match the IPv4 pattern X.X.X.X, with 1–3 digits in each octet. Return ip.

  • MediumPremium
    PostgreSQL · MySQL · ClickHouse

    Rank Fans by Concerts Attended

    The band's loyalty program is ranking superfans by the number of concerts they have attended.

  • MediumPremium
    PostgreSQL · MySQL · ClickHouse

    Club Members' Expenses in 2005

    A book club wants to identify its most active readers in 2005 by their total borrowing expenses. For each club member, calculate the total cost of books they borrowed that year.

  • MediumPremium
    PostgreSQL · MySQL · ClickHouse

    DNA Samples Containing a Start Codon

    A lab is examining DNA sequences for the start codon ATG. Find the samples containing it and return sample_id, the sequence (dna_sequence), and start_pos—the position of the first occurrence of ATG…

  • MediumPremium
    PostgreSQL · MySQL · ClickHouse

    Distribution of Users by Number of Tweets

    Twitter is building a histogram of user activity for 2022. Group users by how many tweets they posted that year, using that number as tweet_bucket.

    Twitter (X)

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.