Sub topic 15
Skipping NULLs with IGNORE NULLS
This chapter assumes you can write LAG and LEAD with an OVER (ORDER BY ...) clause and know the offset and default arguments. It covers what happens when the neighbouring row holds a NULL, and how to make Oracle look past it.
The problem: the previous row is empty
LAG and LEAD return the value from the row at a fixed distance. If that row's value is NULL, you get NULL back. That is the correct answer to "what was the value on the previous row?" It is often the wrong answer to "what was the last value we actually knew?"
Sensor feeds, price histories, and manual data entry produce this pattern. A device skips reports, and the table holds NULL for those intervals. A report that needs "the last known reading" must skip the gaps.
Create a small table to follow along:
CREATE TABLE sensor_readings (
reading_time TIMESTAMP PRIMARY KEY,
reading NUMBER(5,1)
);
INSERT INTO sensor_readings VALUES (TIMESTAMP '2026-01-01 08:00:00', 20.5);
INSERT INTO sensor_readings VALUES (TIMESTAMP '2026-01-01 08:05:00', NULL);
INSERT INTO sensor_readings VALUES (TIMESTAMP '2026-01-01 08:10:00', NULL);
INSERT INTO sensor_readings VALUES (TIMESTAMP '2026-01-01 08:15:00', 21.0);
INSERT INTO sensor_readings VALUES (TIMESTAMP '2026-01-01 08:20:00', NULL);
INSERT INTO sensor_readings VALUES (TIMESTAMP '2026-01-01 08:25:00', 22.0);
COMMIT;RESPECT NULLS and IGNORE NULLS
LAG and LEAD accept an optional null-treatment clause after the closing parenthesis of the function call and before OVER:
RESPECT NULLSis the default. A NULL row counts like any other row.IGNORE NULLSremoves rows whosevalue_expris NULL from consideration. The offset then counts only non-NULL rows.
This clause is available in Oracle Database 11g Release 2 and later. Run both variants side by side:
SELECT reading_time,
reading,
LAG(reading) OVER (ORDER BY reading_time) AS respect_nulls,
LAG(reading) IGNORE NULLS OVER (ORDER BY reading_time) AS last_known_reading
FROM sensor_readings
ORDER BY reading_time;| reading_time | reading | respect_nulls | last_known_reading |
|---|---|---|---|
| 08:00 | 20.5 | NULL | NULL |
| 08:05 | NULL | 20.5 | 20.5 |
| 08:10 | NULL | NULL | 20.5 |
| 08:15 | 21.0 | NULL | 20.5 |
| 08:20 | NULL | 21.0 | 21.0 |
| 08:25 | 22.0 | NULL | 21.0 |
At 08:10 the previous row is NULL, so the default behaviour returns NULL. With IGNORE NULLS, Oracle walks back past 08:05 to 08:00 and returns 20.5.
What exactly is ignored
Three details cause most of the confusion.
- The current row's own value does not matter.
LAGonly looks at earlier rows. At 08:05 the current reading is NULL, yetlast_known_readingis 20.5, because the lookup ignores the current row. - The test applies to
value_expr. If you writeLAG(reading * 2), Oracle skips rows wherereading * 2is NULL. Rows where the expression is non-NULL are kept, whatever other columns hold. - The ordering still controls "previous".
IGNORE NULLSchanges which rows qualify, not the sort. With a non-uniqueORDER BY, the result is as nondeterministic as before. Add a tiebreaker column as you did forROW_NUMBER.
Offset and default with IGNORE NULLS
The offset counts only qualifying rows. LAG(reading, 2) IGNORE NULLS returns the second-most-recent non-NULL reading before the current row.
SELECT reading_time,
reading,
LAG(reading, 1) IGNORE NULLS OVER (ORDER BY reading_time) AS last_known,
LAG(reading, 2) IGNORE NULLS OVER (ORDER BY reading_time) AS second_last_known,
LAG(reading, 2, -1) IGNORE NULLS OVER (ORDER BY reading_time) AS second_or_minus_one
FROM sensor_readings
ORDER BY reading_time;| reading_time | reading | last_known | second_last_known | second_or_minus_one |
|---|---|---|---|---|
| 08:00 | 20.5 | NULL | NULL | -1 |
| 08:05 | NULL | 20.5 | NULL | -1 |
| 08:10 | NULL | 20.5 | NULL | -1 |
| 08:15 | 21.0 | 20.5 | NULL | -1 |
| 08:20 | NULL | 21.0 | 20.5 | 20.5 |
| 08:25 | 22.0 | 21.0 | 20.5 | 20.5 |
At 08:15 only one non-NULL reading precedes the row (20.5), so a second one does not exist and the default applies. The default is returned when no qualifying row exists at that distance. It is not substituted for NULL values that were skipped.
LEAD works the same way in the other direction. LEAD(reading) IGNORE NULLS OVER (ORDER BY reading_time) returns the next non-NULL reading, which is useful for "when will we next know the value?" questions.
Filling gaps: carry the last value forward
A common use is to replace NULLs with the most recent known value. Combine COALESCE with LAG ... IGNORE NULLS:
SELECT reading_time,
reading,
COALESCE(
reading,
LAG(reading) IGNORE NULLS OVER (ORDER BY reading_time)
) AS filled_reading
FROM sensor_readings
ORDER BY reading_time;When reading is present, it is used. When it is NULL, the last known value fills in. The 08:05 and 08:10 rows both show 20.5, and the 08:20 row shows 21.0. Rows before the first real reading stay NULL, because nothing earlier exists. Decide whether that is acceptable, or add a final default in the COALESCE.
The same effect is available with LAST_VALUE:
SELECT reading_time,
reading,
LAST_VALUE(reading) IGNORE NULLS OVER (
ORDER BY reading_time
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) AS filled_reading
FROM sensor_readings
ORDER BY reading_time;This version includes the current row in its search, so no COALESCE is needed. The explicit ROWS window matters. Without it, the default window with an ORDER BY is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, which treats rows with equal sort keys as peers. On a unique key the result is the same, but ROWS states the intent and avoids surprises.
Common errors
- Placing the clause in the wrong spot. In Oracle 11g and 12c syntax, the null treatment goes after the function's closing parenthesis and before
OVER:LAG(reading) IGNORE NULLS OVER (...). Putting it afterOVERis a syntax error. - Expecting the default to replace skipped NULLs. The third argument only appears when no qualifying row exists. To replace NULL results, wrap the call in
COALESCEorNVL. - Forgetting the ordering tiebreaker. If two readings share a timestamp, "the previous non-NULL value" is not defined. Order by a unique column set.
- Assuming all NULLs are errors. A NULL that means "no event happened" and a NULL that means "value not recorded" are different. Carrying a value forward is only valid for the second kind.
Cost
IGNORE NULLS can require more work than the default, because Oracle may need to move back through a run of NULL rows to find a value. For typical data this is negligible. For a very sparse column with long NULL runs, test with realistic volumes and look at the execution plan. As with other analytic functions, an index that matches the PARTITION BY and ORDER BY columns can help the sort, but it does not change the logic.
Exercise
Using the sensorreadings table above, what does LEAD(reading) IGNORE NULLS OVER (ORDER BY readingtime) return for the 08:05 row, and what does LEAD(reading) with the default null treatment return for the same row?
Show answer
With IGNORE NULLS, the result for 08:05 is 21.0. The next row (08:10) is NULL and is skipped, and 08:15 holds 21.0. With the default RESPECT NULLS, the result is NULL, because the next row (08:10) holds NULL.
Exercise
Write a query that returns each readingtime with a column nextknown_reading holding the next non-NULL reading, or -1 if no later reading exists.
Show answer
SELECT readingtime, reading, LEAD(reading, 1, -1) IGNORE NULLS OVER (ORDER BY readingtime) AS nextknownreading FROM sensorreadings ORDER BY readingtime; The default -1 is returned only for rows with no later non-NULL reading. In the sample data that is the 08:25 row. Earlier rows such as 08:00 return 21.0 or 22.0 as appropriate, and none of them return -1, since each has a later non-NULL reading.
Exercise
Why does LAG(reading) IGNORE NULLS return 20.5 on the 08:05 row even though the reading on that row is itself NULL?
Show answer
LAG only examines rows before the current row. The current row's value is never tested, so its NULL is irrelevant. Oracle looks backward, skips rows with NULL readings, and finds 20.5 at 08:00.