Querydrill

SQL for data analyst interviews

A data analyst is mostly asked to turn a table into an answer: how many, how much, which group, compared with last month. This plan follows that work in eight sessions, from filters and grouped totals through joins and missing rows to ranking, running totals and a monthly report built step by step.

Twenty-four exercises, three per session, about 20 to 30 minutes each time. Every answer is checked on the rows you see and again on changed data. The selection is ours, based on the kinds of questions analyst roles lean on; it is not taken from any employer.

0 of 24 solvedStart session 1

Session 1: Filter and tidy

  • Filter rowsReturn the name and salary of employees who earn more than 90,000.
  • Combine AND with ORReturn the id, amount and status of every order that is either refunded (any amount), or paid with an amount of 200 or more.
  • Label with CASEReturn each order id with a label: large when the amount is 150 or more, otherwise small.

Session 2: Counts and totals by group

  • Customers per countryFor each country, return the country and how many customers it has.
  • Spend per customerFor each customer_id, return the customer_id and the total amount of their paid orders. Customers without paid orders do not appear.
  • Repeat buyersReturn the customer_id of every customer who placed more than two orders, whatever the status.

Session 3: Percentages and bands

  • Share of paid ordersWhat percentage of all orders have the status paid? Return a single number, rounded to 1 decimal.
  • Salary bandsPut every employee in a salary band: junior below 70,000, mid from 70,000 up to but not including 100,000, senior at 100,000 or more. Return each band name and how many employees are in it.
  • Customers who orderedHow many different customers have placed at least one order? Return a single number.

Session 4: Grouping by dates

  • 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.
  • Paid revenue per week (date_trunc)Return the first day (Monday) of each week that has paid orders, as a date, together with the paid revenue of that week.

Session 5: Joins for reporting

  • Orders with customer namesReturn one row per order: the order id, the customer name and the amount.
  • Revenue per countryReturn each country with the total of its customers' paid orders. Countries without paid orders do not appear.
  • Revenue per categoryReturn each product category with its revenue from order lines (quantity times price).

Session 6: What is missing

Session 7: Ranking, running totals and month over month

  • Rank within departmentReturn each employee's name, dept_id and their salary rank inside their own department, where 1 is the highest salary.
  • Running revenue totalFor every paid order, return the order id and the running total of paid revenue, ordered by order date and then by id.
  • 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.

Session 8: A report from start to finish

  • Revenue report, step 1: revenue per monthYou are building the monthly revenue report. Start with the base: for each month that has paid orders, return the month as YYYY-MM and the total paid amount, in columns month and revenue.
  • Revenue report, step 2: change on the previous monthExtend the monthly report. For each month return month, revenue, the previous month's revenue as prev_revenue, and the difference as change (revenue minus prev_revenue). The first month has no previous month, so both extra columns are NULL there.
  • Revenue report, step 3: running totalAdd a year-to-date column. For each month return month, revenue and running_total: the sum of paid revenue from the first month up to and including that month.
  • Take the 15-minute practice testTen scored questions against the clock, with a breakdown of what to repeat.

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.