🎬No video for this question yet. The written answer is below.
Window functions? Explain CTEs ,- Write the UPSERT / MERGE query,
- Write SQL Code for finding duplicates.
📝 Answer
Interviewer:
Can you explain Window Functions in SQL?
Candidate:
So basically, window functions are something I use a lot when working with analytical queries.
Let me explain in a simple way.
Normally, when we use GROUP BY, it aggregates data and reduces the number of rows. But window functions allow us to perform calculations across a set of rows without reducing the row count.
So each row still remains, but we can calculate things like rank, running total, or previous values.
In my project, I used window functions for deduplication and ranking.
For example, when I had multiple records for the same customer, I used ROW_NUMBER with partition by customer_id and ordered by last_updated timestamp to pick the latest record.
So basically, window functions help in performing advanced analytics without losing row-level detail.
SELECT
customer_id,
order_amount,
ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date DESC) AS rn
FROM orders;
Interviewer:
Can you explain CTEs (Common Table Expressions)?
Candidate:
So basically, CTE stands for Common Table Expression.
I usually think of it as a temporary result set that we define at the beginning of a query and then reuse.
Instead of writing complex nested queries, I use CTEs to make the query more readable and maintainable.
In one of my projects, I had a complex query with multiple joins and filters. It was very difficult to understand.
So I broke it down using CTEs. Each step had its own CTE, and then I combined them.
This made debugging much easier.
So basically, CTEs improve readability and help structure complex queries.
WITH filtered_data AS (
SELECT * FROM orders WHERE order_amount > 1000
)
SELECT * FROM filtered_data;
Interviewer:
Can you write an UPSERT or MERGE query?
Candidate:
So basically, UPSERT means insert if the record does not exist, otherwise update.
In my ETL pipeline, I used MERGE statements for incremental loading into data warehouse tables.
Let me show an example.
MERGE INTO target_table t
USING source_table s
ON t.id = s.id
WHEN MATCHED THEN
UPDATE SET t.name = s.name, t.updated_at = CURRENT_TIMESTAMP
WHEN NOT MATCHED THEN
INSERT (id, name, updated_at)
VALUES (s.id, s.name, CURRENT_TIMESTAMP);
So basically, this ensures that existing records are updated and new records are inserted.
Interviewer:
How do you find duplicates in SQL?
Candidate:
So basically, duplicate detection is a common requirement in ETL.
First, I use GROUP BY with HAVING.
SELECT email, COUNT(*) FROM users GROUP BY email HAVING COUNT(*) > 1;
This gives duplicate values.
In my project, I also used ROW_NUMBER to identify and remove duplicates.
SELECT *
FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY email ORDER BY created_at DESC) AS rn
FROM users
) t
WHERE rn > 1;
So basically, I identify duplicates and then remove older records.
Interviewer (Follow-up):
Good. Can you explain when you would prefer CTE over subqueries in real-time scenarios?