Case 2: analyzing an order funnel in SQL

Out of a hundred visitors to a product page, only a handful end up buying. What matters is where the others leave. A funnel counts how many sessions reach each step, which tells you where an improvement would pay off most.

In short: a funnel counts sessions, not clicks, so it's COUNT(DISTINCT session_id) per step. For rates by device, start from the sessions table with a LEFT JOIN so that visits without a single click still count. LAG(sessions) OVER (ORDER BY step_no) fetches the previous step, and a CASE sets the order of the steps.

The situation

The team has just reworked the checkout, and the product owner wants to know where visitors still get lost. The web tracking for the first two weeks of October 2026 is available: every session with its date, device and channel, plus every click on one of the four steps Product page, Cart, Checkout and Purchase.

The data

sessions has one row per visit with session_id, session_date, device (Mobile, Desktop, Tablet) and channel. events has one row per click with session_id, event_time and step. A session that ends in a purchase looks like this:

SELECT event_id, event_time, step
FROM events
WHERE session_id = 713
ORDER BY event_time;
event_idevent_timestep
242026-10-02 22:21:48Product page
252026-10-02 22:23:20Product page
262026-10-02 22:24:06Cart
272026-10-02 22:25:01Checkout
282026-10-02 22:27:37Purchase

This visitor looked at two products, so the product page shows up twice. For the funnel the session still counts once. And some sessions have no row in events at all: visitors who landed on the home page and left straight away.

Task 1: sessions per step

Task You want to see the order funnel from the web tracking. For each step, show the number of sessions that reached it as sessions. Careful: some sessions have the same step more than once, and they still only count once.

events 185 rows, 4 columns
event_idsession_idevent_timestep
17022026-10-01 12:36:27Product page
27022026-10-01 12:37:11Cart
37032026-10-01 20:18:38Product page
47032026-10-01 20:21:39Cart
57032026-10-01 20:22:02Checkout
67032026-10-01 20:23:48Purchase
77042026-10-01 22:45:55Product page
87042026-10-01 22:49:50Product page
97042026-10-01 22:50:18Cart
107042026-10-01 22:54:12Checkout
117052026-10-01 18:53:28Product page
127062026-10-01 10:07:22Product page
137062026-10-01 10:09:26Cart
147072026-10-01 15:32:38Product page
157082026-10-02 08:35:46Product page
167082026-10-02 08:39:38Product page
177092026-10-02 09:25:13Product page
187092026-10-02 09:25:56Cart
197092026-10-02 09:28:37Checkout
207092026-10-02 09:29:33Purchase
217102026-10-02 07:08:53Product page
227112026-10-02 09:54:37Product page
237122026-10-02 09:50:30Product page
247132026-10-02 22:21:48Product page
257132026-10-02 22:23:20Product page
267132026-10-02 22:24:06Cart
277132026-10-02 22:25:01Checkout
287132026-10-02 22:27:37Purchase
297142026-10-03 09:46:44Product page
307142026-10-03 09:50:09Cart
317142026-10-03 09:53:27Checkout
327142026-10-03 09:54:59Purchase
337162026-10-03 22:50:52Product page
347172026-10-03 13:29:41Product page
357182026-10-03 14:52:58Product page
367192026-10-03 17:02:31Product page
377192026-10-03 17:04:16Cart
387212026-10-04 09:30:22Product page
397212026-10-04 09:32:27Cart
407222026-10-04 09:24:37Product page
417222026-10-04 09:25:11Product page
427222026-10-04 09:27:30Cart
437222026-10-04 09:28:21Checkout
447222026-10-04 09:31:45Purchase
457232026-10-04 22:35:29Product page
467242026-10-04 22:57:32Product page
477242026-10-04 22:59:57Product page
487242026-10-04 23:02:36Cart
497252026-10-04 13:10:38Product page
507252026-10-04 13:12:27Product page
517252026-10-04 13:15:22Cart
527252026-10-04 13:16:00Checkout
537262026-10-04 11:30:43Product page
547272026-10-05 22:24:38Product page
557282026-10-05 15:29:45Product page
567282026-10-05 15:30:59Product page
577292026-10-05 18:41:06Product page
587312026-10-05 22:07:22Product page
597312026-10-05 22:09:37Product page
607322026-10-05 18:10:56Product page
617322026-10-05 18:11:48Product page
627322026-10-05 18:15:32Cart
637322026-10-05 18:17:28Checkout
647322026-10-05 18:18:53Purchase
657332026-10-05 12:43:21Product page
667332026-10-05 12:46:03Cart
677332026-10-05 12:46:46Cart
687352026-10-06 11:27:54Product page
697352026-10-06 11:31:39Cart
707352026-10-06 11:32:17Checkout
717352026-10-06 11:35:28Purchase
727362026-10-06 09:13:36Product page
737372026-10-06 22:24:40Product page
747372026-10-06 22:27:23Product page
757382026-10-06 09:46:34Product page
767382026-10-06 09:50:10Product page
777392026-10-06 22:11:37Product page
787392026-10-06 22:13:18Cart
797392026-10-06 22:16:59Checkout
807392026-10-06 22:20:01Purchase
817402026-10-07 22:00:56Product page
827402026-10-07 22:02:43Product page
837402026-10-07 22:04:59Cart
847402026-10-07 22:07:10Checkout
857412026-10-07 12:49:24Product page
867422026-10-07 21:23:26Product page
877422026-10-07 21:26:04Product page
887422026-10-07 21:29:01Cart
897432026-10-07 14:53:04Product page
907432026-10-07 14:56:53Product page
917432026-10-07 14:57:55Cart
927442026-10-07 15:48:42Product page
937442026-10-07 15:51:57Product page
947462026-10-08 21:45:55Product page
957462026-10-08 21:47:17Cart
967472026-10-08 13:53:04Product page
977472026-10-08 13:56:49Cart
987472026-10-08 13:59:14Checkout
997482026-10-08 07:45:31Product page
1007492026-10-08 16:38:24Product page
1017492026-10-08 16:41:35Product page
1027502026-10-08 21:42:19Product page
1037502026-10-08 21:42:48Product page
1047502026-10-08 21:46:17Cart
1057502026-10-08 21:49:27Checkout
1067512026-10-08 10:24:53Product page
1077512026-10-08 10:27:16Product page
1087522026-10-08 14:01:42Product page
1097522026-10-08 14:03:31Product page
1107532026-10-09 22:10:47Product page
1117532026-10-09 22:13:43Product page
1127532026-10-09 22:14:20Cart
1137552026-10-09 22:20:17Product page
1147552026-10-09 22:21:19Cart
1157552026-10-09 22:22:23Checkout
1167562026-10-09 09:42:32Product page
1177562026-10-09 09:44:56Product page
1187572026-10-09 13:17:31Product page
1197572026-10-09 13:20:09Product page
1207572026-10-09 13:21:15Cart
1217572026-10-09 13:22:06Checkout
1227592026-10-10 12:03:35Product page
1237602026-10-10 17:33:33Product page
1247602026-10-10 17:34:19Product page
1257612026-10-10 19:44:54Product page
1267612026-10-10 19:48:11Cart
1277612026-10-10 19:49:10Checkout
1287622026-10-10 20:53:29Product page
1297622026-10-10 20:53:57Product page
1307632026-10-10 14:27:38Product page
1317642026-10-10 22:29:14Product page
1327642026-10-10 22:32:55Product page
1337642026-10-10 22:33:52Cart
1347652026-10-10 13:03:20Product page
1357652026-10-10 13:07:18Cart
1367662026-10-11 19:36:58Product page
1377672026-10-11 22:13:09Product page
1387682026-10-11 15:06:24Product page
1397682026-10-11 15:09:20Cart
1407682026-10-11 15:11:08Checkout
1417682026-10-11 15:12:36Purchase
1427692026-10-11 19:06:55Product page
1437692026-10-11 19:09:27Cart
1447702026-10-11 13:02:38Product page
1457712026-10-11 09:52:03Product page
1467722026-10-12 10:33:46Product page
1477732026-10-12 19:04:27Product page
1487732026-10-12 19:07:36Cart
1497732026-10-12 19:11:08Cart
1507742026-10-12 21:19:50Product page
1517752026-10-12 15:56:57Product page
1527752026-10-12 15:59:54Cart
1537752026-10-12 16:03:50Checkout
1547752026-10-12 16:05:59Purchase
1557762026-10-12 10:24:01Product page
1567772026-10-12 10:27:35Product page
1577792026-10-13 21:44:12Product page
1587792026-10-13 21:47:55Cart
1597792026-10-13 21:50:26Checkout
1607792026-10-13 21:54:08Purchase
1617802026-10-13 11:53:50Product page
1627802026-10-13 11:55:49Product page
1637802026-10-13 11:58:52Cart
1647802026-10-13 12:02:28Checkout
1657802026-10-13 12:03:37Purchase
1667812026-10-13 08:25:15Product page
1677822026-10-13 16:20:14Product page
1687822026-10-13 16:22:04Product page
1697822026-10-13 16:24:05Cart
1707842026-10-13 17:20:03Product page
1717842026-10-13 17:22:42Product page
1727842026-10-13 17:23:43Cart
1737852026-10-14 16:04:48Product page
1747852026-10-14 16:08:41Product page
1757862026-10-14 16:42:35Product page
1767862026-10-14 16:45:44Product page
1777862026-10-14 16:48:27Cart
1787862026-10-14 16:50:28Checkout
1797872026-10-14 17:36:26Product page
1807872026-10-14 17:37:15Product page
1817872026-10-14 17:39:38Cart
1827872026-10-14 17:42:12Checkout
1837872026-10-14 17:44:19Purchase
1847892026-10-14 15:07:13Product page
1857892026-10-14 15:08:32Cart

78 sessions reach the product page and 13 make a purchase. With COUNT(*) the product page would show 109 instead of 78, because repeat clicks count too, and every rate after it would be too low.

Task 2: conversion by device

Now the device from the sessions table comes in. The base of the conversion rate is every session, including the 12 without a single event. So you start from sessions and attach events with a LEFT JOIN. Purchases are a conditional count: COUNT(DISTINCT CASE WHEN e.step = 'Purchase' THEN e.session_id END).

Task Do people buy less often on their phones? For each device show: sessions (all sessions on that device from the sessions table, including those without a single event), purchases (sessions with the step 'Purchase') and conversion_rate (purchases divided by sessions times 100, rounded to 1 decimal).

sessions 90 rows, 4 columns
session_idsession_datedevicechannel
7012026-10-01MobileNewsletter
7022026-10-01DesktopSearch
7032026-10-01TabletNewsletter
7042026-10-01MobileNewsletter
7052026-10-01MobileSocial media
7062026-10-01DesktopReferral
7072026-10-01MobileSocial media
7082026-10-02DesktopSearch
7092026-10-02DesktopNewsletter
7102026-10-02MobileSearch
7112026-10-02MobileSearch
7122026-10-02DesktopReferral
7132026-10-02DesktopDirect
7142026-10-03MobileNewsletter
7152026-10-03TabletSocial media
7162026-10-03DesktopReferral
7172026-10-03DesktopSearch
7182026-10-03MobileSocial media
7192026-10-03DesktopSocial media
7202026-10-03DesktopSocial media
7212026-10-04DesktopSocial media
7222026-10-04MobileSearch
7232026-10-04DesktopDirect
7242026-10-04DesktopSearch
7252026-10-04MobileDirect
7262026-10-04MobileReferral
7272026-10-05DesktopSearch
7282026-10-05DesktopSocial media
7292026-10-05MobileDirect
7302026-10-05MobileSocial media
7312026-10-05MobileSocial media
7322026-10-05MobileSearch
7332026-10-05MobileSocial media
7342026-10-06MobileSearch
7352026-10-06DesktopDirect
7362026-10-06DesktopSocial media
7372026-10-06DesktopSearch
7382026-10-06MobileDirect
7392026-10-06DesktopSearch
7402026-10-07MobileDirect
7412026-10-07MobileSocial media
7422026-10-07MobileNewsletter
7432026-10-07MobileSearch
7442026-10-07MobileNewsletter
7452026-10-07MobileSearch
7462026-10-08TabletNewsletter
7472026-10-08DesktopReferral
7482026-10-08DesktopDirect
7492026-10-08TabletDirect
7502026-10-08DesktopSocial media
7512026-10-08MobileSearch
7522026-10-08MobileDirect
7532026-10-09DesktopNewsletter
7542026-10-09TabletSocial media
7552026-10-09MobileSocial media
7562026-10-09MobileSearch
7572026-10-09MobileSocial media
7582026-10-09MobileSearch
7592026-10-10DesktopSearch
7602026-10-10DesktopSearch
7612026-10-10MobileDirect
7622026-10-10MobileSocial media
7632026-10-10TabletSocial media
7642026-10-10DesktopSearch
7652026-10-10MobileSearch
7662026-10-11MobileSearch
7672026-10-11DesktopNewsletter
7682026-10-11MobileSocial media
7692026-10-11DesktopSearch
7702026-10-11MobileSearch
7712026-10-11TabletSearch
7722026-10-12MobileSocial media
7732026-10-12MobileSearch
7742026-10-12DesktopSearch
7752026-10-12TabletSearch
7762026-10-12DesktopNewsletter
7772026-10-12MobileSearch
7782026-10-12MobileSearch
7792026-10-13MobileNewsletter
7802026-10-13DesktopSearch
7812026-10-13DesktopDirect
7822026-10-13DesktopSearch
7832026-10-13MobileSearch
7842026-10-13MobileSearch
7852026-10-14TabletDirect
7862026-10-14MobileReferral
7872026-10-14MobileNewsletter
7882026-10-14MobileNewsletter
7892026-10-14MobileNewsletter
7902026-10-14DesktopSocial media
events 185 rows, 4 columns
event_idsession_idevent_timestep
17022026-10-01 12:36:27Product page
27022026-10-01 12:37:11Cart
37032026-10-01 20:18:38Product page
47032026-10-01 20:21:39Cart
57032026-10-01 20:22:02Checkout
67032026-10-01 20:23:48Purchase
77042026-10-01 22:45:55Product page
87042026-10-01 22:49:50Product page
97042026-10-01 22:50:18Cart
107042026-10-01 22:54:12Checkout
117052026-10-01 18:53:28Product page
127062026-10-01 10:07:22Product page
137062026-10-01 10:09:26Cart
147072026-10-01 15:32:38Product page
157082026-10-02 08:35:46Product page
167082026-10-02 08:39:38Product page
177092026-10-02 09:25:13Product page
187092026-10-02 09:25:56Cart
197092026-10-02 09:28:37Checkout
207092026-10-02 09:29:33Purchase
217102026-10-02 07:08:53Product page
227112026-10-02 09:54:37Product page
237122026-10-02 09:50:30Product page
247132026-10-02 22:21:48Product page
257132026-10-02 22:23:20Product page
267132026-10-02 22:24:06Cart
277132026-10-02 22:25:01Checkout
287132026-10-02 22:27:37Purchase
297142026-10-03 09:46:44Product page
307142026-10-03 09:50:09Cart
317142026-10-03 09:53:27Checkout
327142026-10-03 09:54:59Purchase
337162026-10-03 22:50:52Product page
347172026-10-03 13:29:41Product page
357182026-10-03 14:52:58Product page
367192026-10-03 17:02:31Product page
377192026-10-03 17:04:16Cart
387212026-10-04 09:30:22Product page
397212026-10-04 09:32:27Cart
407222026-10-04 09:24:37Product page
417222026-10-04 09:25:11Product page
427222026-10-04 09:27:30Cart
437222026-10-04 09:28:21Checkout
447222026-10-04 09:31:45Purchase
457232026-10-04 22:35:29Product page
467242026-10-04 22:57:32Product page
477242026-10-04 22:59:57Product page
487242026-10-04 23:02:36Cart
497252026-10-04 13:10:38Product page
507252026-10-04 13:12:27Product page
517252026-10-04 13:15:22Cart
527252026-10-04 13:16:00Checkout
537262026-10-04 11:30:43Product page
547272026-10-05 22:24:38Product page
557282026-10-05 15:29:45Product page
567282026-10-05 15:30:59Product page
577292026-10-05 18:41:06Product page
587312026-10-05 22:07:22Product page
597312026-10-05 22:09:37Product page
607322026-10-05 18:10:56Product page
617322026-10-05 18:11:48Product page
627322026-10-05 18:15:32Cart
637322026-10-05 18:17:28Checkout
647322026-10-05 18:18:53Purchase
657332026-10-05 12:43:21Product page
667332026-10-05 12:46:03Cart
677332026-10-05 12:46:46Cart
687352026-10-06 11:27:54Product page
697352026-10-06 11:31:39Cart
707352026-10-06 11:32:17Checkout
717352026-10-06 11:35:28Purchase
727362026-10-06 09:13:36Product page
737372026-10-06 22:24:40Product page
747372026-10-06 22:27:23Product page
757382026-10-06 09:46:34Product page
767382026-10-06 09:50:10Product page
777392026-10-06 22:11:37Product page
787392026-10-06 22:13:18Cart
797392026-10-06 22:16:59Checkout
807392026-10-06 22:20:01Purchase
817402026-10-07 22:00:56Product page
827402026-10-07 22:02:43Product page
837402026-10-07 22:04:59Cart
847402026-10-07 22:07:10Checkout
857412026-10-07 12:49:24Product page
867422026-10-07 21:23:26Product page
877422026-10-07 21:26:04Product page
887422026-10-07 21:29:01Cart
897432026-10-07 14:53:04Product page
907432026-10-07 14:56:53Product page
917432026-10-07 14:57:55Cart
927442026-10-07 15:48:42Product page
937442026-10-07 15:51:57Product page
947462026-10-08 21:45:55Product page
957462026-10-08 21:47:17Cart
967472026-10-08 13:53:04Product page
977472026-10-08 13:56:49Cart
987472026-10-08 13:59:14Checkout
997482026-10-08 07:45:31Product page
1007492026-10-08 16:38:24Product page
1017492026-10-08 16:41:35Product page
1027502026-10-08 21:42:19Product page
1037502026-10-08 21:42:48Product page
1047502026-10-08 21:46:17Cart
1057502026-10-08 21:49:27Checkout
1067512026-10-08 10:24:53Product page
1077512026-10-08 10:27:16Product page
1087522026-10-08 14:01:42Product page
1097522026-10-08 14:03:31Product page
1107532026-10-09 22:10:47Product page
1117532026-10-09 22:13:43Product page
1127532026-10-09 22:14:20Cart
1137552026-10-09 22:20:17Product page
1147552026-10-09 22:21:19Cart
1157552026-10-09 22:22:23Checkout
1167562026-10-09 09:42:32Product page
1177562026-10-09 09:44:56Product page
1187572026-10-09 13:17:31Product page
1197572026-10-09 13:20:09Product page
1207572026-10-09 13:21:15Cart
1217572026-10-09 13:22:06Checkout
1227592026-10-10 12:03:35Product page
1237602026-10-10 17:33:33Product page
1247602026-10-10 17:34:19Product page
1257612026-10-10 19:44:54Product page
1267612026-10-10 19:48:11Cart
1277612026-10-10 19:49:10Checkout
1287622026-10-10 20:53:29Product page
1297622026-10-10 20:53:57Product page
1307632026-10-10 14:27:38Product page
1317642026-10-10 22:29:14Product page
1327642026-10-10 22:32:55Product page
1337642026-10-10 22:33:52Cart
1347652026-10-10 13:03:20Product page
1357652026-10-10 13:07:18Cart
1367662026-10-11 19:36:58Product page
1377672026-10-11 22:13:09Product page
1387682026-10-11 15:06:24Product page
1397682026-10-11 15:09:20Cart
1407682026-10-11 15:11:08Checkout
1417682026-10-11 15:12:36Purchase
1427692026-10-11 19:06:55Product page
1437692026-10-11 19:09:27Cart
1447702026-10-11 13:02:38Product page
1457712026-10-11 09:52:03Product page
1467722026-10-12 10:33:46Product page
1477732026-10-12 19:04:27Product page
1487732026-10-12 19:07:36Cart
1497732026-10-12 19:11:08Cart
1507742026-10-12 21:19:50Product page
1517752026-10-12 15:56:57Product page
1527752026-10-12 15:59:54Cart
1537752026-10-12 16:03:50Checkout
1547752026-10-12 16:05:59Purchase
1557762026-10-12 10:24:01Product page
1567772026-10-12 10:27:35Product page
1577792026-10-13 21:44:12Product page
1587792026-10-13 21:47:55Cart
1597792026-10-13 21:50:26Checkout
1607792026-10-13 21:54:08Purchase
1617802026-10-13 11:53:50Product page
1627802026-10-13 11:55:49Product page
1637802026-10-13 11:58:52Cart
1647802026-10-13 12:02:28Checkout
1657802026-10-13 12:03:37Purchase
1667812026-10-13 08:25:15Product page
1677822026-10-13 16:20:14Product page
1687822026-10-13 16:22:04Product page
1697822026-10-13 16:24:05Cart
1707842026-10-13 17:20:03Product page
1717842026-10-13 17:22:42Product page
1727842026-10-13 17:23:43Cart
1737852026-10-14 16:04:48Product page
1747852026-10-14 16:08:41Product page
1757862026-10-14 16:42:35Product page
1767862026-10-14 16:45:44Product page
1777862026-10-14 16:48:27Cart
1787862026-10-14 16:50:28Checkout
1797872026-10-14 17:36:26Product page
1807872026-10-14 17:37:15Product page
1817872026-10-14 17:39:38Cart
1827872026-10-14 17:42:12Checkout
1837872026-10-14 17:44:19Purchase
1847892026-10-14 15:07:13Product page
1857892026-10-14 15:08:32Cart

This exercise is part of Premium.

Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.

See Premium Already Premium? Log in

The first exercise in this lesson is free.

On mobile, 12.8% of sessions end in a purchase, on desktop 14.7%. Tablet leads with 22.2%, but with only 9 sessions that's one purchase more or less. Small groups should always be reported with their count next to the rate. A word on scale, too: in real stores only a small share of visits ends in a purchase, often a few percent. This slice is small and dense on purpose, so you can count every number by hand.

Task 3: step rates with LAG

The most useful number in a funnel is the rate from one step to the next: how many of the sessions with a cart reach checkout? For that, each row needs the count of the previous step, which is what LAG does once the rows have an order. That order isn't in the data, and alphabetically Cart would come before Checkout before Product page. You set it with a CASE:

CASE step
  WHEN 'Product page' THEN 1
  WHEN 'Cart' THEN 2
  WHEN 'Checkout' THEN 3
  WHEN 'Purchase' THEN 4
END AS step_no

Task Where does the shop lose most visitors? Show the funnel steps in order with step_no (1 Product page, 2 Cart, 3 Checkout, 4 Purchase), step, sessions (different sessions per step) and step_rate: the share of the previous step's sessions that reach this step, times 100, rounded to 1 decimal (NULL for the first step). Sorted by step_no.

events 185 rows, 4 columns
event_idsession_idevent_timestep
17022026-10-01 12:36:27Product page
27022026-10-01 12:37:11Cart
37032026-10-01 20:18:38Product page
47032026-10-01 20:21:39Cart
57032026-10-01 20:22:02Checkout
67032026-10-01 20:23:48Purchase
77042026-10-01 22:45:55Product page
87042026-10-01 22:49:50Product page
97042026-10-01 22:50:18Cart
107042026-10-01 22:54:12Checkout
117052026-10-01 18:53:28Product page
127062026-10-01 10:07:22Product page
137062026-10-01 10:09:26Cart
147072026-10-01 15:32:38Product page
157082026-10-02 08:35:46Product page
167082026-10-02 08:39:38Product page
177092026-10-02 09:25:13Product page
187092026-10-02 09:25:56Cart
197092026-10-02 09:28:37Checkout
207092026-10-02 09:29:33Purchase
217102026-10-02 07:08:53Product page
227112026-10-02 09:54:37Product page
237122026-10-02 09:50:30Product page
247132026-10-02 22:21:48Product page
257132026-10-02 22:23:20Product page
267132026-10-02 22:24:06Cart
277132026-10-02 22:25:01Checkout
287132026-10-02 22:27:37Purchase
297142026-10-03 09:46:44Product page
307142026-10-03 09:50:09Cart
317142026-10-03 09:53:27Checkout
327142026-10-03 09:54:59Purchase
337162026-10-03 22:50:52Product page
347172026-10-03 13:29:41Product page
357182026-10-03 14:52:58Product page
367192026-10-03 17:02:31Product page
377192026-10-03 17:04:16Cart
387212026-10-04 09:30:22Product page
397212026-10-04 09:32:27Cart
407222026-10-04 09:24:37Product page
417222026-10-04 09:25:11Product page
427222026-10-04 09:27:30Cart
437222026-10-04 09:28:21Checkout
447222026-10-04 09:31:45Purchase
457232026-10-04 22:35:29Product page
467242026-10-04 22:57:32Product page
477242026-10-04 22:59:57Product page
487242026-10-04 23:02:36Cart
497252026-10-04 13:10:38Product page
507252026-10-04 13:12:27Product page
517252026-10-04 13:15:22Cart
527252026-10-04 13:16:00Checkout
537262026-10-04 11:30:43Product page
547272026-10-05 22:24:38Product page
557282026-10-05 15:29:45Product page
567282026-10-05 15:30:59Product page
577292026-10-05 18:41:06Product page
587312026-10-05 22:07:22Product page
597312026-10-05 22:09:37Product page
607322026-10-05 18:10:56Product page
617322026-10-05 18:11:48Product page
627322026-10-05 18:15:32Cart
637322026-10-05 18:17:28Checkout
647322026-10-05 18:18:53Purchase
657332026-10-05 12:43:21Product page
667332026-10-05 12:46:03Cart
677332026-10-05 12:46:46Cart
687352026-10-06 11:27:54Product page
697352026-10-06 11:31:39Cart
707352026-10-06 11:32:17Checkout
717352026-10-06 11:35:28Purchase
727362026-10-06 09:13:36Product page
737372026-10-06 22:24:40Product page
747372026-10-06 22:27:23Product page
757382026-10-06 09:46:34Product page
767382026-10-06 09:50:10Product page
777392026-10-06 22:11:37Product page
787392026-10-06 22:13:18Cart
797392026-10-06 22:16:59Checkout
807392026-10-06 22:20:01Purchase
817402026-10-07 22:00:56Product page
827402026-10-07 22:02:43Product page
837402026-10-07 22:04:59Cart
847402026-10-07 22:07:10Checkout
857412026-10-07 12:49:24Product page
867422026-10-07 21:23:26Product page
877422026-10-07 21:26:04Product page
887422026-10-07 21:29:01Cart
897432026-10-07 14:53:04Product page
907432026-10-07 14:56:53Product page
917432026-10-07 14:57:55Cart
927442026-10-07 15:48:42Product page
937442026-10-07 15:51:57Product page
947462026-10-08 21:45:55Product page
957462026-10-08 21:47:17Cart
967472026-10-08 13:53:04Product page
977472026-10-08 13:56:49Cart
987472026-10-08 13:59:14Checkout
997482026-10-08 07:45:31Product page
1007492026-10-08 16:38:24Product page
1017492026-10-08 16:41:35Product page
1027502026-10-08 21:42:19Product page
1037502026-10-08 21:42:48Product page
1047502026-10-08 21:46:17Cart
1057502026-10-08 21:49:27Checkout
1067512026-10-08 10:24:53Product page
1077512026-10-08 10:27:16Product page
1087522026-10-08 14:01:42Product page
1097522026-10-08 14:03:31Product page
1107532026-10-09 22:10:47Product page
1117532026-10-09 22:13:43Product page
1127532026-10-09 22:14:20Cart
1137552026-10-09 22:20:17Product page
1147552026-10-09 22:21:19Cart
1157552026-10-09 22:22:23Checkout
1167562026-10-09 09:42:32Product page
1177562026-10-09 09:44:56Product page
1187572026-10-09 13:17:31Product page
1197572026-10-09 13:20:09Product page
1207572026-10-09 13:21:15Cart
1217572026-10-09 13:22:06Checkout
1227592026-10-10 12:03:35Product page
1237602026-10-10 17:33:33Product page
1247602026-10-10 17:34:19Product page
1257612026-10-10 19:44:54Product page
1267612026-10-10 19:48:11Cart
1277612026-10-10 19:49:10Checkout
1287622026-10-10 20:53:29Product page
1297622026-10-10 20:53:57Product page
1307632026-10-10 14:27:38Product page
1317642026-10-10 22:29:14Product page
1327642026-10-10 22:32:55Product page
1337642026-10-10 22:33:52Cart
1347652026-10-10 13:03:20Product page
1357652026-10-10 13:07:18Cart
1367662026-10-11 19:36:58Product page
1377672026-10-11 22:13:09Product page
1387682026-10-11 15:06:24Product page
1397682026-10-11 15:09:20Cart
1407682026-10-11 15:11:08Checkout
1417682026-10-11 15:12:36Purchase
1427692026-10-11 19:06:55Product page
1437692026-10-11 19:09:27Cart
1447702026-10-11 13:02:38Product page
1457712026-10-11 09:52:03Product page
1467722026-10-12 10:33:46Product page
1477732026-10-12 19:04:27Product page
1487732026-10-12 19:07:36Cart
1497732026-10-12 19:11:08Cart
1507742026-10-12 21:19:50Product page
1517752026-10-12 15:56:57Product page
1527752026-10-12 15:59:54Cart
1537752026-10-12 16:03:50Checkout
1547752026-10-12 16:05:59Purchase
1557762026-10-12 10:24:01Product page
1567772026-10-12 10:27:35Product page
1577792026-10-13 21:44:12Product page
1587792026-10-13 21:47:55Cart
1597792026-10-13 21:50:26Checkout
1607792026-10-13 21:54:08Purchase
1617802026-10-13 11:53:50Product page
1627802026-10-13 11:55:49Product page
1637802026-10-13 11:58:52Cart
1647802026-10-13 12:02:28Checkout
1657802026-10-13 12:03:37Purchase
1667812026-10-13 08:25:15Product page
1677822026-10-13 16:20:14Product page
1687822026-10-13 16:22:04Product page
1697822026-10-13 16:24:05Cart
1707842026-10-13 17:20:03Product page
1717842026-10-13 17:22:42Product page
1727842026-10-13 17:23:43Cart
1737852026-10-14 16:04:48Product page
1747852026-10-14 16:08:41Product page
1757862026-10-14 16:42:35Product page
1767862026-10-14 16:45:44Product page
1777862026-10-14 16:48:27Cart
1787862026-10-14 16:50:28Checkout
1797872026-10-14 17:36:26Product page
1807872026-10-14 17:37:15Product page
1817872026-10-14 17:39:38Cart
1827872026-10-14 17:42:12Checkout
1837872026-10-14 17:44:19Purchase
1847892026-10-14 15:07:13Product page
1857892026-10-14 15:08:32Cart

This exercise is part of Premium.

Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.

See Premium Already Premium? Log in

The first exercise in this lesson is free.

The result is clear: only half of the sessions with a product page put something in the cart, and after that the rates are 56% and 59%. The biggest lever is on the product page, not in the checkout.

Common mistakes

Counting clicks instead of sessions

COUNT(*) and COUNT(session_id) count every row. The exercise says "Columns and row count match, but not every value."

INNER JOIN in task 2

With JOIN instead of LEFT JOIN, sessions without events drop out. The rate looks better than it is and the values don't match.

Purchase filter in WHERE

WHERE e.step = 'Purchase' keeps only sessions with a purchase, so the rate is 100% everywhere. The condition belongs in the CASE or in the ON condition of the LEFT JOIN.

LAG ordered by name

LAG(sessions) OVER (ORDER BY step) compares alphabetically, so Purchase with Product page and Cart with nothing at all. Order by the step number inside OVER.

Integer division

sessions / LAG(sessions) OVER (…) * 100 gives 0 in SQLite, because 39 / 78 is 0 in integer arithmetic. Start with 100.0 *.

In MySQL, PostgreSQL and SQL Server

COUNT(DISTINCT …), CASE, LEFT JOIN and LAG with OVER (ORDER BY …) look the same in all three. The difference that matters is integer division:

Database39 / 78To get a percentage
MySQLdecimal result100 * a / b is enough
PostgreSQL0, the fraction is truncated100.0 * a / b
SQL Server0, the fraction is truncated100.0 * a / b
SQLite (these exercises)0100.0 * a / b

Write 100.0 * and you're safe in all four. Sources: MySQL Reference Manual (Arithmetic Operators), PostgreSQL documentation (Mathematical Functions and Operators), Microsoft Learn (Division).

How to check your result

  • Task 1: Product page 78, Cart 39, Checkout 22, Purchase 13.
  • Task 2: sessions across all devices add up to 90 and purchases to 13, the same as in task 1.
  • Task 3: the first row has no rate (NULL), the second has 50.0.

FAQ

What's the difference between step rate and overall rate?

The step rate is relative to the previous step, the overall rate to the first. The overall rate to purchase here is 13 out of 78 sessions with a product page. To find out where things get stuck, use the step rate; for targets and forecasts, the overall rate. You get it with FIRST_VALUE(sessions) OVER (ORDER BY step_no) instead of LAG.

Does the order of the clicks matter?

In a strict funnel, a session only counts for checkout if it was in the cart before. In this data every session follows the order. In real data it's worth checking, for example with EXISTS on the previous step.

Why only two weeks of data?

Web tracking produces a lot of rows. A slice is enough for the exercise, and the method works exactly the same on a whole year.

Up next: case 3, ABC analysis

Which products carry the revenue? You calculate shares and cumulative shares and split the range into classes A, B and C.

Go to case 3

Back to the course overview.

Write to us

Choose language

Select your preferred language