LAG() lets you look at a value from a previous row in the same result set without using a self-join. It's like looking back one step in a list.
Click to reveal answer
beginner
How does the LEAD() function differ from LAG()?
LEAD() looks forward to the next row's value, while LAG() looks backward to the previous row's value. Both help compare rows easily.
Click to reveal answer
intermediate
What is a common use case for LAG and LEAD functions?
They are often used to compare current row values with previous or next rows, like finding changes over time or calculating differences between rows.
Click to reveal answer
intermediate
Write a simple SQL snippet using LAG() to get the previous month's sales for each row.
SELECT month, sales, LAG(sales) OVER (ORDER BY month) AS prev_month_sales FROM sales_data;
Click to reveal answer
beginner
What happens if there is no previous row when using LAG()?
LAG() returns NULL if there is no previous row to look at, so you can handle missing data gracefully.
Click to reveal answer
What does LEAD() function return?
AValue from the previous row
BCurrent row value
CSum of all rows
DValue from the next row
✗ Incorrect
LEAD() returns the value from the next row in the result set.
Which clause is required to use LAG() or LEAD() functions?
AORDER BY inside OVER()
BWHERE
CGROUP BY
DHAVING
✗ Incorrect
LAG() and LEAD() require an ORDER BY clause inside the OVER() to define row order.
If you want to compare each row with the row two steps before, which function and parameter do you use?
ALEAD(column, 1)
BLEAD(column, 2)
CLAG(column, 2)
DLAG(column, 1)
✗ Incorrect
LAG(column, 2) looks two rows back from the current row.
What value does LAG() return if there is no previous row?
A0
BNULL
CEmpty string
DError
✗ Incorrect
LAG() returns NULL when no previous row exists.
Which of these is NOT a use case for LAG and LEAD?
ALooking up values in unrelated tables
BCalculating running totals
CFinding differences between rows
DComparing current row with previous or next row
✗ Incorrect
LAG and LEAD work within the same result set, not for unrelated table lookups.
Explain how LAG and LEAD functions help compare rows in a table.
Think about looking backward and forward in a list.
You got /4 concepts.
Write a SQL query using LEAD() to find the next month's sales for each month in a sales table.
Use LEAD() with ORDER BY month inside OVER()
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the LAG function in PostgreSQL?
easy
A. To sort rows in ascending order
B. To access data from the next row in the same result set
C. To delete duplicate rows
D. To access data from a previous row in the same result set
Solution
Step 1: Understand LAG function behavior
The LAG function returns the value from a previous row based on the current row's position.
Step 2: Compare with other options
LEAD accesses the next row, sorting and deleting duplicates are unrelated to LAG.
Final Answer:
To access data from a previous row in the same result set -> Option D
Quick Check:
LAG = previous row data access [OK]
Hint: LAG looks backward, LEAD looks forward in rows [OK]
Common Mistakes:
Confusing LAG with LEAD
Thinking LAG sorts rows
Assuming LAG deletes duplicates
2. Which of the following is the correct syntax to use LEAD to get the next row's salary ordered by employee_id?
easy
A. LEAD(salary) OVER (ORDER BY employee_id)
B. LEAD(salary ORDER BY employee_id)
C. LEAD(salary) ORDER BY employee_id
D. LEAD(ORDER BY salary) OVER (employee_id)
Solution
Step 1: Recall correct LEAD syntax
The correct syntax uses LEAD(column) OVER (ORDER BY column) to specify the column and ordering.
Step 2: Check each option
LEAD(salary) OVER (ORDER BY employee_id) matches the correct syntax; others misuse parentheses or clause order.
Final Answer:
LEAD(salary) OVER (ORDER BY employee_id) -> Option A
Quick Check:
LEAD(column) OVER (ORDER BY column) [OK]
Hint: LEAD/LAG need OVER with ORDER BY inside parentheses [OK]
Common Mistakes:
Placing ORDER BY outside OVER clause
Missing parentheses around ORDER BY
Putting ORDER BY inside function parameters
3. Given the table sales with columns month and revenue, what will this query return?
SELECT month, revenue, LAG(revenue) OVER (ORDER BY month) AS prev_revenue FROM sales;
medium
A. Each row shows only current month revenue
B. Each row shows current month revenue and next month's revenue
C. Each row shows current month revenue and previous month's revenue
D. Query will cause a syntax error
Solution
Step 1: Understand LAG with ORDER BY month
The LAG(revenue) returns the revenue from the previous month ordered by month.
Step 2: Interpret query output
Each row shows current month revenue and previous month's revenue in prev_revenue. The first row's prev_revenue will be NULL.
Final Answer:
Each row shows current month revenue and previous month's revenue -> Option C
Quick Check:
LAG = previous row value [OK]
Hint: LAG returns previous row's value based on ORDER BY [OK]
Common Mistakes:
Confusing LAG with LEAD
Expecting next month's revenue instead
Thinking query causes error
4. Identify the error in this query:
SELECT employee_id, salary, LEAD(salary) ORDER BY employee_id FROM employees;
medium
A. ORDER BY should be inside LEAD() function
B. Missing OVER() clause for LEAD function
C. LEAD cannot be used with salary column
D. employee_id must be in GROUP BY clause
Solution
Step 1: Check LEAD function syntax
LEAD requires an OVER() clause to specify ordering and partitioning.
Step 2: Analyze query structure
The query uses LEAD(salary) without OVER(), causing a syntax error.
Final Answer:
Missing OVER() clause for LEAD function -> Option B
Quick Check:
Window functions need OVER() [OK]
Hint: Always use OVER() with LEAD/LAG [OK]
Common Mistakes:
Omitting OVER() clause
Placing ORDER BY outside OVER()
Confusing window functions with aggregates
5. You want to find all employees whose salary increased compared to the previous employee ordered by hire_date. Which query correctly uses LAG to achieve this?
hard
A. SELECT employee_id, salary FROM (SELECT employee_id, salary, LAG(salary) OVER (ORDER BY hire_date) AS prev_salary FROM employees) t WHERE salary > prev_salary;
B. SELECT employee_id, salary FROM employees WHERE salary > (SELECT LAG(salary) FROM employees ORDER BY hire_date);
C. SELECT employee_id, salary, LAG(salary) OVER (ORDER BY hire_date) AS prev_salary FROM employees WHERE salary > prev_salary;
D. SELECT employee_id, salary, LAG(salary) OVER (ORDER BY hire_date) AS prev_salary FROM employees HAVING salary > prev_salary;
Solution
Step 1: Understand LAG usage for filtering
Window functions like LAG cannot be used directly in WHERE clause; use subquery or CTE to compute then filter.
Step 2: Analyze options
SELECT employee_id, salary FROM (SELECT employee_id, salary, LAG(salary) OVER (ORDER BY hire_date) AS prev_salary FROM employees) t WHERE salary > prev_salary; correctly uses subquery. The second option's subquery LAG lacks OVER clause (syntax error). The third uses alias in WHERE (invalid). The fourth uses HAVING without GROUP BY (invalid).
Final Answer:
SELECT employee_id, salary FROM (SELECT employee_id, salary, LAG(salary) OVER (ORDER BY hire_date) AS prev_salary FROM employees) t WHERE salary > prev_salary; -> Option A
Quick Check:
Subquery to filter on window functions [OK]
Hint: Use subquery to filter on window function results [OK]
Common Mistakes:
Using alias in WHERE clause
Misusing HAVING without GROUP BY
Incorrect subquery for LAG
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.