Active Businesses
Problem
Table: Events
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| business_id | int |
| event_type | varchar |
| occurrences | int |
+---------------+---------+
(business_id, event_type) is the primary key (combination of columns with unique values) of this table.
Each row in the table logs the info that an event of some type occurred at some business for a number of times.
Problem Definition
The average activity for a particular event_type is the average occurrences across all companies that have this event.
An active business is a business that has more than one event_type such that their occurrences is strictly greater than the average activity for that event.
Write a solution to find all active businesses.
Example
Output
Try It YourSelf
-- TODO: Write your user queries here
Solution
In tackling this challenge, our strategy involves two key steps using MySQL. First, within a subquery, we calculate the average occurrences for each event type by employing the AVG function over a window partitioned by event type. Subsequently, in the main query, we filter records to select only those where the occurrences surpass the calculated average.
The final step includes grouping the results by business_id and applying additional filters to ensure inclusion only for businesses with occurrences consistently exceeding the average across multiple events.
SELECT business_id FROM (SELECT *, Avg(occurrences) OVER( partition BY event_type) AS avgo FROM Events) x WHERE occurrences > avgo GROUP BY business_id HAVING Count(business_id) > 1
Let's break down the query step by step:
Step 1: Subquery (Alias x)
The subquery calculates the average occurrences for each event_type using the window function Avg(occurrences) OVER (PARTITION BY event_type) AS avgo. This calculates the average occurrences for each row based on the event_type.
SELECT *, Avg(occurrences) OVER (PARTITION BY event_type) AS avgo FROM events;
Output After Step 1:
+-------------+------------+-------------+------+ | business_id | event_type | occurrences | avgo | +-------------+------------+-------------+------+ | 1 | reviews | 7 | 5.0 | | 3 | reviews | 3 | 5.0 | | 1 | ads | 11 | 8.0 | | 2 | ads | 7 | 8.0 | | 3 | ads | 6 | 8.0 | | 1 | page views | 3 | 7.5 | | 2 | page views | 12 | 7.5 | +-------------+------------+-------------+------+
Step 2: Main Query
The main query filters rows where occurrences is greater than the calculated average (avgo) and groups the result by business_id. It also applies a HAVING clause to select only those business_id with a count greater than 1.
SELECT business_id FROM (subquery) x WHERE occurrences > avgo GROUP BY business_id HAVING count(business_id) > 1;
Final Output:
+-------------+ | business_id | +-------------+ | 1 | +-------------+
🤖 Don't fully get this? Learn it with Claude
Stuck on Active Businesses? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.
Build the mental picture, not memorization.
I just read a lesson on **Active Businesses** (Databases) and want to truly understand it. Explain Active Businesses from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Socratic — adapts to where you're stuck.
Teach me **Active Businesses** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Active recall exposes what you missed.
Quiz me on **Active Businesses** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Intuition + hook + flashcards for long-term memory.
Help me remember **Active Businesses** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.