The LAG function lets you look at the value from a previous row in the same result set without using a self-join. It helps compare current row data with previous row data easily.
Click to reveal answer
beginner
Write the basic syntax of the LAG function.
LAG(column_name, offset, default_value) OVER (PARTITION BY partition_column ORDER BY order_column)
Here, offset is how many rows back to look (default 1), and default_value is what to return if no previous row exists.
Click to reveal answer
intermediate
Why do we use the OVER clause with LAG?
The OVER clause defines how to group and order rows for the LAG function. It tells SQL which rows to compare and in what order, so it knows which previous row to look at.
Click to reveal answer
beginner
What happens if there is no previous row when using LAG?
If there is no previous row, LAG returns NULL by default or the specified default value if provided. This prevents errors and lets you handle edge cases.
Click to reveal answer
beginner
Give a real-life example where LAG is useful.
LAG is useful to compare sales of a product this month with last month. For example, you can find the difference in sales between current and previous month to see growth or decline.
Click to reveal answer
What does the LAG function return by default if there is no previous row?
ANULL
B0
CAn error
DThe current row value
✗ Incorrect
By default, LAG returns NULL if there is no previous row to look back to.
Which clause is required to specify the order of rows for the LAG function?
AGROUP BY
BORDER BY inside OVER()
CWHERE
DHAVING
✗ Incorrect
The ORDER BY clause inside the OVER() clause defines the order of rows for LAG to look back.
What is the default offset value for LAG if not specified?
A0
B2
C1
DNULL
✗ Incorrect
The default offset for LAG is 1, meaning it looks at the immediately previous row.
Which of these is a valid use of LAG?
ATo get the previous row's value
BTo filter rows
CTo sum all rows
DTo get the next row's value
✗ Incorrect
LAG is used to access the previous row's value in the result set.
Can LAG be used without the OVER() clause?
AOnly with GROUP BY
BYes, always
COnly in MySQL
DNo, OVER() is required
✗ Incorrect
LAG is a window function and requires the OVER() clause to define the window.
Explain how the LAG function works and why it is useful.
Think about comparing current data with past data in a list.
You got /4 concepts.
Describe the syntax of the LAG function and the role of each part.
Break down the function into its parameters and the window definition.
You got /4 concepts.
Practice
(1/5)
1. What does the SQL LAG function do?
easy
A. Returns the value from the next row in the result set
B. Calculates the sum of all previous rows
C. Returns the value from the previous row in the result set
D. Filters rows based on a condition
Solution
Step 1: Understand the purpose of LAG
The LAG function is used to access data from a previous row relative to the current row in a query result.
Step 2: Compare options with definition
Returns the value from the previous row in the result set correctly states that LAG returns the previous row's value. Other options describe different functions or behaviors.
Final Answer:
Returns the value from the previous row in the result set -> Option C
Quick Check:
LAG = previous row value [OK]
Hint: LAG looks backward one row by default [OK]
Common Mistakes:
Confusing LAG with LEAD (next row)
Thinking LAG sums values
Assuming LAG filters rows
2. Which of the following is the correct syntax to use LAG to get the previous salary ordered by employee ID?
easy
A. LAG(salary) OVER (ORDER BY employee_id)
B. LAG(salary) BY employee_id
C. LAG(salary) ORDER employee_id
D. LAG(salary) PARTITION BY employee_id
Solution
Step 1: Recall LAG syntax
LAG requires an OVER clause with ORDER BY to define the row order for previous row access.
Step 2: Match syntax with options
LAG(salary) OVER (ORDER BY employee_id) correctly uses OVER (ORDER BY employee_id). The other options misuse syntax or keywords.
Final Answer:
LAG(salary) OVER (ORDER BY employee_id) -> Option A
Quick Check:
LAG syntax = LAG() OVER (ORDER BY ...) [OK]
Hint: Always use OVER with ORDER BY for LAG [OK]
Common Mistakes:
Omitting OVER clause
Using PARTITION BY without ORDER BY
Incorrect keyword order
3. Given the table Employees with columns id and salary, what will this query return?
SELECT id, salary, LAG(salary) OVER (ORDER BY id) AS prev_salary FROM Employees;
LAG returns previous row's value or NULL if none [OK]
Hint: First row prev value is always NULL [OK]
Common Mistakes:
Assuming first row has previous value
Mixing up current and previous rows
Ignoring ORDER BY effect
4. Identify the error in this query:
SELECT id, salary, LAG(salary) ORDER BY id AS prev_salary FROM Employees;
medium
A. AS alias must come before LAG
B. ORDER BY cannot be used inside LAG
C. LAG cannot be used with salary column
D. Missing OVER() clause for LAG function
Solution
Step 1: Check LAG function syntax
LAG requires an OVER() clause to specify ordering. The query uses ORDER BY without OVER(), which is invalid.
Step 2: Confirm correct syntax
The correct syntax is LAG(salary) OVER (ORDER BY id). The query misses OVER(), causing a syntax error.
Final Answer:
Missing OVER() clause for LAG function -> Option D
Quick Check:
LAG needs OVER() with ORDER BY [OK]
Hint: LAG always needs OVER() clause [OK]
Common Mistakes:
Using ORDER BY without OVER()
Placing alias before function
Misunderstanding LAG usage
5. You want to find the difference between each employee's salary and the previous employee's salary ordered by id. Which query correctly calculates this difference?
hard
A. SELECT id, salary, LAG(salary) - salary OVER (ORDER BY id) AS diff FROM Employees;
B. SELECT id, salary, salary - LAG(salary) OVER (ORDER BY id) AS diff FROM Employees;
C. SELECT id, salary, salary - LAG(salary) FROM Employees ORDER BY id;
D. SELECT id, salary, salary - LAG(salary) PARTITION BY id AS diff FROM Employees;
Solution
Step 1: Understand the goal
We want to subtract the previous salary from the current salary, ordered by id.
Step 2: Check each option's correctness
SELECT id, salary, salary - LAG(salary) OVER (ORDER BY id) AS diff FROM Employees; correctly uses LAG(salary) OVER (ORDER BY id) and subtracts it from salary. SELECT id, salary, LAG(salary) - salary OVER (ORDER BY id) AS diff FROM Employees; reverses subtraction order. SELECT id, salary, salary - LAG(salary) FROM Employees ORDER BY id; misses OVER clause. SELECT id, salary, salary - LAG(salary) PARTITION BY id AS diff FROM Employees; misuses PARTITION BY without OVER.
Final Answer:
SELECT id, salary, salary - LAG(salary) OVER (ORDER BY id) AS diff FROM Employees; -> Option B
Quick Check:
Difference = current - previous salary using LAG [OK]
Hint: Use LAG with OVER and subtract current minus previous [OK]
Common Mistakes:
Forgetting OVER() clause
Subtracting in wrong order
Misusing PARTITION BY without OVER
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.