Ivy Professional School
Rating
Learning/SQL Data Drill #10

Advertisement Analysis

Last updated

Analyze website engagement to identify inactive high-engagement customers and understand how many events customers usually take before purchasing.

SQLIntermediateCTERANKLAGNTILEFIRST_VALUE

Your Objective

Analyze website engagement to identify inactive high-engagement customers and understand how many events customers usually take before purchasing.

The Setup

Your dataset contains one table from a web analytics system: 1. A Web_Events table recording each customer interaction — page views, add-to-cart, checkout, and purchase events — along with the campaign, session, and a JSON field containing event details Note: Some rows have a blank customer_id — exclude these from all queries.

Table Schema

Web_Events
ColumnTypeKey / Notes
event_idINTPK
customer_idVARCHAR(20)
event_dateDATE
event_typeVARCHAR(50)
session_idVARCHAR(50)
utm_campaignVARCHAR(50)
event_jsonTEXT

Sample Data

Web_Events — 30 rows

event_idcustomer_idevent_dateevent_typesession_idutm_campaignevent_json
1C0012025-01-05page_viewS001SummerSale{"product_id":101}
2C0012025-01-06add_to_cartS002SummerSale{"product_id":101}
3C0012025-01-07checkoutS003SummerSale{"product_id":101}
4C0012025-01-08PurchaseS004SummerSale{"product_id":101}
5C0012025-02-10page_viewS005SummerSale{"product_id":102}
6C0012025-03-12page_viewS006SummerSale{"product_id":103}
7C0022025-02-01page_viewS007FestiveOffer{"product_id":104}
8C0022025-02-02add_to_cartS008FestiveOffer{"product_id":104}
9C0022025-02-03PurchaseS009FestiveOffer{"product_id":104}
10C0022025-08-20page_viewS010FestiveOffer{"product_id":105}
11C0032025-01-10page_viewS011NewLaunch{"product_id":101}
12C0032025-01-11page_viewS012NewLaunch{"product_id":101}
13C0032025-01-12add_to_cartS013NewLaunch{"product_id":101}
14C0032025-01-13checkoutS014NewLaunch{"product_id":101}
15C0032025-01-14PurchaseS015NewLaunch{"product_id":101}
16C0032025-03-01page_viewS016NewLaunch{"product_id":102}
17C0042025-04-05page_viewS017SummerSale{"product_id":103}
18C0042025-04-06add_to_cartS018SummerSale{"product_id":103}
19C0042025-04-07page_viewS019SummerSale{"product_id":103}
20C0042025-04-08checkoutS020SummerSale{"product_id":103}
21C0042025-04-09PurchaseS021SummerSale{"product_id":103}
22C0042025-04-10page_viewS022SummerSale{"product_id":103}
23C0052025-08-01page_viewS023FestiveOffer{"product_id":105}
24C0052025-08-02add_to_cartS024FestiveOffer{"product_id":105}
25C0052025-08-03checkoutS025FestiveOffer{"product_id":105}
26C0052025-08-04PurchaseS026FestiveOffer{"product_id":105}
27C0062025-05-01page_viewS027NewLaunch{"product_id":106}
28C0062025-05-02add_to_cartS028NewLaunch{"product_id":106}
29C0062025-05-03PurchaseS029NewLaunch{"product_id":106}
302025-05-04page_viewS030NewLaunch{"product_id":106}

Your Task

This drill has two questions. Use 2025-09-01 as the reference date. Exclude rows where customer_id is blank. 1. Inactive High-Engagement Customers — Find customers who had more than 5 events on the website in 2025 but no event in the past 45 days (before 2025-09-01). Your result table should have: customer_id. 2. Events Before Purchase — For each customer, find the average number of events they take before making a purchase. Then divide all customers into 4 equal groups using NTILE. Your result table should have: customer_id, avg_events_before_purchase, bukcets.

Your output should look like this (partial example):

customer_id
C001
C003
…

Hints

  • →Q1: Write one CTE to count 2025 events per customer (HAVING count > 5). Write a second CTE using FIRST_VALUE(event_date) OVER (PARTITION BY customer_id ORDER BY event_date DESC) to find each customer's most recent event. JOIN both and filter where last_event_date < "2025-09-01" - INTERVAL 45 DAY.
  • →Q2: Start by ranking every event per customer using RANK() OVER (PARTITION BY customer_id ORDER BY event_date). Then filter to only purchase rows and use LAG(rnk) to find the rank of the previous purchase. The difference (current rank − previous rank − 1) gives the events that happened between two purchases.
  • →Q2: Wrap all that in a CTE, then GROUP BY customer_id to compute AVG(no_of_events). Apply NTILE(4) OVER (ORDER BY avg_events) in the outer SELECT to split customers into four equal buckets.
Explore Enterprise Solutions