Table: Orders
+-------------+------+ | Column Name | Type | +-------------+------+ | minute | int | | order_count | int | +-------------+------+ minute is the primary key for this table. Each row of this table contains the minute and number of orders received during that specific minute. The total number of rows will be a multiple of 6.
Write a query to calculate total orders within each interval. Each interval is defined as a combination of 6 minutes.
1 to 6 fall within interval 1, while minutes 7 to 12 belong to interval 2, and so forth.Return the result table ordered by interval_no in ascending order.
The result format is in the following example.
Example 1:
Input: Orders table: +--------+-------------+ | minute | order_count | +--------+-------------+ | 1 | 0 | | 2 | 2 | | 3 | 4 | | 4 | 6 | | 5 | 1 | | 6 | 4 | | 7 | 1 | | 8 | 2 | | 9 | 4 | | 10 | 1 | | 11 | 4 | | 12 | 6 | +--------+-------------+ Output: +-------------+--------------+ | interval_no | total_orders | +-------------+--------------+ | 1 | 17 | | 2 | 18 | +-------------+--------------+ Explanation: - Interval number 1 comprises minutes from 1 to 6. The total orders in these six minutes are (0 + 2 + 4 + 6 + 1 + 4) = 17. - Interval number 2 comprises minutes from 7 to 12. The total orders in these six minutes are (1 + 2 + 4 + 1 + 4 + 6) = 18. Returning table orderd by interval_no in ascending order.
Problem Overview: You are given a table of time intervals and another table containing order timestamps. For each interval, compute how many orders occurred between its start and end time. The result should return the interval along with the total number of orders that fall inside that range.
Approach 1: Correlated Subquery Count (O(n * m) time, O(1) space)
The most direct approach runs a correlated subquery for every interval. For each row in the intervals table, execute a SELECT COUNT(*) on the orders table where the order timestamp falls between start_time and end_time. This works because each interval independently checks all orders. The downside is performance: if there are n intervals and m orders, the database may scan the orders table repeatedly. This method is easy to write but does not scale well for large datasets.
Approach 2: Range Join with Aggregation (O(n + m) to O(n log m) depending on indexing, O(1) extra space)
The efficient solution performs a LEFT JOIN between the intervals table and the orders table using a range condition such as orders.order_time BETWEEN intervals.start_time AND intervals.end_time. After joining, group the results by the interval identifier and use COUNT(order_id) to compute how many orders fall in that range. Because the database can leverage indexes on the order timestamp, it avoids repeated scans and evaluates the condition efficiently. This pattern is a classic range join problem in SQL and frequently appears in analytics-style queries.
Using a LEFT JOIN ensures intervals with zero orders still appear in the result. The GROUP BY aggregation then collapses matching rows into a single count per interval. Most relational databases optimize this pattern well when indexes exist on the timestamp column.
Recommended for interviews: The range join with aggregation is the expected approach. It demonstrates correct use of database querying patterns such as conditional joins, grouping, and counting. Mentioning the correlated subquery approach shows baseline understanding, but using a join with GROUP BY shows stronger SQL skills and familiarity with scalable JOIN operations.
MySQL
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Correlated Subquery Count | O(n * m) | O(1) | Small datasets or quick prototypes where query simplicity matters more than performance |
| Range Join with GROUP BY | O(n + m) to O(n log m) | O(1) | Production queries or interview solutions where indexed joins efficiently count orders per interval |
Leetcode MEDIUM 2893 - Orders Within Each Interval - Using CEIL in SQL | Everyday Data Science • Everyday Data Science • 481 views views
Practice Calculate Orders Within Each Interval with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor