SQL and ETL interview preparation
Interviews for data engineering, ETL testing, data quality and analytics roles usually include a SQL round and a conversation about data warehouses and pipelines. The questions repeat in patterns, and it is much easier to prepare for patterns than for individual questions. This guide covers the SQL techniques that come up most often, with worked examples that you can run on any common database, followed by warehouse and pipeline concepts and a few testing-specific topics.
Set up a practice schema
Use these two tables to try every query below. Syntax differs slightly between databases (Oracle, PostgreSQL, SQL Server, Snowflake), so adjust function names where needed.
employees(emp_id, name, dept_id, salary, manager_id, hire_date)
departments(dept_id, dept_name)
orders(order_id, customer_id, order_date, amount, status)
Pattern 1: The nth highest value
A classic question is "find the second highest salary". There are several correct answers, and a good candidate can explain the trade-offs.
-- Window function: handles ties with DENSE_RANK
SELECT DISTINCT salary
FROM (
SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rnk
FROM employees
) t
WHERE rnk = 2;
-- Subquery without a window function
SELECT MAX(salary) FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);
Explain that RANK leaves gaps after ties, DENSE_RANK does not, and ROW_NUMBER gives a unique number to each row and so would skip a tied value. Mention what should happen if there is no second value.
Pattern 2: Finding duplicates and removing them
-- Which customers appear more than once for the same order date?
SELECT customer_id, order_date, COUNT(*) AS cnt
FROM orders
GROUP BY customer_id, order_date
HAVING COUNT(*) > 1;
-- Keep one row per group, using ROW_NUMBER
SELECT *
FROM (
SELECT o.*, ROW_NUMBER() OVER (PARTITION BY order_id ORDER BY order_date DESC) AS rn
FROM orders o
) t
WHERE rn = 1;
For data testers, duplicates are a favourite topic. Be ready to explain how you would check for duplicates on a business key, how you would decide which duplicate to keep, and how to confirm that a pipeline does not create them on a re-run.
Pattern 3: Joins and what each one returns
Know the difference between inner, left, right and full outer joins, and be able to say what each returns when keys do not match. A common follow-up is to find rows in one table that have no match in another:
-- Employees with no department record
SELECT e.emp_id, e.name
FROM employees e
LEFT JOIN departments d ON d.dept_id = e.dept_id
WHERE d.dept_id IS NULL;
Remember that a condition on the right-hand table placed in the WHERE clause can turn a left join into an inner join, and that NULL never equals NULL in a comparison.
Pattern 4: Aggregation by group and filtering groups
-- Departments whose average salary is above 60000
SELECT dept_id, AVG(salary) AS avg_salary
FROM employees
GROUP BY dept_id
HAVING AVG(salary) > 60000;
State clearly that WHERE filters rows before grouping and HAVING filters groups after it.
Pattern 5: Top n per group
-- Top 3 earners in each department
SELECT dept_id, name, salary
FROM (
SELECT dept_id, name, salary,
ROW_NUMBER() OVER (PARTITION BY dept_id ORDER BY salary DESC) AS rn
FROM employees
) t
WHERE rn <= 3;
Pattern 6: Running totals and comparisons with the previous row
SELECT order_date,
amount,
SUM(amount) OVER (ORDER BY order_date) AS running_total,
LAG(amount) OVER (ORDER BY order_date) AS previous_amount
FROM orders;
Window functions are tested heavily. Be able to explain PARTITION BY, ORDER BY inside a window, and the default frame that decides which rows a running total includes.
Pattern 7: Hierarchies and recursion
-- All reports under a manager (recursive CTE)
WITH RECURSIVE team AS (
SELECT emp_id, name, manager_id FROM employees WHERE emp_id = 100
UNION ALL
SELECT e.emp_id, e.name, e.manager_id
FROM employees e JOIN team t ON e.manager_id = t.emp_id
)
SELECT * FROM team;
Oracle uses CONNECT BY, and SQL Server omits the word RECURSIVE. Mention that you would check for cycles.
Pattern 8: Data validation queries
For ETL and QA roles, interviewers want to see how you prove that data moved correctly. A basic reconciliation approach compares counts, sums and sample rows:
-- Rows in source but missing in target
SELECT * FROM source_table
EXCEPT -- MINUS in Oracle
SELECT * FROM target_table;
-- Compare row counts and a numeric checksum
SELECT COUNT(*), SUM(amount) FROM source_table;
SELECT COUNT(*), SUM(amount) FROM target_table;
Go beyond counts: check nulls, data types, truncation, rounding, time zones, character encoding, and boundary values. Explain how you would test a transformation rule from the specification, including negative cases.
Data warehouse concepts to know
- Fact and dimension tables. Facts hold measurable events (orders, payments), and dimensions hold descriptive attributes (customer, product, date).
- Star and snowflake schemas. A star schema keeps dimensions denormalised and is simpler to query. A snowflake schema normalises dimensions further to reduce redundancy.
- Slowly changing dimensions. Type 1 overwrites the old value. Type 2 keeps history by adding a new row with effective dates or a current flag. Type 3 keeps a limited history in extra columns. Be ready to describe how you would test a Type 2 load.
- Full and incremental loads. A full load replaces everything, while an incremental load processes only new or changed records using a watermark or change data capture. Discuss how you would handle late-arriving data and deletes.
- Surrogate keys, grain and slowly growing tables. State the grain of a fact table clearly, because many reconciliation problems come from a mismatch in grain.
Pipeline and platform topics
- Orchestration. Know how a tool such as Apache Airflow defines tasks as a directed acyclic graph, retries failed tasks, and handles dependencies and backfills.
- Cloud storage and query. For AWS-based roles, understand how data lands in object storage such as S3, is catalogued, and is queried by services such as Athena, or transformed by jobs in Glue.
- Distributed processing. For PySpark, know the difference between transformations and actions, why shuffles are expensive, and how partitioning affects performance.
- Idempotency. A pipeline that gives the same result when it is run twice is much easier to operate. Explain how you would design loads so that a re-run does not create duplicates.
- Performance. Be ready to talk about indexes, partition pruning, avoiding functions on indexed columns in filters, reading an execution plan and reducing the data scanned.
Testing-specific questions
- How would you build a test strategy for a new data pipeline?
- How do you test data transformations when the business logic is complex or documented poorly?
- What would you automate in data testing, and what would you leave manual?
- How do you handle test data that must resemble production without exposing personal information?
- Describe the most difficult data defect you found, how you found it and what changed afterwards.
How to practise
- Create the tables above in a free local database and write each query from memory.
- Change the data to include duplicates, nulls and ties, and check that your queries still behave correctly.
- Explain each solution aloud as though to an interviewer, including the alternatives and their limits.
- Time yourself. Many interview problems expect a working query in ten to fifteen minutes.
- Prepare two stories from real projects about data quality, using the structure in Prepare for an interview with evidence.
If you are updating your resume for these roles, see the resume guide for testers, QA and data engineers.