with base as ( select employee_id, to_dt, from_dt, lag(to_dt) over (partition by employee_id order by from_dt) lag_to_dt from abakhytbekuly.salary ), error as ( select employee_id, from_dt, case when lag_to_dt > from_dt then 'пересечение' when lag_to_dt < from_dt then 'дыра' end as status from base where lag_to_dt is not null ) select * from error order by employee_id, from_dt