Reviewed by Aditya Kumar · Last reviewed 2026-08-08
To find each student's previous year's score, use the LAG window function with PARTITION BY student id and ORDER BY year . This calculates the score from the preceding row within each student's…
This medium-level SQL question appears frequently in data engineering interviews at companies like Deolite. While less common, it tests deeper understanding that distinguishes strong candidates. Mastering the underlying concepts (partition) will help you answer variations of this question confidently.
Break this problem into components. Identify the core trade-offs involved, then walk the interviewer through your reasoning step by step. Demonstrate awareness of edge cases and production considerations - this is what separates good answers from great ones. The expert answer includes a code example that demonstrates the implementation pattern.
To find each student's previous year's score, use the LAG window function with PARTITION BY student_id and ORDER BY year. This calculates the score from the preceding row within each student's ordered set of records.
The LAG function retrieves a value from a row at a given physical offset before the current row within its partition. Its full syntax is LAG(expression, offset, default).
* expression: The column whose value you want to retrieve (e.g., score).
* offset: An integer indicating how many rows back to look. The default is 1. To find the score from two years back, you would use LAG(score, 2).
* default: The value to return if the offset goes beyond the start of the partition. The default is NULL. This means the first record for each student will have a NULL prev_year_score. You can use COALESCE(LAG(score), 0) to replace NULL with 0, for example.
The OVER clause is crucial for defining the window:
PARTITION BY student_id: This divides the dataset into independent groups, ensuring the LAG calculation only considers scores for the same* student. Without this, LAG would look at the previous row in the entire dataset, potentially a different student.
* ORDER BY year: This defines the logical order within each student's partition, so "previous year" is correctly identified by the chronological sequence of years.
SELECT
student_id,
year,
score,
LAG(score, 1, 0) OVER (PARTITION BY student_id ORDER BY year) AS prev_year_score
FROM
student_scores;
Window functions, especially those with PARTITION BY on large datasets, can be computationally intensive. In distributed systems like Spark or Snowflake, they often require data to be grouped and sorted across nodes (known as a data shuffle), which can significantly impact query performance. Optimizing the PARTITION BY key and ensuring efficient data distribution are key considerations for performance.
Discuss related window functions like LEAD (for subsequent values), ROW_NUMBER, RANK, or NTH_VALUE to demonstrate a broader understanding of analytical functions and their applications in data engineering.
Red Flag: Missing ORDER BY in LAG—undefined order. Pro-Move: 'We used LAG(score, 1, 0) to default first year to 0 for YoY growth calculation.'
Some links below are affiliate links. If you buy through them we may earn a small commission at no extra cost to you — it helps keep DataEngPrep free.
According to DataEngPrep.tech, this is one of the most frequently asked SQL interview questions, reported at 1 company. DataEngPrep.tech maintains an editor-reviewed database of 1,863 data engineering interview questions across 7 categories.