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

  • EasyFree
    ClickHouse

    Row counts by status code (ClickHouse)

    From table logs, count the number of rows cnt for each status_code. Output columns status_code, cnt. Order by status_code.

  • EasyFree
    ClickHouse

    Average latency per service

    From logs(service, latency_ms) compute the mean request latency for each service. Return columns service and avg_latency (the average of latency_ms rounded to 2 decimals). Order by service ascending.

  • EasyFree
    ClickHouse

    Number of distinct pages per user

    From sessions(user_id, page), count the number of distinct pages each user_id visited. Output columns user_id and distinct_pages. Order by distinct_pages descending, then by user_id ascending.

  • EasyFree
    PostgreSQL · MySQL · ClickHouse

    Tour Trip Schedule

    Before the season begins, the team needs a single timeline of all tour trips: where each trip departs from, where it arrives, and its departure and arrival times.

  • EasyFree
    PostgreSQL · MySQL · ClickHouse

    Doctors Who Saw Patients in Neurology Wards

    The head of neurology is preparing a list of colleagues to recognize. The list should include doctors who have seen patients in neurology wards at least once.

  • EasyFree
    PostgreSQL · MySQL · ClickHouse

    Properties in Moscow

    The real estate team is preparing an overview of the Moscow market. Return all columns for properties located in the city of Moscow.

    Domclick
  • EasyFree
    PostgreSQL · MySQL · ClickHouse

    Patients Born in May

    The hospital's HR department is preparing May birthday greetings and compiling a list of patients whose birthdays fall in that month.

  • EasyFree
    PostgreSQL · MySQL · ClickHouse

    Wi-Fi Availability in Cabins

    Each cabin listing needs a clear Wi-Fi indicator: YES when internet access is available and NO when it is not. For each cabin, return its ID and the corresponding string.

  • EasyFree
    PostgreSQL · MySQL · ClickHouse

    Patients in Alphabetical Order

    A hospital receptionist is preparing a printed patient directory. Return the first and last name of every patient. Sort the list by last name, then by first name, both in ascending order.

  • EasyFree
    PostgreSQL · MySQL · ClickHouse

    Rock Artists in the Festival Database

    A talent booker is planning a series of rock festivals and putting together a shortlist of artists who fit the genre. Return the name and country of each artist whose genre is Rock.

  • EasyFree
    PostgreSQL · MySQL · ClickHouse

    Number of Festivals in 2024

    The annual reporting team is reviewing 2024 and wants a quick measure of the festival program’s size. Count the festivals in the database whose year is 2024.

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.