Three Groupings in One Query
A dashboard needs a single result with three aggregations: event counts by event type, event counts by user, and a grand total across all events.
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: 174
Reset filtersA dashboard needs a single result with three aggregations: event counts by event type, event counts by user, and a grand total across all events.
The reporting team wants the number of events for each month and a separate "total for the period" row. Group events by the month of their ts timestamp and include a grand total across all months.
Marketing wants to measure M+1 retention: the percentage of users who return in the calendar month immediately after their first activity.
The CFO prefers Excel-style reports: products appear in rows, months in columns, and each cell contains total sales.
Display a sales report in a different layout: one row per month, with products as columns. For each month, calculate total sales separately for products A, B, and C.
A financial report needs a product-by-month table with an additional column for the quarterly total.
Marketing wants to understand how each product's revenue is distributed across months.
The data arrived in wide format: one row per product, with amounts for three months stored in separate columns m1, m2, and m3.
You're an analyst at Vexcom, a telecom operator. Over the years, each service plan has accumulated its own collection of features in a JSONB object: some keys hold numeric values for gigabytes and…
You're a backend engineer at ForkHouse, a restaurant group. A delivery aggregator retrieves menus through an API and requires exactly one row per restaurant, containing a JSON array of menu items…
You're a video streaming quality analyst at GlowPlay. At the end of each session, the player sends a JSONB array named events containing objects with an event type stored under the key type (play…
You're a data engineer at KeyNest, a real estate portal. Each listing has a JSONB document called doc containing details such as price, amenities, and a rooms array of room objects.
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.