SQL for data engineer interviews
Data engineering SQL is less about the report and more about whether the rows can be trusted: values typed three ways, amounts stored as text, duplicates, keys with no match, gaps in a sequence. This plan covers those in eight sessions.
Twenty-four exercises, three per session, on messy tables as well as clean ones. Several use PostgreSQL features such as generate_series, DISTINCT ON and string_agg, and the last session reads JSON payloads and compares two tables. The selection is ours, based on the kinds of questions data engineering roles lean on; it is not taken from any employer.
Session 1: Messy values and duplicates
- 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.
- 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.
- Keep one account per personFrom Sam in Support: "For everyone who signed up more than once with the same email typed differently, I only want their earliest signup. The signup dates come in mixed formats, and if two signups fall on the same day the lowest id wins. Send me one row per person." Return the id of the account to keep and the email in lowercase with no spaces around it.
Session 2: Numbers and dates stored as text
- Total revenue from text amountsFrom Priya in Finance: "The amount column is text, like $1,200.00 and ' 30.50'. What is our total revenue from paid payments? Count a payment as paid whatever the capitals or spaces in its status." Return one number, rounded to 2 decimals.
- 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.
- Days from signup to first paymentFrom Leo: "For each signup that has a paid payment, how many days passed between the signup and the first paid payment? Both dates come in mixed formats: a plain date, a date with a time, or a date with a T and a time." Return the signup id and the number of days as a whole number.
Session 3: Keys with no match
- Customers who never orderedReturn the names of customers who have no orders at all.
- Keep both sides with a full joinReturn every employee name with their department name. Keep employees without a department and also departments without employees; the missing side shows NULL. In the sample data every department has staff, but your query must still handle an empty department.
- Employees who manage nobodyReturn the names of employees who do not manage anyone: no other employee has them as manager.
Session 4: Keep one row per key
- Each customer's first orderFor each customer, return the customer_id and the id of their first order (earliest date, then lowest id). Count every order, whatever the status.
- 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.
- Rank within departmentReturn each employee's name, dept_id and their salary rank inside their own department, where 1 is the highest salary.
Session 5: Hierarchies and generated rows
- 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.
- 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.
- 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.
Session 6: Gaps and sequences
- Days until the next orderFor every order, return the order id and the number of days until the same customer's next order (by date, then id), as a whole number. A customer's last order shows NULL. Count every order, whatever the status. In PostgreSQL, subtracting one date from another gives the number of days.
- 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.
Session 7: Combining and checking rows
- Stack two lists with UNION ALLBuild one contact list: the name of every employee and every customer, with a second column that says employee or customer.
- Products on each order as one text (string_agg)For each order that has order lines, return the order_id and the names of its products as one text, separated by a comma and a space, in alphabetical order. A product listed twice on an order appears once.
- Customers whose every order is paid (bool_and)Return the names of customers who have at least one order and whose orders all have the status paid.
Session 8: JSON, arrays and comparing tables
- Read fields out of JSONThe payload column holds JSON with the keys type, order_id and amount at the top level. For every webhook whose type is exactly order.paid, return the order_id as a whole number and the amount as a number.
- One row per array elementSome payloads carry a JSON array called tags, and a tag appears at most once in a payload. Return every tag with the number of webhooks that carry it.
- Compare two tables (EXCEPT)customers_import is a fresh export of the customer list. Return the rows of customers_import that are new or changed: every row that is not in customers with exactly the same id, name, country and signup_date. Two empty (NULL) values count as the same. customers_import has one row per id.
Other plans and cases
- 14-day SQL interview sprint (41)
- SQL for data analyst interviews (24)
- SQL for BI analyst interviews (21)
- Product analytics SQL in four sessions (12)
- SQL traps and time periods in three sessions (10)
- Window functions in six sessions (18)
- SQL on messy data: one work week (16)
- PostgreSQL features in four sessions (11)
- SQL business case: the refund review (5)
- SQL business case: the payroll review (5)
- SQL business case: the monthly revenue report (4)
- SQL business case: customer value and segments (4)
- SQL business case: product performance (4)
- All exercises by level
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.