By role
SQL interview questions for analytics engineers
Analytics engineer screens go past the query to the model: checking grain, deduplicating reloads, building type 2 history, joining facts to the dimension row that was true at the time, and filling gaps with date spines.
What to get right
- Prove the grain with a GROUP BY ... HAVING count(*) > 1 before you trust a join.
- Join type 2 tables on the time range, not just the key.
- Build a date spine when every day must appear, even days with no rows.
Analytics engineering Read the lesson
- A small data quality test suite as one queryThe data team runs these four checks on the HR tables every night.
- Current gold customers by region from type 2 historyThe loyalty team wants today's gold customers by region. dim_customers_history has one row per version of a customer.
- Data test for direct buyers with no usable emailThe shop's receipts are sent by email, so every customer who orders through the web or app channel must have an email.
- Grain check for a user by day purchase modelAn analytics model fct_user_day is meant to have one row per user per calendar day (UTC), built from purchases.
- Order copies that arrived more than 12 hours lateData quality wants to know which rows reach the warehouse late.
- Active accounts and seats per day from subscription datesThe finance dashboard shows, for each day, how many accounts had an active subscription and how many seats they paid for.
- Agent by month fact table of tickets created and resolvedSupport leaders want a monthly scorecard at the grain one row per agent per calendar month, for February to June 2025.
- Customer tier at a point in time from type 2 historyA campaign was sent at 2025-03-15 12:00:00 (UTC) and marketing wants to know which tier each customer was in at that moment, not today.
- Latest copy of each order loaded after a watermarkAn incremental model processes only new rows from stg_orders. The last run finished with the watermark 2025-06-15 00:00:00.
- Monthly recurring revenue by plan with empty monthsFinance reports monthly recurring revenue (MRR) as a snapshot on the first day of each month.
- One row per savings account per month, with zero fillBuild a monthly fact table for savings accounts at the grain one row per account per calendar month, for January to June 2025.
- Reconcile active subscriptions against February invoicesBilling wants to check that every account that should have been invoiced in February 2025 was, and that nobody was invoiced without a subscription.
- Salary validity ranges built with LEADThe HR warehouse needs a type 2 style table from the salary_changes log, so analysts can join pay to any date. Build it for employees of the Finance department.
- Turn stock snapshots into restock eventsThe warehouse stores a snapshot of stock per product at the end of each day. Operations wants an event table of restocks instead.
- Campaign by day fact table with cumulative spendMarketing wants a daily table at the grain one row per campaign per day it was live, for 2025-03-10 to 2025-03-16 inclusive.
- Daily end-of-day balance from transactions with a date spineThe bank wants a daily balance table for the savings accounts of its premium customers.
- Daily stock levels with a date spine and carry forwardThe planning dashboard needs a stock figure for every product on every day, even though some end-of-day snapshots are missing.
- Delivered revenue in USD with as-of exchange ratesFinance reports delivered revenue in US dollars by the month orders were placed. Build it from the raw order load.
- Merge unchanged versions in a type 2 historydim_customers_history has one row per version of a customer, but some versions changed only a column we do not track.
- Orders by the customer's tier when the order was placedMarketing wants to know how orders split by the tier the customer had when they ordered, not the tier they have today.
Deduplication and latest row Read the lesson
- Customers with several accounts under one emailCustomer support wants to merge accounts that belong to the same person.
- Find exact duplicate rows in a raw order loadThe pipeline sometimes loads the same copy of an order twice.
- How many rows would deduplication remove?Before building the order model, the team plans to keep exactly one row for each order_id and updated_at pair in stg_orders and delete the rest.
- Emails shared by accounts that disagree on countryBefore merging duplicate customer accounts, the data team wants to find the ones that cannot be merged automatically: the same email is used by several...
- Events fired twice within ten secondsThe app sometimes sends the same event twice when a button is tapped quickly. The analytics team wants to know how many events to drop.
- Keep the first purchase event for each web userGrowth wants one row per user for their first purchase, to build a "first order" table. Use only users whose platform is web.
- Orders by current status from a raw loadstg_orders holds every copy of every order our pipeline has loaded.
- Repeat tickets from the same customerSupport managers count one problem once.
- Salary in effect on a date for each employeeFinance is rebuilding the mid-year headcount cost report.
- Current order copy when two copies share a timestampOperations wants the list of orders that are shipped but not yet delivered, taken from the raw load in stg_orders.
Dates and time Read the lesson
- Customers by tier on a past dateA report needs the customer mix as it stood at 2025-05-01 00:00:00. dim_customers_history keeps one row per version of a customer.
- Subscriptions active on a given dateFinance asks for a snapshot of paying seats on 2025-03-15. A subscription is active on a date when it started on or before that date and has not ended by then.
- Live campaign days with no deliveryAd ops suspects some campaigns stopped serving on days they were supposed to be live.
- Month-end balance by account typeFinance reports the combined balance of each account type at the end of every month, from January to June 2025.
- Seats on the last day of each monthFinance reports month-end paying seats.
Joins Read the lesson
- Refund value of wrong-item returns by categoryCustomer care is costing out "wrong item" returns. Look at rows in returns whose reason is 'wrong_item'.
- Invoiced amount by the plan active on the issue dateInvoices do not store a plan. To attribute revenue to plans, link each invoice to the subscription its account had on the invoice's issued_on day.
- Premium customers, accounts and money inRelationship managers review every customer in the 'premium' segment. A customer can have several accounts, and each account has many transactions.
- Spending by merchant partner group, NULL merchants includedPartnerships keeps a small lookup of merchants and the group each belongs to.
- Tracks and plays per jazz artist without double countingThe label relations team reviews every artist whose genre is 'jazz'. For each one they want the size of the catalog and how much it is played.
- Units ordered and units returned per orderOperations reviews every completed order placed in August 2024 (from 2024-08-01 00:00 up to, but not including, 2024-09-01 00:00) for returns.
- Open accounts at each month end, by account typeFinance reports the number of open accounts on the last day of every month, split by account type.
- Purchases after assignment in the one-tap checkout testThe growth team reads out the one_tap_checkout experiment. Everyone assigned to it is in experiment_assignments with their variant and assigned_at time.
- Salary in effect at review time, by ratingThe people team asks whether higher ratings go with higher pay.
- Spend and sign-ups per campaign from two sourcesThe marketing report builds one row per campaign from two separate sources: total spend from ad_daily, and the number of sign-ups credited to the campaign...
CTEs and subqueries Read the lesson
- Everyone who reports to a manager, at any depthWen Becker (employee_id 2) is leaving on sabbatical and the people team needs everyone in their organisation to hear about the cover plan.
- Reporting chain from an employee up to the CEOAn approval request from employee 33 needs to be routed up the reporting line.
- Headcount by level below the CEOThe people team wants to know how deep the reporting line goes. The CEO is the one employee with no manager_id.
Gaps and islands Read the lesson
- Missing inventory snapshot ranges per SKUThe warehouse takes one inventory snapshot per product per day, but some days were skipped.
- Listening sessions split by a 30-minute gapThe product team defines a listening session from play timestamps alone.
- Merge subscription rows into continuous customer stretchessubscriptions has one row per plan period, and a plan change creates a new row.
More for this role
- Customers whose every account is closedGroup and aggregate
- Plan stretches and billing for accounts that changed planGroup and aggregate
- Units sold and units returned by categoryGroup and aggregate
- Pay ladder with RANK and DENSE_RANK side by sideRanking and top N
- Each customer's largest single expense, with tie-breakersRanking and top N
- Second highest salary in each departmentRanking and top N
- The third longest completed trip in each cityRanking and top N
- Favourite spending category per customer, counted by segmentRanking and top N
- Each day's rate next to the month-end rateRunning totals, LAG and LEAD
- The invoice that takes an account past 5,000 billedRunning totals, LAG and LEAD
- Events in the 10 minutes before each purchaseRunning totals, LAG and LEAD
- Rolling seven-day spend when days are missingRunning totals, LAG and LEAD
- Return rate by product categoryRatios and percentages
- Revenue per account, including non-payersRatios and percentages
- Active subscriptions by seat band and planConditional aggregation
- On-time, late and unpaid invoices by monthConditional aggregation
- First-response SLA by agentConditional aggregation
- Customer contact labels with missing emailsNULL handling
- Tickets per agent, including unassigned onesNULL handling
- Riders who got a different driver than last timeNULL handling
- New and churned seats by monthPeriod over period
- Year-to-date units against the same period last yearPeriod over period
- Raises of 9.5 percent or more over the previous salarySelf joins
- Accounts billed since May with no paid invoiceRows with no match
- Month-over-month net revenue retentionCohorts and retention
- Monthly subscription churn rateCohorts and retention
- Purchase funnel inside a single sessionFunnels
- Median listen length, continuous and discreteMedians and percentiles
- Median dining spend by month, without percentile functionsMedians and percentiles
- Names shared by several drivers and ridersText and set operations