Rank Each Organization's Most Viewed Workbooks Over Thirty Days

Last updated: November 29, 2026

Quick Overview

Given a `workbook_views(view_id, workbook_id, org_id, user_id, viewed_at)` table, return the top 3 workbooks per organization by distinct viewers in the 30 days before a given date, keeping ties.

Sigma Computing
Data Manipulation (SQL/Python)
Data Engineer
Sigma Computing
November 29, 2026
Data Engineer
Phone Screen
Data Manipulation (SQL/Python)
Easy

0

0

0 solved


Given a `workbook_views(view_id, workbook_id, org_id, user_id, viewed_at)` table, return the top 3 workbooks per organization by distinct viewers in the 30 days before a given date, keeping ties.

Sigma's own usage analytics, and the admin usage dashboards customers rely on, are built from event tables like this one, and Sigma-style SQL tends to be Snowflake-flavored. The screen checks distinct counting, window ranking per partition, and a precise date window.

What the Interviewer Expects
  • Count distinct users, not raw view rows
  • Use a half-open date window so the boundary day is counted once
  • Rank within each org with a window function partitioned by org
  • State how ties are handled and pick RANK, DENSE_RANK or ROW_NUMBER on purpose
  • Order the output deterministically
Key Topics to Cover
window functions
RANK
COUNT DISTINCT
date filtering
Snowflake SQL
How to Approach This
  1. Clarify the schema and expected output format before writing queries.
  2. Use CTEs (WITH clauses) to break complex queries into readable steps.
  3. Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
  4. Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
  5. For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
  • How would the result differ with DENSE_RANK or ROW_NUMBER when three workbooks tie for first?
  • How would you exclude views by the workbook's own author?
  • If this table has 10 billion rows, how would you keep the query cheap in the warehouse?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice SQL Problems
Sample Answer
Clarify the Data

One row per view event; a user can view a workbook many times. org_id is the workbook's org. "Last 30 days" means [as_of - 30 days, as_of). Ties: if several workbooks share third place, show all o...

Approach

First aggregate to one row per (org_id, workbook_id) with COUNT(DISTINCT user_id). Then apply RANK() partitioned by org, ordered by viewers descending, and keep rank at most 3. RANK leaves gaps ...


Submit Your Answer
Markdown supported

Related Questions