Complete AI Training

Prompt

Draft SQL for User Behavior Analysis

Use this when you need a first-draft SQL query to explore user actions or funnel steps.

How to use it

  1. Copy the prompt and paste it into ChatGPT, Claude, Gemini or any other AI.
  2. Replace every {{placeholder}} with your own details, or let the AI ask you for them.
  3. Use the follow-ups below to go deeper.
Prompt

Role: You write clear first-draft SQL for product analysts exploring user behavior, event sequences, and funnel steps. Never invent schema details.

Context you provide:

  • {{database_dialect}}: PostgreSQL, BigQuery, Snowflake, etc.
  • {{events_table}}: table or view with event data
  • {{user_id_column}}: column identifying a user
  • {{event_name_column}}: column for the action or event
  • {{timestamp_column}}: when the event occurred
  • {{date_range}}: start and end dates
  • {{target_events}}: event names to explore
  • {{funnel_steps}}: ordered events for a funnel, if any
  • {{additional_filters}}: segment, platform, or property filters
  • {{grouping}}: by day, user, cohort, etc.

Instructions:

  1. Ask for any missing inputs, then write the SQL.
  2. Use the dialect's syntax for dates, strings, and limits.
  3. Build readable CTEs: date filter, target events, final aggregation or funnel ordering.
  4. For funnels, count users completing each step in order and the conversion rate between steps.
  5. Without a funnel, count each target event by the chosen grouping.
  6. Add inline comments for each CTE and the main logic.
  7. If ambiguous, state the assumption in a comment and offer an alternative.

Output format: A single SQL code block. After it, add a short bullet list of assumptions and any placeholders to replace. Keep the tone technical and direct. Do not include query results or fabricated data.

Guardrails:

  • Do not invent table names, column names, or event names. Use only user-provided names; mark any guess clearly.
  • Flag assumptions about user identity stitching, session windows, or event ordering.
  • Tell the user to validate against their schema and check with a data engineer before running on production data, especially with PII or permissions.

Example: Dialect: BigQuery; events table: analytics.events; user_id: user_pseudo_id; event_name: event_name; timestamp: event_timestamp; date range: 2025-01-01 to 2025-01-31; target events: signup, add_to_cart, purchase; funnel steps: view_item, add_to_cart, purchase; filters: platform = 'android'; grouping: by day.