How do you analyze an event sequence with LAG and LEAD?
You have an events table with id, user_id, event_type, and created_at. A user may have several events with the same timestamp. Explain how to use LAG and LEAD to find the event type immediately before and after each event in a user's history. How do you define the order so the results are consistent across runs? How do you handle the first and last events? And how do you filter the rows you want to display without removing their neighbors from the calculation? Explain your approach in words, with a short SQL snippet if you like.
ExplainWindow functionsMiddleAnalystData EngineerBackend