Querydrill

SQL interview practice problems

The questions that come up again and again in data analyst and data engineer interviews: second highest salary, top N per group, customers who never ordered, duplicates, running totals and month over month change.

Solve them here with a real PostgreSQL database in your browser. A typed-in answer that only fits the sample rows is rejected, the same way a good interviewer would reject it.

Try one here: Third highest salary

Try it yourself

Return the third highest distinct salary as a single value.

employees (11 rows)

idnamedept_idsalaryhire_datemanager_id
1Ava21300002019-03-15NULL
2Ben21050002020-07-011
3Cleo1820002021-01-107
4Dan1760002022-05-237
5Eve3540002022-09-1211
6Finn2980002021-11-301
7Gia1990002018-06-04NULL
8Hugo3610002020-02-18NULL
9Iris4880002019-10-07NULL
10Jack4720002023-04-179
11KaiNULL500002024-01-088

Free, no account needed. PostgreSQL runs in your browser and your answer is checked again on changed data.

Interview classics

  • 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.
  • Earns more than the managerReturn the names of employees who earn more than their own manager.
  • 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.
  • Salary against the department averageFor every employee who has a department, return the name and the difference between their salary and the average salary of their department, rounded to 2 decimals. Above the average is positive, below is negative.
  • Pivot the statusesReturn one row with three columns: the number of paid orders, refunded orders and pending orders, in that order.
  • 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.
  • 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.
  • 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.
  • 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.
  • First order not paidReturn the names of customers whose first order (earliest date, then lowest id) has a status other than paid.
  • A month with no sales still shows up (generate_series)Return every month from January to June 2025 as text like 2025-01, with its paid revenue. A month without paid orders must still appear, with 0.
  • Daily active usersReturn every day that has events, as a date, with the number of different users who were active on that day.
  • Monthly active usersReturn every month that has events, as text in the form YYYY-MM, with the number of different users who were active in that month.
  • Funnel: users at each stepReturn one row with three numbers: how many different users have a visit event, how many have a signup event and how many have a purchase event. Each step is counted on its own here; the order of the steps comes in the next exercise.
  • Signup to purchase conversionA user converted when they have a purchase that is later than their first signup. Return one row: the number of users who signed up, the number of those who converted, and the conversion rate as a percentage rounded to 1 decimal.
  • New and returning users per dayA user is new on the first day they have any event and returning on every later day they are active. Return every day that has events, as a date, with the number of new users and the number of returning users on that day.
  • Day-1 retentionA user is retained on day 1 when they have an event on the calendar day right after their first active day. Every user counts in the total, also a user whose first day is the last day in the data. Return one row: the number of users, the number retained on day 1, and the retention rate as a percentage rounded to 1 decimal.
  • Retention curveFor each number of days since a user's first active day, from 0 to 7, return that number and how many users were active that many days after their first day. Day 0 is the first day itself. Leave out day numbers on which nobody was active.
  • Sessions per user (30-minute rule)A new session starts with a user's first event, and again whenever more than 30 minutes have passed since that user's previous event. Return each user_id with their number of sessions.
  • Rolling 7-day active usersFor every day that has events, return the day, as a date, and the number of different users with an event in the 7 days ending on that day (that day and the 6 days before it).
  • Longest streak of active daysReturn each user_id with the length of their longest run of consecutive active days. A user who was never active two days in a row has a streak of 1.
  • Monthly churn rateA user is active on a day when one of their subscriptions started on or before that day and has no end date or an end date after that day. Return one row for March 2025: the number of users active on 2025-03-01, how many of them were no longer active on 2025-04-01, and that share as a percentage rounded to 1 decimal.
  • Reactivated subscribersThe end date is the first day without the subscription, so a new subscription that starts on the previous end date is a plan change with no gap. A reactivation is a subscription whose start date is later than the end date of the same user's previous subscription. A user's subscriptions never overlap. Return the user_id, the start date of each reactivation, and the gap in days: that start date minus the previous end date.
  • Merge bookings into occupied periodsFor each room, merge bookings that overlap or touch (one ends on the day the next starts) into one occupied period. Return the room, the first check_in and the last check_out of every period.
  • Peak number of bookings at onceA booking occupies its room from check_in up to, but not including, check_out. Return one number: the highest number of bookings that are in the hotel at the same time, over all rooms.

Read first if you are stuck

Other practice sets

Your first 8 exercises are free, with no account. Every answer is checked on the rows you see and again on changed data.