Querydrill

SQL for BI analyst interviews

BI work is metrics that have to be right every time the dashboard refreshes: totals per period, subtotals, share of total, change on the previous period and the top items in each group. This plan drills those patterns in seven sessions and ends with a product performance review.

Twenty-one exercises, three per session. Several use PostgreSQL features that show up in BI work, such as FILTER, ROLLUP and DISTINCT ON. The selection is ours, based on the kinds of questions BI roles lean on; it is not taken from any employer.

0 of 21 solvedStart session 1

Session 1: Metrics by period

  • Revenue by monthReturn each month as text like 2025-01 together with the total of its paid orders. In PostgreSQL, to_char(order_date, 'YYYY-MM') gives the month.
  • Paid revenue per quarterReturn each quarter as text like 2025-Q1 together with its paid revenue. January to March is Q1, April to June is Q2, and so on. In PostgreSQL, to_char(order_date, 'Q') gives the quarter number.
  • Orders per weekday (EXTRACT)Return the ISO weekday number (1 for Monday to 7 for Sunday) and the number of orders placed on that weekday, whatever the status. Only weekdays that have orders.

Session 2: Subtotals and conditional counts

  • Subtotals and a grand total (ROLLUP)Return the paid revenue per country and, in the same result, one extra row with the total over all countries. The total row shows NULL as the country.
  • Several counts in one pass (FILTER)For each customer_id that has orders, return the customer_id, the number of orders, the number of paid orders and the total amount of the paid orders, in that order. A customer with no paid orders shows 0 for both.
  • Orders per statusReturn each order status and how many orders have it.

Session 3: Share of total

  • Share of revenueFor each country, return the country and its share of total paid revenue in percent, rounded to 1 decimal.
  • Share of the customer's totalFor every paid order, return the id and its share of that customer's total paid amount in percent, rounded to 1 decimal. A customer with one paid order shows 100.
  • Cumulative share of revenueReturn each sold product with its revenue (quantity times price) and the cumulative share of total revenue in percent, rounded to 1 decimal, going from the product with the most revenue to the least. Order by revenue descending, then by product name. The last row shows 100.

Session 4: Period over period

  • Change from the previous orderFor every paid order, return the id and the difference between its amount and the amount of the previous paid order, ordered by order date then id. The first order shows NULL.
  • Month over monthReturn each month (like 2025-01), its paid revenue, and the change from the previous month's paid revenue. The first month shows NULL for the change.
  • Moving averageGo through the paid orders by date and then id. For each one, return the id and the average amount of that order and the two paid orders before it, rounded to 2 decimals. The first two rows average over fewer orders.

Session 5: Top N in each group

  • Top earner per departmentReturn the dept_id, name and salary of the highest-paid employee in each department. If several employees share the top salary in a department, return all of them. Ignore employees without a department.
  • Top two per categoryFor each category, return the category, product name and revenue (quantity times price) of its two best-selling products by revenue.
  • Latest order per customer (DISTINCT ON)For each customer, return the customer_id, the id and the amount of their most recent order (latest date, then highest id), whatever the status.

Session 6: Customer metrics

  • Orders per customerReturn every customer name with the number of orders they placed, including customers with 0 orders.
  • Most recent order per customerReturn every customer's name and the date of their most recent order, whatever the status. Customers who never ordered must still appear, with NULL as the date.
  • Repeat purchase rateOf all customers who placed at least one order, what percentage placed two or more orders, whatever the status? Return a single number rounded to 1 decimal.

Session 7: A product performance review

Other plans and cases

Your first 8 exercises are free, with no account, from any plan or level. Progress is kept in this browser; a free account keeps it on every device.