Are you preparing for a Data Engineer interview and struggling with real-world SQL problems?
SQL is one of the most important skills every Data Engineer must master. Companies expect candidates to understand more than basic SELECT, INSERT, UPDATE, and DELETE statements. They want developers who can solve complex data problems, optimize queries, handle duplicate records, and build reliable ETL pipelines.
In this article, we cover 25 real-time SQL interview scenarios for Data Engineers, including practical SQL queries, solutions, explanations, and industry use cases.
Whether you are a fresher or have 1–5 years of experience, these SQL interview questions and answers will help you prepare for technical interviews at companies using SQL Server, PostgreSQL, Snowflake, Azure SQL, Databricks, and other database platforms.
Why Is SQL Important for Data Engineers?
Data Engineers work with large volumes of structured and semi-structured data. SQL helps them extract, transform, validate, and load data into databases and data warehouses.
Some important responsibilities include:
- Writing complex SQL queries.
- Building ETL and ELT pipelines.
- Identifying and removing duplicate records.
- Performing data quality checks.
- Creating analytical reports.
- Optimizing slow-running queries.
- Managing incremental data loads.
- Working with large datasets.
Let’s explore the most important SQL scenario-based interview questions.
25 SQL Interview Scenarios with Solutions
1. Find the Second Highest Salary
Question: Find the second-highest distinct salary from an Employee table.
SELECT MAX(salary)
FROM Employee
WHERE salary < (
SELECT MAX(salary)
FROM Employee
);
Explanation: The inner query finds the highest salary. The outer query finds the maximum salary below it.
Concept: Subqueries and aggregate functions.
2. Find Duplicate Records
Question: Identify duplicate customer email addresses.
SELECT email, COUNT(*) AS total
FROM Customers
GROUP BY email
HAVING COUNT(*) > 1;
Explanation: GROUP BY groups identical emails, and HAVING identifies those appearing more than once.
Concept: GROUP BY and HAVING.
3. Remove Duplicate Records
Question: Keep the latest customer record based on updated_at.
WITH CTE AS (
SELECT *,
ROW_NUMBER() OVER (
PARTITION BY email
ORDER BY updated_at DESC, customer_id DESC
) AS rn
FROM Customers
)
SELECT *
FROM CTE
WHERE rn = 1;
Explanation: ROW_NUMBER identifies the latest record for each email. This SELECT returns the records to retain. You can use a database-specific DELETE statement to remove the others.
Concept: ROW_NUMBER and data deduplication.
4. Find Employees Earning More Than Their Managers
SELECT
e.emp_name,
e.salary,
m.emp_name AS manager_name
FROM Employee e
JOIN Employee m
ON e.manager_id = m.emp_id
WHERE e.salary > m.salary;
Explanation: A self-join compares employee salaries with their managers’ salaries.
Concept: Self JOIN.
5. Find Top 3 Salaries in Each Department
WITH CTE AS (
SELECT *,
DENSE_RANK() OVER (
PARTITION BY department_id
ORDER BY salary DESC
) AS rnk
FROM Employee
)
SELECT *
FROM CTE
WHERE rnk <= 3;
Explanation: DENSE_RANK ranks salaries separately within each department.
Concept: Window functions.
6. Find Customers Who Never Placed an Order
SELECT c.customer_id, c.customer_name
FROM Customers c
LEFT JOIN Orders o
ON c.customer_id = o.customer_id
WHERE o.order_id IS NULL;
Explanation: LEFT JOIN identifies customers without matching orders.
Concept: LEFT JOIN and NULL handling.
7. Calculate Running Total of Sales
SELECT
order_date,
sales_amount,
SUM(sales_amount) OVER (
ORDER BY order_date, order_id
ROWS BETWEEN UNBOUNDED PRECEDING
AND CURRENT ROW
) AS running_total
FROM Orders;
Explanation: SUM with a window function calculates cumulative sales.
Concept: Running totals.
8. Find the Latest Record for Each Customer
WITH CTE AS (
SELECT *,
ROW_NUMBER() OVER (
PARTITION BY customer_id
ORDER BY updated_at DESC, record_id DESC
) AS rn
FROM CustomerHistory
)
SELECT *
FROM CTE
WHERE rn = 1;
Explanation: Returns the most recently updated record for each customer.
Concept: ROW_NUMBER and partitioning.
9. Calculate Month-over-Month Revenue Growth
WITH MonthlySales AS (
SELECT
YEAR(order_date) AS sales_year,
MONTH(order_date) AS sales_month,
SUM(sales_amount) AS revenue
FROM Orders
GROUP BY YEAR(order_date), MONTH(order_date)
),
Comparison AS (
SELECT *,
LAG(revenue) OVER (
ORDER BY sales_year, sales_month
) AS previous_revenue
FROM MonthlySales
)
SELECT *,
100.0 * (revenue - previous_revenue)
/ NULLIF(previous_revenue, 0) AS growth_percentage
FROM Comparison;
Explanation: LAG retrieves the previous month’s revenue for comparison.
Concept: LAG and analytical functions.
10. Find Consecutive Login Days
WITH LoginGroups AS (
SELECT
user_id,
login_date,
DATEADD(
DAY,
-ROW_NUMBER() OVER (
PARTITION BY user_id
ORDER BY login_date
),
login_date
) AS grp
FROM (
SELECT DISTINCT user_id, login_date
FROM UserLogins
) d
)
SELECT user_id, COUNT(*) AS consecutive_days
FROM LoginGroups
GROUP BY user_id, grp
HAVING COUNT(*) >= 3;
Explanation: The date grouping technique identifies consecutive login dates. This example uses SQL Server syntax.
Concept: Advanced window functions.
11. Identify Missing Dates
SELECT c.calendar_date
FROM Calendar c
LEFT JOIN Orders o
ON c.calendar_date = CAST(o.order_date AS DATE)
WHERE o.order_id IS NULL;
Explanation: A calendar table helps identify dates without transactions.
Concept: Data completeness validation.
12. Calculate a Rolling 7-Day Average
SELECT
sales_date,
daily_sales,
AVG(daily_sales) OVER (
ORDER BY sales_date
ROWS BETWEEN 6 PRECEDING AND CURRENT ROW
) AS rolling_average
FROM DailySales;
Explanation: Calculates the average over the current and previous six rows. Use a calendar table to guarantee seven calendar days when dates are missing.
Concept: Window frames and analytical SQL.
13. Find First and Last Orders for Each Customer
SELECT
customer_id,
MIN(order_date) AS first_order,
MAX(order_date) AS last_order
FROM Orders
GROUP BY customer_id;
Explanation: MIN and MAX identify the first and most recent purchase dates.
Concept: Aggregate functions.
14. Calculate Customer Retention
SELECT COUNT(DISTINCT j.customer_id)
FROM JanuaryCustomers j
INNER JOIN FebruaryCustomers f
ON j.customer_id = f.customer_id;
Explanation: Identifies customers who purchased in both periods.
Concept: Customer analytics and joins.
15. Find Employees with the Same Salary
SELECT salary, COUNT(*) AS employee_count
FROM Employee
GROUP BY salary
HAVING COUNT(*) > 1;
Explanation: Identifies salaries shared by multiple employees.
Concept: Grouping and aggregation.
16. Split Full Name into First and Last Name
SELECT
full_name,
LEFT(full_name, CHARINDEX(' ', full_name + ' ') - 1)
AS first_name
FROM Customers;
Explanation: Extracts the first word from a full name in SQL Server.
Concept: String functions.
17. Convert Rows into Columns
SELECT
month,
SUM(CASE WHEN category = 'Electronics'
THEN amount ELSE 0 END) AS electronics,
SUM(CASE WHEN category = 'Clothing'
THEN amount ELSE 0 END) AS clothing
FROM Sales
GROUP BY month;
Explanation: Conditional aggregation converts category values into separate columns.
Concept: CASE WHEN and pivoting.
18. Find Products with No Sales
SELECT p.product_id, p.product_name
FROM Products p
LEFT JOIN Orders o
ON p.product_id = o.product_id
WHERE o.product_id IS NULL;
Explanation: Finds products without matching sales records.
Concept: LEFT JOIN.
19. Calculate Cumulative Revenue Percentage
WITH Revenue AS (
SELECT
product_id,
SUM(sales_amount) AS total_revenue
FROM Orders
GROUP BY product_id
)
SELECT *,
100.0 * total_revenue /
NULLIF(SUM(total_revenue) OVER (), 0)
AS revenue_percentage
FROM Revenue;
Explanation: Calculates each product’s contribution to total revenue.
Concept: Window aggregates and business analytics.
20. Find Missing Transaction IDs
SELECT t1.transaction_id + 1 AS missing_id
FROM Transactions t1
LEFT JOIN Transactions t2
ON t1.transaction_id + 1 = t2.transaction_id
WHERE t2.transaction_id IS NULL
AND t1.transaction_id < (
SELECT MAX(transaction_id)
FROM Transactions
);
Explanation: Identifies missing IDs between existing transaction records.
Concept: Data validation.
21. Find Customers with More Than Three Orders
SELECT
customer_id,
COUNT(order_id) AS total_orders
FROM Orders
GROUP BY customer_id
HAVING COUNT(order_id) > 3;
Explanation: Finds customers who placed more than three orders.
Concept: Aggregation and HAVING.
22. Compare Current and Previous Records
SELECT
customer_id,
status,
LAG(status) OVER (
PARTITION BY customer_id
ORDER BY updated_at, record_id
) AS previous_status
FROM CustomerHistory;
Explanation: LAG compares the current customer status with the previous status.
Concept: Change Data Capture and history tracking.
23. Perform Insert and Update Using MERGE
MERGE INTO TargetCustomers AS target
USING SourceCustomers AS source
ON target.customer_id = source.customer_id
WHEN MATCHED THEN
UPDATE SET
target.email = source.email
WHEN NOT MATCHED BY TARGET THEN
INSERT (customer_id, email)
VALUES (source.customer_id, source.email);
Explanation: MERGE performs insert and update operations based on matching keys. This is SQL Server-style syntax.
Concept: Incremental ETL loading.
24. Replace NULL Values with Zero
SELECT
emp_name,
salary,
COALESCE(bonus, 0) AS bonus,
salary + COALESCE(bonus, 0) AS total_salary
FROM Employee;
Explanation: COALESCE replaces NULL values with zero.
Concept: NULL handling and data cleaning.
25. Optimize a Slow SQL Query
Original Query:
SELECT *
FROM Orders
WHERE YEAR(order_date) = 2026;
Optimized Query:
SELECT
order_id,
customer_id,
order_date,
sales_amount
FROM Orders
WHERE order_date >= '2026-01-01'
AND order_date < '2027-01-01';
Explanation: Using a date range instead of applying YEAR() to the filtered column can improve index usage.
Concept: SQL performance optimization.
Important SQL Topics for Data Engineer Interviews
| Topic | Important concepts |
|---|---|
| SQL Joins | INNER, LEFT, RIGHT, FULL |
| Window Functions | ROW_NUMBER, RANK, DENSE_RANK |
| Analytical Functions | LAG, LEAD |
| CTEs | Common Table Expressions |
| Aggregations | SUM, COUNT, AVG |
| Subqueries | Nested and correlated |
| Data Cleaning | Duplicates and NULL |
| ETL | Incremental loading and MERGE |
| Optimization | Indexing and execution plans |
| Data Warehousing | Fact and dimension tables |
How to Prepare for Data Engineer SQL Interviews
Follow this four-week SQL preparation roadmap.
Week 1: SQL Fundamentals
- SELECT, WHERE, ORDER BY
- GROUP BY and HAVING
- Joins and subqueries
Week 2: Advanced SQL
- Window functions
- CTEs
- Ranking and analytical queries
Week 3: Real-Time SQL Scenarios
- Duplicate records
- Data quality checks
- Incremental loading
- ETL transformations
Week 4: Interview Practice
- Solve 5–10 SQL problems daily.
- Practice writing queries without help.
- Explain your SQL logic.
- Work with real datasets.
Frequently Asked Questions
1. What are the most important SQL interview questions for Data Engineers?
Joins, window functions, CTEs, duplicate removal, running totals, incremental loading, and query optimization are important SQL interview topics.
2. Is SQL enough to become a Data Engineer?
SQL is essential, but Data Engineers should also learn Python, PySpark, ETL tools, data warehousing, cloud platforms, and data pipeline development.
3. Which SQL database should I learn for Data Engineering?
Start with SQL Server or PostgreSQL. Then explore Databricks SQL, Snowflake, or cloud data warehouse platforms.
4. How can I practice real-time SQL interview scenarios?
Use sample datasets, solve business problems, practice window functions, and build small ETL projects.
5. Are SQL scenario-based questions important for experienced Data Engineers?
Yes. Experienced candidates are often expected to solve complex data transformation, performance, data quality, and incremental loading problems.
Final Thoughts
Learning SQL is not about memorizing hundreds of queries. It is about understanding business problems and converting them into efficient SQL solutions.
Practice these 25 real-time SQL interview scenarios for Data Engineers, understand the logic behind every query, and build confidence before your next technical interview.
Keep practicing. Keep learning. Keep growing!
Follow TechInterviewTitans for more SQL interview questions, Data Engineering tutorials, Azure Data Engineer preparation, Databricks guides, and PySpark interview scenarios.
Recommended Next Reads:
- 100 SQL Interview Questions and Answers
- Top 100 PySpark Interview Questions
- Azure Data Engineer Interview Questions
- Databricks Interview Questions and Answers
- Real-Time ETL Pipeline Scenarios
- Advanced SQL Query Optimization Guide
Tags: SQL Interview Questions, Data Engineer, SQL Scenarios, Advanced SQL, ETL, SQL Queries, Azure Data Engineer, Databricks, Data Engineering
🎯 DATA ENGINEER INTERVIEW PACKS
👉 1–4 YOE Data Engineer Pack
https://techinterviewtitans.com/product/top-100-real-data-engineer-interview-questions-answers-2025-edition-1-4-years-experience/
👉 4–8 YOE Data Engineer Pack
https://techinterviewtitans.com/product/100-real-data-engineer-interview-questions-answers-2025-edition-for-4-8-years-of-experience/
👉 Azure Data Engineer Interview Guide 2026
https://techinterviewtitans.com/product/crack-azure-data-engineer-interviews-2026-topic-wise-questions-real-scenario-based-answers/
👉 600 Real Data Engineer Interview Questions — Top Companies
https://techinterviewtitans.com/product/600-real-data-engineer-interview-questions-answers-2025-edition-from-top-tech-companies-ey-infosys-tcs-dell-wipro-more/
👉 Azure Data Engineer Company-Wise Pack — 500 Questions
https://techinterviewtitans.com/product/crack-azure-data-engineer-interviews-2026-500-company-wise-questions-real-scenarios-expert-answers/
👉 34-Day Data Engineer Interview Preparation Journey
https://techinterviewtitans.com/product/crack-the-data-engineer-interview-real-questions-real-struggles-the-complete-34-day-preparation-journey-2026/
👉 Data Engineer Mega Pack — 1300+ Real-Time Scenario Q&As
https://techinterviewtitans.com/product/data-engineer-mega-interview-pack-2025-1300-real-time-scenario-qas-azure-adf-databricks-delta-lake-pyspark-sql-data-warehouse/
🔥 Azure Data Factory (ADF) Mastery Guide 2026
https://techinterviewtitans.com/product/azure-data-factory-adf-mastery-guide-2026/
🔥 ETL & ELT Complete Guide 2026 — 86 Modules
Data Engineering Notes + Interview Questions
https://techinterviewtitans.com/product/etl-elt-complete-guide-2026-86-modules-data-engineering-notes-interview-questions/
🔥 Azure SQL Complete Notes 2026 — 68 Topics
https://techinterviewtitans.com/product/azure-sql-complete-notes-2026-68-topics-interview-questions-data-engineer-guide/




