User product array (ClickHouse)
For each user_id, build an alphabetically sorted array of the distinct product names in column products. Order by user_id. Use groupUniqArray/groupArray + arraySort.
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 problemFor each user_id, build an alphabetically sorted array of the distinct product names in column products. Order by user_id. Use groupUniqArray/groupArray + arraySort.
From the events table (columns product_id, user_id, event_type), for each product_id compute unique_users — the number of distinct user_id over all events — and unique_buyers — the number of distinct…
From the events table (columns product_id, event_type), for each product_id compute views (count of 'view'), purchases (count of 'purchase'), and conversion = purchases / views rounded to 4 decimals…
From the events table (columns product_id, event_type), output only products that have at least one 'purchase' event, together with the purchase count. Return product_id, purchases.
From the events table (columns product_id, user_id), for each product_id compute total_events — the total number of events — and events_per_user = total_events / number of distinct user_id, rounded…
For each day (bucket toStartOfDay(ts)) of the events table, count the number of views (views, where event_type = 'view') and purchases (purchases, where event_type = 'purchase').
Group events by the numeric date key toYYYYMMDD(ts) and, for each day, count the number of distinct user_id. Return columns ymd (date as YYYYMMDD) and users. Order by ymd ascending.
Group events by day (toStartOfDay(ts)), count the events in each day and return a single row — the day with the most events. Columns day and events. On ties pick the earlier day.
For each day (toStartOfDay(ts)) of the events table, count the number of purchases (purchases, where event_type = 'purchase') and the number of distinct product_id involved (products).
From sales(id, user_id, product, amount, ts) count the number of DISTINCT products purchased by each user. Group by user_id and return user_id and distinct_products (count of unique product values).
From sales(id, user_id, product, amount, ts) find users whose average purchase amount is greater than 100.
From sales(id, user_id, product, amount, ts) compute total revenue per product and keep only products whose revenue is at least 100.
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.
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.
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.
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.
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.
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.
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.
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.
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.