Interview Questions/SQL/Date Series Gap-Fill with Event Counts

Date Series Gap-Fill with Event Counts

Preview mode. Log in to edit, run, submit, and save progress.

Medium

Date Series Gap-Fill with Event Counts

You are given an Events table and a DateRange table with a single row containing a start and end date. Write a SQL query that generates every calendar date in [start_date, end_date] using a recursive CTE, and for each date reports the total event count, click count, and view count - even for dates with no events (showing 0s). Return (event_date, total_events, clicks, views) ordered by event_date. Table: Events

Column NameTypeDescription
event_idINTPrimary key
event_dateDATEDate of the event
event_typeVARCHAREither 'click' or 'view'

Table: DateRange

Column NameTypeDescription
start_dateDATERange start
end_dateDATERange end

Examples

Example 1

Generate the full date range with a recursive CTE. LEFT JOIN events to preserve gap dates. Use conditional aggregation to split clicks vs views.

Approach hint

Start with a simple approach, explain the trade-off, then move toward a cleaner or more scalable solution.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

query.sql