Error percentage per service, ranked (ClickHouse)
From table logs, for each service compute the error percentage error_pct = 100 * the share of rows with level = 'ERROR', rounded to 2 decimals. Output columns service, error_pct.
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 problemProblems found: 200
Reset filtersFrom table logs, for each service compute the error percentage error_pct = 100 * the share of rows with level = 'ERROR', rounded to 2 decimals. Output columns service, error_pct.
From table logs, for each service count the number of rows of each level: info (level = 'INFO'), warn (level = 'WARN') and error (level = 'ERROR'). Use countIf.
From logs(service, latency_ms) compute the median (50th percentile) latency for each service. Return columns service and median_latency. Use an exact computation. Order by service ascending.
From logs(service, latency_ms) compute the 95th percentile of latency for each service. Return columns service and p95_latency. Use an exact percentile computation. Order by service ascending.
In payments, for each user_id compute revenue — the sum of amount over rows with status = 'success'. Keep only users with revenue > 0 (use HAVING). Use sumIf.
In payments, for each method compute: success_revenue = sum of amount where status = 'success', success_cnt = number of successful transactions, and avg_ticket = success_revenue / success_cnt rounded…
In payments, for each method compute total = count(), failed = rows with status = 'failed', and failed_share = failed / total rounded to 4 decimals. Use countIf.
The revenue management team wants to know how long guests stay in different room categories on average. This information affects housekeeping schedules, room occupancy planning, and pricing.
The payroll department is calculating employees' net salaries.
The retail team is preparing a sales pivot table by day of the week.
The finance team is analyzing how user spending varies by platform.
The analytics dashboard needs three event counts in a single row for each user. In one query, count each user's events of type view, purchase, and refund in separate columns.
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.