r/learnSQL • u/thequerylab • 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_id | purchase_date | amount |
|---|---|---|
| 101 | 2026-01-05 | 0 |
| 101 | 2026-01-10 | 50 |
| 102 | 2026-02-01 | 100 |
| 102 | 2026-02-01 | 90 |
| 103 | 2026-03-01 | 0 |
| 103 | 2026-03-05 | 25 |
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
8
u/Yavuz_Selim Sep 14 '26 edited Sep 14 '26
Impossible to answer.
All three answers are incorrect. Because of row 3 and 4.
Solution 1 and 3 are the wrongest.
And (purchase_)id is mising
Just ask your AI what's wrong with it.
3
u/thetwoweeks Sep 14 '26
There's a far simpler solution to this. Concerning that AI didn't provide it.
1
u/TheMagarity Sep 14 '26
If that's the way you worded the prompt then that's the problem. First, don't assume the ai understands abstractions such as "the customer actually paid for something" is the same as "the amount is greater than zero".
Secondly, always give it some examples. It will check against those examples. For this, give sample output rows for customers 101 and 102.
Do those two things and I guarantee very different ai outputs.
1
u/NastyPastyLucas Sep 14 '26
Why not use a CTE or a lateral join even?
Very few people who rely on AI output are critical of any of it, and a query of this length should be writable by any applicant.
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/Geraldtanwx1990 Sep 15 '26
I would guess using a cte to get the 0 out first then do a groupby is easier?
Sorry am learning too. What would be the correct way?
0
u/[deleted] Sep 14 '26
[deleted] — view deleted comment