AdvancedSQLFree prompt

Sessionization and User Journey Reconstruction from Raw Web Events,

SQL queries to transform raw pageview events into sessions, reconstruct user journeys, and calculate engagement metrics from event-level web analytics data.

Build SQL-based sessionization logic that groups raw events into meaningful sessions, reconstructs user paths, and calculates engagement metrics typically found in analytics platforms.

At a glance

Access

Free prompt

Open to copy — no account or payment needed.

Prompt objective

Build SQL-based sessionization logic that groups raw events into meaningful sessions, reconstructs user paths, and calculates engagement metrics typically found in analytics platforms.

Real use case

A startup's analytics team exports raw ClickHouse event data and needs to build their own session-level metrics because their analytics tool doesn't support custom event properties. They have 2 million events per day.

Customize these fields first

COMPANY NAMEPostgreSQL/BigQuery/ClickHouse

Replace the placeholders with your own context before you run the prompt. That usually improves the first output more than adding more instructions later.

Prompt

Write SQL queries to sessionize and analyze user journeys from raw web events for [COMPANY NAME].

Table: `events`
- event_id, user_id (or anonymous_id), event_type, page_url, page_title, referrer, timestamp, device, browser, session_id (NULL — needs to be calculated)

**Query 1 — Session Assignment:**
- Define session: 30-minute gap between events = new session
- Use LAG() to detect gaps > 30 minutes
- Assign session_id using cumulative SUM of session breaks
- Output: session_id, user_id, start_time, end_time, duration, page_count

**Query 2 — Session Metrics:**
- Average session duration
- Pages per session
- Bounce rate (single-page sessions)
- Sessions by device, browser, hour of day
- Returning vs. new visitor sessions

**Query 3 — User Journey Paths:**
- Most common page sequences (first 5 pages of session)
- Entry pages (most common first page)
- Exit pages (most common last page)
- Internal search: what do users search for and where do they go after?

**Query 4 — Conversion Attribution:**
- For users who converted, what was their most common path?
- Number of sessions before conversion
- Time from first visit to conversion
- Multi-touch: all pages visited before conversion

**Query 5 — Engagement Scoring:**
- Session engagement score based on: duration, pages, scroll depth (if available), interactions
- User-level engagement: aggregate across all sessions
- Identify power users vs. casual visitors

SQL dialect: [PostgreSQL/BigQuery/ClickHouse]. Optimize for large datasets (use partitioning).

Open directly in an AI — the text is pre-filled:

How to use this prompt

  1. 1Replace the key placeholders first: COMPANY NAME, PostgreSQL/BigQuery/ClickHouse.
  2. 2Replace any bracketed placeholders like [this] with your own context.
  3. 3Add extra background information when you want more tailored results.
  4. 4Combine multiple prompts in one conversation when you need a richer output.
  5. 5Save your best-performing prompts so they are easy to reuse later.

Next best step

Open the guide first, then branch only if you still need more.

A guide for technical builders choosing between prompts, coding workflows, and agent-based implementation.

If this prompt is close but not quite right, generate variants next. If the job is recurring, move into the course library after the guide.

Related prompts

View all

Explore other prompt categories

Move sideways into adjacent libraries when the current category is not the full answer.

Every prompt here is free. The course teaches the thinking behind them.

Copy as many prompts as you like. When you want to move from single prompts to a repeatable AI workflow, Learn AI in 30 Days walks through it, one day at a time.

Get the courseSee the 30-day curriculum first

Buy the course once ($15/$20 by length), or go all-access for $10/mo with a verifiable certificate.