with temp as(
  select
    user_id,
    attend_tb.course_id,
    course_name,
    in_datetime as dt,
    1 as flag
  from
    attend_tb
    left join course_tb on attend_tb.course_id = course_tb.course_id
  union all
  select
    user_id,
    attend_tb.course_id,
    course_name,
    out_datetime as dt,
    -1 as flag
  from
    attend_tb
    left join course_tb on attend_tb.course_id = course_tb.course_id
)
select
course_id,course_name,max(num)
from
  (
    select
      course_id,
      course_name,
      sum(flag) over(
        partition by course_id
        order by
          dt
      ) as num
    from
      temp
  ) t
  group by course_id,course_name
  order by course_id