lag()
and lead()
do not accept a windowing clause (the rows between
part). What they do is look at the "previous" and "next" row, so, if you think about it, a window frame just does not make sense from functional perspective.
From the documentation - emphasis mine:
Some analytic functions allow the windowing_clause. In the listing of analytic functions at the end of this section, the functions that allow the windowing_clause are followed by an asterisk (*).
Further down in the documentation, lag()
and lead()
are listed without the asterisk (unlike, for example avg()
or count()
.
Presumably, you just want:
select
department_id,
first_name,
lag(first_name) over (partition by department_id order by salary) lag_first_name,
lead(first_name) over (partition by department_id order by salary) lead_first_name
from employees
order by department_id;
Note that you can offset lead()
and lag()
by passing a second argument to the function (which defaults to 1
when not specified), like:
lead(first_name, 3) -- get the "third" following first name
over (partition by department_id order by salary) lag_first_name