← Meta Interview Insights

Meta·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

SQL question from a Meta SWE interview, filtering categories by author diversity then ranking customers by purchase volume. Pretty clean problem once you break it into steps, though the nested aggregation part is where things get tricky.

Questions Asked (1)

Q1

Given a Purchases table (CustomerId, BookId, Category) and a Books table (BookId, Author), find the top 3 customers by number of books purchased, but only count purchases from categories that have books by at least 3 distinct authors.

Algorithms & Data StructuresData Modeling
Author's notes

The filtering step tripped me up a bit.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Break the problem into two parts: first identify categories that have books by at least 3 distinct authors, then count purchases only from those categories and rank customers by purchase count. Use a subquery or CTE to filter categories, then join with Purchases and aggregate by CustomerId, finally order and limit to top 3.

Pro tip: Clarify whether 'top 3 customers' should include ties or if a strict limit of 3 is required, and mention that you'd handle ties by either using RANK/DENSE_RANK or by discussing with the interviewer. Also, consider if the same book purchased multiple times should count multiple times—usually it should, but confirm.

1. Identify qualifying categories

Write a subquery to find categories where the number of distinct authors (from Books) is at least 3. Join Books with Purchases on BookId to get authors per category, then group by Category and filter with HAVING COUNT(DISTINCT Author) >= 3.

2. Filter purchases to qualifying categories

Use the result from step 1 to filter the Purchases table, keeping only rows where Category is in the qualifying set. This can be done with an IN clause or a join.

3. Count purchases per customer

Group the filtered Purchases by CustomerId and count the number of purchases (e.g., COUNT(*) or COUNT(BookId)). This gives the total qualifying purchases per customer.

4. Rank and select top 3

Order the aggregated results by purchase count descending and limit to 3. If ties are a concern, use a window function like RANK() or DENSE_RANK() and then filter for rank <= 3.

5. Validate and discuss edge cases

Mention potential edge cases: customers with zero qualifying purchases, ties at the boundary, and whether to include customers with no purchases. Also, consider performance implications and indexing.

Key Points to Mention

  • Use of COUNT(DISTINCT Author) to ensure at least 3 distinct authors per category.
  • Subquery or CTE to pre-filter categories before joining with Purchases for efficiency.
  • Aggregation with GROUP BY CustomerId and COUNT to get purchase counts.
  • Handling ties with window functions (RANK/DENSE_RANK) if needed.
  • Discussion of whether to count multiple purchases of the same book (usually yes).
  • Consideration of NULLs or missing data in joins (e.g., books not in Books table).

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.