r/learnSQL • • Sep 14 '26

If you have SQL interviews, do not ignore these small things! (Part 9)

Yesterday I took a SQL interview and tried something different this time: evaluating AI-generated code rather than writing from scratch.

I didn't ask the candidate to write a query from scratch.

Instead, I gave him an SQL AI Agent.

The SQL AI Agent is coded in such a way, it gives three solution to one problem statement.

Here is the problem statement given:

We have a purchases table:

customer_idpurchase_dateamount
1012026-01-050
1012026-01-1050
1022026-02-01100
1022026-02-0190
1032026-03-010
1032026-03-0525

Find each customer's first purchase where they actually paid something.

Below are the 3 responses provided by Agent:

Agent's Solution 1:

SELECT customer_id, purchase_date, amount 
FROM ( 
    SELECT *, ROW_NUMBER() OVER ( PARTITION BY customer_id ORDER BY purchase_date ) rn 
    FROM purchases 
) t 
WHERE rn = 1 AND amount > 0;

Agent's Solution 2:

SELECT customer_id, purchase_date, amount 
FROM ( 
    SELECT *, ROW_NUMBER() OVER ( PARTITION BY customer_id ORDER BY purchase_date ) rn 
    FROM purchases WHERE amount > 0 
) t 
WHERE rn = 1;

Agent's Solution 3:

SELECT customer_id, purchase_date, amount 
FROM ( 
    SELECT *, ROW_NUMBER() OVER ( PARTITION BY customer_id ORDER BY purchase_date, amount DESC ) rn 
    FROM purchases WHERE amount > 0 
) t 
WHERE rn = 1;

Which solution would you approve, and what potential flaws/edge cases do you see in each? Please do comment, we can learn together!

Don't just ask "Does this SQL work?"

Ask:

Will this still give the right answer when the data gets messy?

If you want to test yourself on more SQL problems like this, I usually practice by actually writing the queries instead of just looking at solutions

25 Upvotes

24 comments sorted by

View all comments

1

u/MobileUser21 Sep 15 '26 edited Sep 15 '26

In Today’s Thread: people here still don’t know basics of SQL or how to read and answer questions.

Anyway…

Give the sample dataset and options, I’d pick solution 3.

The problem with solution 1 is that it has a non deterministic windows function and the row number is happening prior to filtering out amounts greater than 0.

Solution 2 is filtering out the amounts greater than 0 first before ordering the amounts by customer_id but it still contains a non deterministic windows function so still would not choose that.

I’d choose solution 3 because the amount in the ORDER BY acts as a tie breaker for customers that made purchases on the same day. The windows function is now deterministic and returns the same result each time.

Will this give the correct answer if data gets messy? Well it depends… for customers that didn’t have multiple purchases the same day yes, but for customers that had multiple purchases the same day, the query will return the highest amount.

Unfortunately real life data is messy, this isn’t that uncommon of a data problem. Explain to the stakeholders the nuances and solution you’ve come up with. Most likely they’ll just agree and roll with it.

1

u/Yavuz_Selim Sep 15 '26

Why would you need a tie breaker? The ROW_NUMBER already tie breaks for you.

What is the advantage of having a deterministic solution if it returns the wrong answer - because the question simply cannot be answered correctly. Talking with the stakeholders and agreeing on a solutions without adjusting the table definition is an answer to a different question.

 

Find each customer's first purchase where they actually paid something.

This question cannot be answered with a table like this.

1

u/MobileUser21 Sep 15 '26

Was altering the table one of the solutions offered?

1

u/MobileUser21 Sep 15 '26

The point of this exercise is during an interview, to critically think about the solutions offered and pick which one gets you closest to the answer you want and explain the nuances.

If saying it’s impossible is your answer, fine. Be one of the many resumes that gets thrown in the trash. I do it all the time to candidates who don’t make a good faith effort to answer questions but instead just throw their hands up.

1

u/Yavuz_Selim Sep 15 '26

I will not make any effort to give an in-depth answer in an AI post like this. Are you mad?

During an interview, I would of course explain what I think of each query and how to attack the problem and find a solution. I am not going to say "impossible to answer" and then just stop talking.

1

u/MobileUser21 Sep 15 '26

Saying “Impossible” and just stop talking would be pretty damn funny though lol