Querydrill

SQL NULL handling practice problems

NULL means unknown, and it behaves unlike any value: it is never equal to anything, it drops out of COUNT(column) and AVG, and it makes NOT IN return nothing. Many wrong reports come from one unhandled NULL.

These problems use IS NULL, COALESCE and NULLIF on data that has gaps in it, including the hidden copy, where the gaps are in different rows.

Try one here: No manager

Try it yourself

Return the names of employees who have no manager.

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.

Basics

Aggregation

  • Average salary per departmentFor each dept_id, return the dept_id and the average salary. Leave out employees without a department.
  • 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.

Joins

  • Customers who never orderedReturn the names of customers who have no orders at all.
  • Products never soldReturn the names of products that appear on no order line.
  • Payroll per departmentReturn every department name with the total salary of its employees. Departments with no employees must show 0, not NULL.
  • Revenue inflated by a join (fan-out)For each customer with at least one paid order (status paid), return the name, the total amount of their paid orders and the total quantity of items on those orders. An order without item lines counts with quantity 0. Customer names are unique.

Advanced

  • 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.
  • Levels in the reporting chainReturn each employee's name and their level in the reporting chain: 1 for employees without a manager, 2 for the people who report to them, 3 for the level below that, and so on.
  • Year-over-year growthFor every month of 2025 that is in the table, return the month, its revenue, the revenue of the same month one year earlier, and the growth as a percentage rounded to 1 decimal. When there is no figure for the same month a year earlier, the last two columns are NULL. When that figure is 0, the growth is NULL.

Interview classics

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

Work mode

  • Clean up the country reportFrom Marta in Marketing: "Our country report shows US, us and ' Us' as three different countries. Send me each country once, in capitals with no spaces around it, with the number of signups. Leave out signups that have no country." Return two columns: country and signups.
  • Signups per planFrom Leo in Product: "How many signups are on each plan? Write the plan names in lowercase, and count signups without a plan as unknown." Return plan and signups.
  • Revenue per planFrom Leo in Product: "What is our paid revenue per plan? Plan names arrive in any capitals, and some signups have no plan: call those unknown. Amounts are text, and a payment is paid whatever the capitals or spaces in its status." Return plan and revenue rounded to 2 decimals.

Case studies

  • Refund review, step 4: refunded value per categoryIs it a product problem? For every product category, return the category and the value of its order lines (quantity times price) on refunded orders. A category with nothing refunded must still appear, with 0.
  • Refund review, step 5: the summary lineClose the review with the line that goes at the top of the email. Return one row: the number of orders, the number of refunded orders, the refund rate in percent rounded to 1 decimal, the refunded amount, and the refunded amount as a percentage of the paid amount, rounded to 1 decimal.
  • Payroll review, step 1: headcount and payroll per departmentFinance is preparing next year's budget and asks for the people numbers. Start with the base table: for every department, return the department name, the number of employees and the total salary. A department with no employees must still appear, with 0 and 0.
  • Payroll review, step 3: the top earner's shareHow concentrated is the pay? For each department, return the department name, the name of its highest-paid employee and that salary as a percentage of the department's total salary, rounded to 1 decimal. If several employees share the top salary, return each of them.

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.