Querydrill

14-day SQL interview sprint

Two weeks, three exercises a day, in the order a SQL interview tends to escalate: filters and NULLs, grouping, joins, subqueries, then the window function questions and the classics such as the second highest salary, the median and retention.

Each day takes about 20 to 30 minutes. Every answer is checked on the rows you see and again on changed data, so a query that only happens to work does not pass. Day 14 ends with the timed practice test.

0 of 41 solvedStart day 1

Day 1: Filters that say exactly what you mean

  • 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.
  • A date rangeReturn the id and order_date of every order placed in the first quarter of 2025: January 1 through March 31, both days included.

Day 2: NULL and CASE

  • No managerReturn the names of employees who have no manager.
  • Replace NULL with a defaultReturn each employee's name and dept_id. Where the dept_id is missing, show 0 instead of NULL.
  • Label with CASEReturn each order id with a label: large when the amount is 150 or more, otherwise small.

Day 3: GROUP BY and HAVING

  • 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.

Day 4: Conditional aggregation

  • Paid and refunded per customerFor each customer_id that has orders, return the customer_id, the number of paid orders and the number of refunded orders, in that order. A customer with no refunds shows 0.
  • Share of paid ordersWhat percentage of all orders have the status paid? Return a single number, rounded to 1 decimal.
  • 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.

Day 5: Inner joins

  • 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 productUsing order lines, return each product name with its revenue: quantity times price, summed over all its order lines. Products never sold do not appear.

Day 6: LEFT JOIN and what is missing

Day 7: Self joins

Day 8: Subqueries and CTEs

  • Above the average salaryReturn the names of employees who earn more than the average salary of all employees.
  • Big spendersCalculate each customer's total paid spend, then return the names of customers whose total is above the average of those totals. Only customers with at least one paid order count in the average.
  • Departments above averageReturn the dept_id and average salary of every department whose average salary is above the average salary of all employees. Ignore employees without a department when grouping.

Day 9: Ranking inside groups

  • Rank within departmentReturn each employee's name, dept_id and their salary rank inside their own department, where 1 is the highest salary.
  • 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.

Day 10: The nth highest questions

  • Second highest salaryReturn the second highest salary as a single value. If several employees share the top salary, the second highest is the next lower salary.
  • Third highest salaryReturn the third highest distinct salary as a single value.
  • Second highest salary per departmentFor each department with at least two different salaries, return the dept_id and its second highest salary. Ignore employees without a department.

Day 11: Running totals and the previous row

  • 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.
  • 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.

Day 12: Interview classics

  • Median salaryReturn the median salary of all employees as a single value: the middle salary when they are sorted, or the average of the two middle salaries when the number of employees is even.
  • 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.
  • Orders in consecutive monthsReturn the customer_id of every customer who placed orders in two calendar months in a row, for example March and April, whatever the status. Each customer appears once.

Day 13: The hard ones

  • Runs of the same statusGo through the orders by id and group consecutive orders that share the same status into runs. For each run, return the status, the first order id, the last order id and the number of orders in the run.
  • Cohort retentionGroup customers by the month of their first order, like 2025-01, counting every order whatever the status. For each such month return the month, the number of customers who first ordered in it, and how many of those customers ordered again in a later month.
  • Products bought togetherReturn every pair of products that appear together on the same order: the first product name, the second product name and the number of orders that contain both. List each pair once, with the lower product id first.

Day 14: Messy data, then the test

  • Find duplicate accountsFrom Sam in Support: "Some people signed up more than once with the same email, typed differently (capitals, extra spaces). List each email that has more than one account, in lowercase with no spaces, and how many accounts it has. Most accounts first, then alphabetically." Return email and accounts.
  • Monthly revenue from mixed datesFrom Priya again: "Same paid revenue, but by month please, oldest month first. Careful: paid_at comes in three formats." Return month as YYYY-MM and revenue rounded to 2 decimals.
  • 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.