Spare Data Center Capacity
The Microsoft Azure team is evaluating unused data center capacity. For each data center, calculate spare capacity as monthly_capacity − monthly_demand. If demand data is missing, treat demand as 0.
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 problemThe Microsoft Azure team is evaluating unused data center capacity. For each data center, calculate spare capacity as monthly_capacity − monthly_demand. If demand data is missing, treat demand as 0.
The academic office is reviewing students' workloads to find those studying several subjects. Each row in StudyPatterns represents one study session, recording a student, a subject, and a date.
The platform team is looking for "zombie sessions" — sessions that last more than a day and were probably not closed properly. Find sessions whose duration is greater than 24 hours.
Find all authors who viewed at least one of their own articles. Return each author’s identifier (author_id) in a result column named id. Sort the result in ascending order.
Find the names of customers who were not referred by the customer with id = 2. Handle NULL values correctly: customers without a referrer must also be included.
An HR analyst is preparing a report on team composition. For each employee, return employee_id and the number of employees on that employee's team in a column named team_size.
The delivery department is preparing a contact list with addresses. For every person in the Person table, return their first and last names, along with their city and state from the Address table.
Count the followers of each user. Return user_id and followers_count. Sort the result by user_id in ascending order.
A casting agency wants to find established partnerships between actors and directors who regularly work together.
Find all email addresses that appear in the table more than once.
The HR team is compiling employee work hours. An employee may enter and leave the office multiple times in a single day, so you need to add up all their time intervals in the office.
Find customers who have never placed an order. Return their names in a result column named customers.
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.
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.
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.
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.
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.
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.
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.
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.
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.