HomeInterview QuestionsGiven a table with daily amounts, write a query or…

Given a table with daily amounts, write a query or script to calculate the increase in amount compared to the previous day's record and add a new column called 'Increase'.

🟡 Medium Coding Junior level
1Times asked
Aug 2026Last seen
Aug 2026First seen

💡 Model Answer

You can use the LAG window function to compare each row with the previous day's amount:

sql
SELECT
    Date,
    Amount,
    Amount - LAG(Amount) OVER (ORDER BY Date) AS Increase
FROM DailyAmounts
ORDER BY Date;

LAG(Amount) returns the amount from the previous row (ordered by Date). Subtracting it gives the daily increase. The first row will return NULL for Increase because there is no prior day. If you prefer a zero instead of NULL, wrap the expression with COALESCE:

sql
COALESCE(Amount - LAG(Amount) OVER (ORDER BY Date), 0) AS Increase

This approach works in most SQL dialects that support window functions, such as PostgreSQL, SQL Server, and BigQuery.

This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.

🎤 Get questions like this answered in real-time

Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.

Get Assisting AI — Starts at ₹500