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_id | event_time | step |
|---|---|---|
| 24 | 2026-10-02 22:21:48 | Product page |
| 25 | 2026-10-02 22:23:20 | Product page |
| 26 | 2026-10-02 22:24:06 | Cart |
| 27 | 2026-10-02 22:25:01 | Checkout |
| 28 | 2026-10-02 22:27:37 | Purchase |
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_id | session_id | event_time | step |
|---|---|---|---|
| 1 | 702 | 2026-10-01 12:36:27 | Product page |
| 2 | 702 | 2026-10-01 12:37:11 | Cart |
| 3 | 703 | 2026-10-01 20:18:38 | Product page |
| 4 | 703 | 2026-10-01 20:21:39 | Cart |
| 5 | 703 | 2026-10-01 20:22:02 | Checkout |
| 6 | 703 | 2026-10-01 20:23:48 | Purchase |
| 7 | 704 | 2026-10-01 22:45:55 | Product page |
| 8 | 704 | 2026-10-01 22:49:50 | Product page |
| 9 | 704 | 2026-10-01 22:50:18 | Cart |
| 10 | 704 | 2026-10-01 22:54:12 | Checkout |
| 11 | 705 | 2026-10-01 18:53:28 | Product page |
| 12 | 706 | 2026-10-01 10:07:22 | Product page |
| 13 | 706 | 2026-10-01 10:09:26 | Cart |
| 14 | 707 | 2026-10-01 15:32:38 | Product page |
| 15 | 708 | 2026-10-02 08:35:46 | Product page |
| 16 | 708 | 2026-10-02 08:39:38 | Product page |
| 17 | 709 | 2026-10-02 09:25:13 | Product page |
| 18 | 709 | 2026-10-02 09:25:56 | Cart |
| 19 | 709 | 2026-10-02 09:28:37 | Checkout |
| 20 | 709 | 2026-10-02 09:29:33 | Purchase |
| 21 | 710 | 2026-10-02 07:08:53 | Product page |
| 22 | 711 | 2026-10-02 09:54:37 | Product page |
| 23 | 712 | 2026-10-02 09:50:30 | Product page |
| 24 | 713 | 2026-10-02 22:21:48 | Product page |
| 25 | 713 | 2026-10-02 22:23:20 | Product page |
| 26 | 713 | 2026-10-02 22:24:06 | Cart |
| 27 | 713 | 2026-10-02 22:25:01 | Checkout |
| 28 | 713 | 2026-10-02 22:27:37 | Purchase |
| 29 | 714 | 2026-10-03 09:46:44 | Product page |
| 30 | 714 | 2026-10-03 09:50:09 | Cart |
| 31 | 714 | 2026-10-03 09:53:27 | Checkout |
| 32 | 714 | 2026-10-03 09:54:59 | Purchase |
| 33 | 716 | 2026-10-03 22:50:52 | Product page |
| 34 | 717 | 2026-10-03 13:29:41 | Product page |
| 35 | 718 | 2026-10-03 14:52:58 | Product page |
| 36 | 719 | 2026-10-03 17:02:31 | Product page |
| 37 | 719 | 2026-10-03 17:04:16 | Cart |
| 38 | 721 | 2026-10-04 09:30:22 | Product page |
| 39 | 721 | 2026-10-04 09:32:27 | Cart |
| 40 | 722 | 2026-10-04 09:24:37 | Product page |
| 41 | 722 | 2026-10-04 09:25:11 | Product page |
| 42 | 722 | 2026-10-04 09:27:30 | Cart |
| 43 | 722 | 2026-10-04 09:28:21 | Checkout |
| 44 | 722 | 2026-10-04 09:31:45 | Purchase |
| 45 | 723 | 2026-10-04 22:35:29 | Product page |
| 46 | 724 | 2026-10-04 22:57:32 | Product page |
| 47 | 724 | 2026-10-04 22:59:57 | Product page |
| 48 | 724 | 2026-10-04 23:02:36 | Cart |
| 49 | 725 | 2026-10-04 13:10:38 | Product page |
| 50 | 725 | 2026-10-04 13:12:27 | Product page |
| 51 | 725 | 2026-10-04 13:15:22 | Cart |
| 52 | 725 | 2026-10-04 13:16:00 | Checkout |
| 53 | 726 | 2026-10-04 11:30:43 | Product page |
| 54 | 727 | 2026-10-05 22:24:38 | Product page |
| 55 | 728 | 2026-10-05 15:29:45 | Product page |
| 56 | 728 | 2026-10-05 15:30:59 | Product page |
| 57 | 729 | 2026-10-05 18:41:06 | Product page |
| 58 | 731 | 2026-10-05 22:07:22 | Product page |
| 59 | 731 | 2026-10-05 22:09:37 | Product page |
| 60 | 732 | 2026-10-05 18:10:56 | Product page |
| 61 | 732 | 2026-10-05 18:11:48 | Product page |
| 62 | 732 | 2026-10-05 18:15:32 | Cart |
| 63 | 732 | 2026-10-05 18:17:28 | Checkout |
| 64 | 732 | 2026-10-05 18:18:53 | Purchase |
| 65 | 733 | 2026-10-05 12:43:21 | Product page |
| 66 | 733 | 2026-10-05 12:46:03 | Cart |
| 67 | 733 | 2026-10-05 12:46:46 | Cart |
| 68 | 735 | 2026-10-06 11:27:54 | Product page |
| 69 | 735 | 2026-10-06 11:31:39 | Cart |
| 70 | 735 | 2026-10-06 11:32:17 | Checkout |
| 71 | 735 | 2026-10-06 11:35:28 | Purchase |
| 72 | 736 | 2026-10-06 09:13:36 | Product page |
| 73 | 737 | 2026-10-06 22:24:40 | Product page |
| 74 | 737 | 2026-10-06 22:27:23 | Product page |
| 75 | 738 | 2026-10-06 09:46:34 | Product page |
| 76 | 738 | 2026-10-06 09:50:10 | Product page |
| 77 | 739 | 2026-10-06 22:11:37 | Product page |
| 78 | 739 | 2026-10-06 22:13:18 | Cart |
| 79 | 739 | 2026-10-06 22:16:59 | Checkout |
| 80 | 739 | 2026-10-06 22:20:01 | Purchase |
| 81 | 740 | 2026-10-07 22:00:56 | Product page |
| 82 | 740 | 2026-10-07 22:02:43 | Product page |
| 83 | 740 | 2026-10-07 22:04:59 | Cart |
| 84 | 740 | 2026-10-07 22:07:10 | Checkout |
| 85 | 741 | 2026-10-07 12:49:24 | Product page |
| 86 | 742 | 2026-10-07 21:23:26 | Product page |
| 87 | 742 | 2026-10-07 21:26:04 | Product page |
| 88 | 742 | 2026-10-07 21:29:01 | Cart |
| 89 | 743 | 2026-10-07 14:53:04 | Product page |
| 90 | 743 | 2026-10-07 14:56:53 | Product page |
| 91 | 743 | 2026-10-07 14:57:55 | Cart |
| 92 | 744 | 2026-10-07 15:48:42 | Product page |
| 93 | 744 | 2026-10-07 15:51:57 | Product page |
| 94 | 746 | 2026-10-08 21:45:55 | Product page |
| 95 | 746 | 2026-10-08 21:47:17 | Cart |
| 96 | 747 | 2026-10-08 13:53:04 | Product page |
| 97 | 747 | 2026-10-08 13:56:49 | Cart |
| 98 | 747 | 2026-10-08 13:59:14 | Checkout |
| 99 | 748 | 2026-10-08 07:45:31 | Product page |
| 100 | 749 | 2026-10-08 16:38:24 | Product page |
| 101 | 749 | 2026-10-08 16:41:35 | Product page |
| 102 | 750 | 2026-10-08 21:42:19 | Product page |
| 103 | 750 | 2026-10-08 21:42:48 | Product page |
| 104 | 750 | 2026-10-08 21:46:17 | Cart |
| 105 | 750 | 2026-10-08 21:49:27 | Checkout |
| 106 | 751 | 2026-10-08 10:24:53 | Product page |
| 107 | 751 | 2026-10-08 10:27:16 | Product page |
| 108 | 752 | 2026-10-08 14:01:42 | Product page |
| 109 | 752 | 2026-10-08 14:03:31 | Product page |
| 110 | 753 | 2026-10-09 22:10:47 | Product page |
| 111 | 753 | 2026-10-09 22:13:43 | Product page |
| 112 | 753 | 2026-10-09 22:14:20 | Cart |
| 113 | 755 | 2026-10-09 22:20:17 | Product page |
| 114 | 755 | 2026-10-09 22:21:19 | Cart |
| 115 | 755 | 2026-10-09 22:22:23 | Checkout |
| 116 | 756 | 2026-10-09 09:42:32 | Product page |
| 117 | 756 | 2026-10-09 09:44:56 | Product page |
| 118 | 757 | 2026-10-09 13:17:31 | Product page |
| 119 | 757 | 2026-10-09 13:20:09 | Product page |
| 120 | 757 | 2026-10-09 13:21:15 | Cart |
| 121 | 757 | 2026-10-09 13:22:06 | Checkout |
| 122 | 759 | 2026-10-10 12:03:35 | Product page |
| 123 | 760 | 2026-10-10 17:33:33 | Product page |
| 124 | 760 | 2026-10-10 17:34:19 | Product page |
| 125 | 761 | 2026-10-10 19:44:54 | Product page |
| 126 | 761 | 2026-10-10 19:48:11 | Cart |
| 127 | 761 | 2026-10-10 19:49:10 | Checkout |
| 128 | 762 | 2026-10-10 20:53:29 | Product page |
| 129 | 762 | 2026-10-10 20:53:57 | Product page |
| 130 | 763 | 2026-10-10 14:27:38 | Product page |
| 131 | 764 | 2026-10-10 22:29:14 | Product page |
| 132 | 764 | 2026-10-10 22:32:55 | Product page |
| 133 | 764 | 2026-10-10 22:33:52 | Cart |
| 134 | 765 | 2026-10-10 13:03:20 | Product page |
| 135 | 765 | 2026-10-10 13:07:18 | Cart |
| 136 | 766 | 2026-10-11 19:36:58 | Product page |
| 137 | 767 | 2026-10-11 22:13:09 | Product page |
| 138 | 768 | 2026-10-11 15:06:24 | Product page |
| 139 | 768 | 2026-10-11 15:09:20 | Cart |
| 140 | 768 | 2026-10-11 15:11:08 | Checkout |
| 141 | 768 | 2026-10-11 15:12:36 | Purchase |
| 142 | 769 | 2026-10-11 19:06:55 | Product page |
| 143 | 769 | 2026-10-11 19:09:27 | Cart |
| 144 | 770 | 2026-10-11 13:02:38 | Product page |
| 145 | 771 | 2026-10-11 09:52:03 | Product page |
| 146 | 772 | 2026-10-12 10:33:46 | Product page |
| 147 | 773 | 2026-10-12 19:04:27 | Product page |
| 148 | 773 | 2026-10-12 19:07:36 | Cart |
| 149 | 773 | 2026-10-12 19:11:08 | Cart |
| 150 | 774 | 2026-10-12 21:19:50 | Product page |
| 151 | 775 | 2026-10-12 15:56:57 | Product page |
| 152 | 775 | 2026-10-12 15:59:54 | Cart |
| 153 | 775 | 2026-10-12 16:03:50 | Checkout |
| 154 | 775 | 2026-10-12 16:05:59 | Purchase |
| 155 | 776 | 2026-10-12 10:24:01 | Product page |
| 156 | 777 | 2026-10-12 10:27:35 | Product page |
| 157 | 779 | 2026-10-13 21:44:12 | Product page |
| 158 | 779 | 2026-10-13 21:47:55 | Cart |
| 159 | 779 | 2026-10-13 21:50:26 | Checkout |
| 160 | 779 | 2026-10-13 21:54:08 | Purchase |
| 161 | 780 | 2026-10-13 11:53:50 | Product page |
| 162 | 780 | 2026-10-13 11:55:49 | Product page |
| 163 | 780 | 2026-10-13 11:58:52 | Cart |
| 164 | 780 | 2026-10-13 12:02:28 | Checkout |
| 165 | 780 | 2026-10-13 12:03:37 | Purchase |
| 166 | 781 | 2026-10-13 08:25:15 | Product page |
| 167 | 782 | 2026-10-13 16:20:14 | Product page |
| 168 | 782 | 2026-10-13 16:22:04 | Product page |
| 169 | 782 | 2026-10-13 16:24:05 | Cart |
| 170 | 784 | 2026-10-13 17:20:03 | Product page |
| 171 | 784 | 2026-10-13 17:22:42 | Product page |
| 172 | 784 | 2026-10-13 17:23:43 | Cart |
| 173 | 785 | 2026-10-14 16:04:48 | Product page |
| 174 | 785 | 2026-10-14 16:08:41 | Product page |
| 175 | 786 | 2026-10-14 16:42:35 | Product page |
| 176 | 786 | 2026-10-14 16:45:44 | Product page |
| 177 | 786 | 2026-10-14 16:48:27 | Cart |
| 178 | 786 | 2026-10-14 16:50:28 | Checkout |
| 179 | 787 | 2026-10-14 17:36:26 | Product page |
| 180 | 787 | 2026-10-14 17:37:15 | Product page |
| 181 | 787 | 2026-10-14 17:39:38 | Cart |
| 182 | 787 | 2026-10-14 17:42:12 | Checkout |
| 183 | 787 | 2026-10-14 17:44:19 | Purchase |
| 184 | 789 | 2026-10-14 15:07:13 | Product page |
| 185 | 789 | 2026-10-14 15:08:32 | Cart |
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_id | session_date | device | channel |
|---|---|---|---|
| 701 | 2026-10-01 | Mobile | Newsletter |
| 702 | 2026-10-01 | Desktop | Search |
| 703 | 2026-10-01 | Tablet | Newsletter |
| 704 | 2026-10-01 | Mobile | Newsletter |
| 705 | 2026-10-01 | Mobile | Social media |
| 706 | 2026-10-01 | Desktop | Referral |
| 707 | 2026-10-01 | Mobile | Social media |
| 708 | 2026-10-02 | Desktop | Search |
| 709 | 2026-10-02 | Desktop | Newsletter |
| 710 | 2026-10-02 | Mobile | Search |
| 711 | 2026-10-02 | Mobile | Search |
| 712 | 2026-10-02 | Desktop | Referral |
| 713 | 2026-10-02 | Desktop | Direct |
| 714 | 2026-10-03 | Mobile | Newsletter |
| 715 | 2026-10-03 | Tablet | Social media |
| 716 | 2026-10-03 | Desktop | Referral |
| 717 | 2026-10-03 | Desktop | Search |
| 718 | 2026-10-03 | Mobile | Social media |
| 719 | 2026-10-03 | Desktop | Social media |
| 720 | 2026-10-03 | Desktop | Social media |
| 721 | 2026-10-04 | Desktop | Social media |
| 722 | 2026-10-04 | Mobile | Search |
| 723 | 2026-10-04 | Desktop | Direct |
| 724 | 2026-10-04 | Desktop | Search |
| 725 | 2026-10-04 | Mobile | Direct |
| 726 | 2026-10-04 | Mobile | Referral |
| 727 | 2026-10-05 | Desktop | Search |
| 728 | 2026-10-05 | Desktop | Social media |
| 729 | 2026-10-05 | Mobile | Direct |
| 730 | 2026-10-05 | Mobile | Social media |
| 731 | 2026-10-05 | Mobile | Social media |
| 732 | 2026-10-05 | Mobile | Search |
| 733 | 2026-10-05 | Mobile | Social media |
| 734 | 2026-10-06 | Mobile | Search |
| 735 | 2026-10-06 | Desktop | Direct |
| 736 | 2026-10-06 | Desktop | Social media |
| 737 | 2026-10-06 | Desktop | Search |
| 738 | 2026-10-06 | Mobile | Direct |
| 739 | 2026-10-06 | Desktop | Search |
| 740 | 2026-10-07 | Mobile | Direct |
| 741 | 2026-10-07 | Mobile | Social media |
| 742 | 2026-10-07 | Mobile | Newsletter |
| 743 | 2026-10-07 | Mobile | Search |
| 744 | 2026-10-07 | Mobile | Newsletter |
| 745 | 2026-10-07 | Mobile | Search |
| 746 | 2026-10-08 | Tablet | Newsletter |
| 747 | 2026-10-08 | Desktop | Referral |
| 748 | 2026-10-08 | Desktop | Direct |
| 749 | 2026-10-08 | Tablet | Direct |
| 750 | 2026-10-08 | Desktop | Social media |
| 751 | 2026-10-08 | Mobile | Search |
| 752 | 2026-10-08 | Mobile | Direct |
| 753 | 2026-10-09 | Desktop | Newsletter |
| 754 | 2026-10-09 | Tablet | Social media |
| 755 | 2026-10-09 | Mobile | Social media |
| 756 | 2026-10-09 | Mobile | Search |
| 757 | 2026-10-09 | Mobile | Social media |
| 758 | 2026-10-09 | Mobile | Search |
| 759 | 2026-10-10 | Desktop | Search |
| 760 | 2026-10-10 | Desktop | Search |
| 761 | 2026-10-10 | Mobile | Direct |
| 762 | 2026-10-10 | Mobile | Social media |
| 763 | 2026-10-10 | Tablet | Social media |
| 764 | 2026-10-10 | Desktop | Search |
| 765 | 2026-10-10 | Mobile | Search |
| 766 | 2026-10-11 | Mobile | Search |
| 767 | 2026-10-11 | Desktop | Newsletter |
| 768 | 2026-10-11 | Mobile | Social media |
| 769 | 2026-10-11 | Desktop | Search |
| 770 | 2026-10-11 | Mobile | Search |
| 771 | 2026-10-11 | Tablet | Search |
| 772 | 2026-10-12 | Mobile | Social media |
| 773 | 2026-10-12 | Mobile | Search |
| 774 | 2026-10-12 | Desktop | Search |
| 775 | 2026-10-12 | Tablet | Search |
| 776 | 2026-10-12 | Desktop | Newsletter |
| 777 | 2026-10-12 | Mobile | Search |
| 778 | 2026-10-12 | Mobile | Search |
| 779 | 2026-10-13 | Mobile | Newsletter |
| 780 | 2026-10-13 | Desktop | Search |
| 781 | 2026-10-13 | Desktop | Direct |
| 782 | 2026-10-13 | Desktop | Search |
| 783 | 2026-10-13 | Mobile | Search |
| 784 | 2026-10-13 | Mobile | Search |
| 785 | 2026-10-14 | Tablet | Direct |
| 786 | 2026-10-14 | Mobile | Referral |
| 787 | 2026-10-14 | Mobile | Newsletter |
| 788 | 2026-10-14 | Mobile | Newsletter |
| 789 | 2026-10-14 | Mobile | Newsletter |
| 790 | 2026-10-14 | Desktop | Social media |
events 185 rows, 4 columns
| event_id | session_id | event_time | step |
|---|---|---|---|
| 1 | 702 | 2026-10-01 12:36:27 | Product page |
| 2 | 702 | 2026-10-01 12:37:11 | Cart |
| 3 | 703 | 2026-10-01 20:18:38 | Product page |
| 4 | 703 | 2026-10-01 20:21:39 | Cart |
| 5 | 703 | 2026-10-01 20:22:02 | Checkout |
| 6 | 703 | 2026-10-01 20:23:48 | Purchase |
| 7 | 704 | 2026-10-01 22:45:55 | Product page |
| 8 | 704 | 2026-10-01 22:49:50 | Product page |
| 9 | 704 | 2026-10-01 22:50:18 | Cart |
| 10 | 704 | 2026-10-01 22:54:12 | Checkout |
| 11 | 705 | 2026-10-01 18:53:28 | Product page |
| 12 | 706 | 2026-10-01 10:07:22 | Product page |
| 13 | 706 | 2026-10-01 10:09:26 | Cart |
| 14 | 707 | 2026-10-01 15:32:38 | Product page |
| 15 | 708 | 2026-10-02 08:35:46 | Product page |
| 16 | 708 | 2026-10-02 08:39:38 | Product page |
| 17 | 709 | 2026-10-02 09:25:13 | Product page |
| 18 | 709 | 2026-10-02 09:25:56 | Cart |
| 19 | 709 | 2026-10-02 09:28:37 | Checkout |
| 20 | 709 | 2026-10-02 09:29:33 | Purchase |
| 21 | 710 | 2026-10-02 07:08:53 | Product page |
| 22 | 711 | 2026-10-02 09:54:37 | Product page |
| 23 | 712 | 2026-10-02 09:50:30 | Product page |
| 24 | 713 | 2026-10-02 22:21:48 | Product page |
| 25 | 713 | 2026-10-02 22:23:20 | Product page |
| 26 | 713 | 2026-10-02 22:24:06 | Cart |
| 27 | 713 | 2026-10-02 22:25:01 | Checkout |
| 28 | 713 | 2026-10-02 22:27:37 | Purchase |
| 29 | 714 | 2026-10-03 09:46:44 | Product page |
| 30 | 714 | 2026-10-03 09:50:09 | Cart |
| 31 | 714 | 2026-10-03 09:53:27 | Checkout |
| 32 | 714 | 2026-10-03 09:54:59 | Purchase |
| 33 | 716 | 2026-10-03 22:50:52 | Product page |
| 34 | 717 | 2026-10-03 13:29:41 | Product page |
| 35 | 718 | 2026-10-03 14:52:58 | Product page |
| 36 | 719 | 2026-10-03 17:02:31 | Product page |
| 37 | 719 | 2026-10-03 17:04:16 | Cart |
| 38 | 721 | 2026-10-04 09:30:22 | Product page |
| 39 | 721 | 2026-10-04 09:32:27 | Cart |
| 40 | 722 | 2026-10-04 09:24:37 | Product page |
| 41 | 722 | 2026-10-04 09:25:11 | Product page |
| 42 | 722 | 2026-10-04 09:27:30 | Cart |
| 43 | 722 | 2026-10-04 09:28:21 | Checkout |
| 44 | 722 | 2026-10-04 09:31:45 | Purchase |
| 45 | 723 | 2026-10-04 22:35:29 | Product page |
| 46 | 724 | 2026-10-04 22:57:32 | Product page |
| 47 | 724 | 2026-10-04 22:59:57 | Product page |
| 48 | 724 | 2026-10-04 23:02:36 | Cart |
| 49 | 725 | 2026-10-04 13:10:38 | Product page |
| 50 | 725 | 2026-10-04 13:12:27 | Product page |
| 51 | 725 | 2026-10-04 13:15:22 | Cart |
| 52 | 725 | 2026-10-04 13:16:00 | Checkout |
| 53 | 726 | 2026-10-04 11:30:43 | Product page |
| 54 | 727 | 2026-10-05 22:24:38 | Product page |
| 55 | 728 | 2026-10-05 15:29:45 | Product page |
| 56 | 728 | 2026-10-05 15:30:59 | Product page |
| 57 | 729 | 2026-10-05 18:41:06 | Product page |
| 58 | 731 | 2026-10-05 22:07:22 | Product page |
| 59 | 731 | 2026-10-05 22:09:37 | Product page |
| 60 | 732 | 2026-10-05 18:10:56 | Product page |
| 61 | 732 | 2026-10-05 18:11:48 | Product page |
| 62 | 732 | 2026-10-05 18:15:32 | Cart |
| 63 | 732 | 2026-10-05 18:17:28 | Checkout |
| 64 | 732 | 2026-10-05 18:18:53 | Purchase |
| 65 | 733 | 2026-10-05 12:43:21 | Product page |
| 66 | 733 | 2026-10-05 12:46:03 | Cart |
| 67 | 733 | 2026-10-05 12:46:46 | Cart |
| 68 | 735 | 2026-10-06 11:27:54 | Product page |
| 69 | 735 | 2026-10-06 11:31:39 | Cart |
| 70 | 735 | 2026-10-06 11:32:17 | Checkout |
| 71 | 735 | 2026-10-06 11:35:28 | Purchase |
| 72 | 736 | 2026-10-06 09:13:36 | Product page |
| 73 | 737 | 2026-10-06 22:24:40 | Product page |
| 74 | 737 | 2026-10-06 22:27:23 | Product page |
| 75 | 738 | 2026-10-06 09:46:34 | Product page |
| 76 | 738 | 2026-10-06 09:50:10 | Product page |
| 77 | 739 | 2026-10-06 22:11:37 | Product page |
| 78 | 739 | 2026-10-06 22:13:18 | Cart |
| 79 | 739 | 2026-10-06 22:16:59 | Checkout |
| 80 | 739 | 2026-10-06 22:20:01 | Purchase |
| 81 | 740 | 2026-10-07 22:00:56 | Product page |
| 82 | 740 | 2026-10-07 22:02:43 | Product page |
| 83 | 740 | 2026-10-07 22:04:59 | Cart |
| 84 | 740 | 2026-10-07 22:07:10 | Checkout |
| 85 | 741 | 2026-10-07 12:49:24 | Product page |
| 86 | 742 | 2026-10-07 21:23:26 | Product page |
| 87 | 742 | 2026-10-07 21:26:04 | Product page |
| 88 | 742 | 2026-10-07 21:29:01 | Cart |
| 89 | 743 | 2026-10-07 14:53:04 | Product page |
| 90 | 743 | 2026-10-07 14:56:53 | Product page |
| 91 | 743 | 2026-10-07 14:57:55 | Cart |
| 92 | 744 | 2026-10-07 15:48:42 | Product page |
| 93 | 744 | 2026-10-07 15:51:57 | Product page |
| 94 | 746 | 2026-10-08 21:45:55 | Product page |
| 95 | 746 | 2026-10-08 21:47:17 | Cart |
| 96 | 747 | 2026-10-08 13:53:04 | Product page |
| 97 | 747 | 2026-10-08 13:56:49 | Cart |
| 98 | 747 | 2026-10-08 13:59:14 | Checkout |
| 99 | 748 | 2026-10-08 07:45:31 | Product page |
| 100 | 749 | 2026-10-08 16:38:24 | Product page |
| 101 | 749 | 2026-10-08 16:41:35 | Product page |
| 102 | 750 | 2026-10-08 21:42:19 | Product page |
| 103 | 750 | 2026-10-08 21:42:48 | Product page |
| 104 | 750 | 2026-10-08 21:46:17 | Cart |
| 105 | 750 | 2026-10-08 21:49:27 | Checkout |
| 106 | 751 | 2026-10-08 10:24:53 | Product page |
| 107 | 751 | 2026-10-08 10:27:16 | Product page |
| 108 | 752 | 2026-10-08 14:01:42 | Product page |
| 109 | 752 | 2026-10-08 14:03:31 | Product page |
| 110 | 753 | 2026-10-09 22:10:47 | Product page |
| 111 | 753 | 2026-10-09 22:13:43 | Product page |
| 112 | 753 | 2026-10-09 22:14:20 | Cart |
| 113 | 755 | 2026-10-09 22:20:17 | Product page |
| 114 | 755 | 2026-10-09 22:21:19 | Cart |
| 115 | 755 | 2026-10-09 22:22:23 | Checkout |
| 116 | 756 | 2026-10-09 09:42:32 | Product page |
| 117 | 756 | 2026-10-09 09:44:56 | Product page |
| 118 | 757 | 2026-10-09 13:17:31 | Product page |
| 119 | 757 | 2026-10-09 13:20:09 | Product page |
| 120 | 757 | 2026-10-09 13:21:15 | Cart |
| 121 | 757 | 2026-10-09 13:22:06 | Checkout |
| 122 | 759 | 2026-10-10 12:03:35 | Product page |
| 123 | 760 | 2026-10-10 17:33:33 | Product page |
| 124 | 760 | 2026-10-10 17:34:19 | Product page |
| 125 | 761 | 2026-10-10 19:44:54 | Product page |
| 126 | 761 | 2026-10-10 19:48:11 | Cart |
| 127 | 761 | 2026-10-10 19:49:10 | Checkout |
| 128 | 762 | 2026-10-10 20:53:29 | Product page |
| 129 | 762 | 2026-10-10 20:53:57 | Product page |
| 130 | 763 | 2026-10-10 14:27:38 | Product page |
| 131 | 764 | 2026-10-10 22:29:14 | Product page |
| 132 | 764 | 2026-10-10 22:32:55 | Product page |
| 133 | 764 | 2026-10-10 22:33:52 | Cart |
| 134 | 765 | 2026-10-10 13:03:20 | Product page |
| 135 | 765 | 2026-10-10 13:07:18 | Cart |
| 136 | 766 | 2026-10-11 19:36:58 | Product page |
| 137 | 767 | 2026-10-11 22:13:09 | Product page |
| 138 | 768 | 2026-10-11 15:06:24 | Product page |
| 139 | 768 | 2026-10-11 15:09:20 | Cart |
| 140 | 768 | 2026-10-11 15:11:08 | Checkout |
| 141 | 768 | 2026-10-11 15:12:36 | Purchase |
| 142 | 769 | 2026-10-11 19:06:55 | Product page |
| 143 | 769 | 2026-10-11 19:09:27 | Cart |
| 144 | 770 | 2026-10-11 13:02:38 | Product page |
| 145 | 771 | 2026-10-11 09:52:03 | Product page |
| 146 | 772 | 2026-10-12 10:33:46 | Product page |
| 147 | 773 | 2026-10-12 19:04:27 | Product page |
| 148 | 773 | 2026-10-12 19:07:36 | Cart |
| 149 | 773 | 2026-10-12 19:11:08 | Cart |
| 150 | 774 | 2026-10-12 21:19:50 | Product page |
| 151 | 775 | 2026-10-12 15:56:57 | Product page |
| 152 | 775 | 2026-10-12 15:59:54 | Cart |
| 153 | 775 | 2026-10-12 16:03:50 | Checkout |
| 154 | 775 | 2026-10-12 16:05:59 | Purchase |
| 155 | 776 | 2026-10-12 10:24:01 | Product page |
| 156 | 777 | 2026-10-12 10:27:35 | Product page |
| 157 | 779 | 2026-10-13 21:44:12 | Product page |
| 158 | 779 | 2026-10-13 21:47:55 | Cart |
| 159 | 779 | 2026-10-13 21:50:26 | Checkout |
| 160 | 779 | 2026-10-13 21:54:08 | Purchase |
| 161 | 780 | 2026-10-13 11:53:50 | Product page |
| 162 | 780 | 2026-10-13 11:55:49 | Product page |
| 163 | 780 | 2026-10-13 11:58:52 | Cart |
| 164 | 780 | 2026-10-13 12:02:28 | Checkout |
| 165 | 780 | 2026-10-13 12:03:37 | Purchase |
| 166 | 781 | 2026-10-13 08:25:15 | Product page |
| 167 | 782 | 2026-10-13 16:20:14 | Product page |
| 168 | 782 | 2026-10-13 16:22:04 | Product page |
| 169 | 782 | 2026-10-13 16:24:05 | Cart |
| 170 | 784 | 2026-10-13 17:20:03 | Product page |
| 171 | 784 | 2026-10-13 17:22:42 | Product page |
| 172 | 784 | 2026-10-13 17:23:43 | Cart |
| 173 | 785 | 2026-10-14 16:04:48 | Product page |
| 174 | 785 | 2026-10-14 16:08:41 | Product page |
| 175 | 786 | 2026-10-14 16:42:35 | Product page |
| 176 | 786 | 2026-10-14 16:45:44 | Product page |
| 177 | 786 | 2026-10-14 16:48:27 | Cart |
| 178 | 786 | 2026-10-14 16:50:28 | Checkout |
| 179 | 787 | 2026-10-14 17:36:26 | Product page |
| 180 | 787 | 2026-10-14 17:37:15 | Product page |
| 181 | 787 | 2026-10-14 17:39:38 | Cart |
| 182 | 787 | 2026-10-14 17:42:12 | Checkout |
| 183 | 787 | 2026-10-14 17:44:19 | Purchase |
| 184 | 789 | 2026-10-14 15:07:13 | Product page |
| 185 | 789 | 2026-10-14 15:08:32 | Cart |
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_id | session_id | event_time | step |
|---|---|---|---|
| 1 | 702 | 2026-10-01 12:36:27 | Product page |
| 2 | 702 | 2026-10-01 12:37:11 | Cart |
| 3 | 703 | 2026-10-01 20:18:38 | Product page |
| 4 | 703 | 2026-10-01 20:21:39 | Cart |
| 5 | 703 | 2026-10-01 20:22:02 | Checkout |
| 6 | 703 | 2026-10-01 20:23:48 | Purchase |
| 7 | 704 | 2026-10-01 22:45:55 | Product page |
| 8 | 704 | 2026-10-01 22:49:50 | Product page |
| 9 | 704 | 2026-10-01 22:50:18 | Cart |
| 10 | 704 | 2026-10-01 22:54:12 | Checkout |
| 11 | 705 | 2026-10-01 18:53:28 | Product page |
| 12 | 706 | 2026-10-01 10:07:22 | Product page |
| 13 | 706 | 2026-10-01 10:09:26 | Cart |
| 14 | 707 | 2026-10-01 15:32:38 | Product page |
| 15 | 708 | 2026-10-02 08:35:46 | Product page |
| 16 | 708 | 2026-10-02 08:39:38 | Product page |
| 17 | 709 | 2026-10-02 09:25:13 | Product page |
| 18 | 709 | 2026-10-02 09:25:56 | Cart |
| 19 | 709 | 2026-10-02 09:28:37 | Checkout |
| 20 | 709 | 2026-10-02 09:29:33 | Purchase |
| 21 | 710 | 2026-10-02 07:08:53 | Product page |
| 22 | 711 | 2026-10-02 09:54:37 | Product page |
| 23 | 712 | 2026-10-02 09:50:30 | Product page |
| 24 | 713 | 2026-10-02 22:21:48 | Product page |
| 25 | 713 | 2026-10-02 22:23:20 | Product page |
| 26 | 713 | 2026-10-02 22:24:06 | Cart |
| 27 | 713 | 2026-10-02 22:25:01 | Checkout |
| 28 | 713 | 2026-10-02 22:27:37 | Purchase |
| 29 | 714 | 2026-10-03 09:46:44 | Product page |
| 30 | 714 | 2026-10-03 09:50:09 | Cart |
| 31 | 714 | 2026-10-03 09:53:27 | Checkout |
| 32 | 714 | 2026-10-03 09:54:59 | Purchase |
| 33 | 716 | 2026-10-03 22:50:52 | Product page |
| 34 | 717 | 2026-10-03 13:29:41 | Product page |
| 35 | 718 | 2026-10-03 14:52:58 | Product page |
| 36 | 719 | 2026-10-03 17:02:31 | Product page |
| 37 | 719 | 2026-10-03 17:04:16 | Cart |
| 38 | 721 | 2026-10-04 09:30:22 | Product page |
| 39 | 721 | 2026-10-04 09:32:27 | Cart |
| 40 | 722 | 2026-10-04 09:24:37 | Product page |
| 41 | 722 | 2026-10-04 09:25:11 | Product page |
| 42 | 722 | 2026-10-04 09:27:30 | Cart |
| 43 | 722 | 2026-10-04 09:28:21 | Checkout |
| 44 | 722 | 2026-10-04 09:31:45 | Purchase |
| 45 | 723 | 2026-10-04 22:35:29 | Product page |
| 46 | 724 | 2026-10-04 22:57:32 | Product page |
| 47 | 724 | 2026-10-04 22:59:57 | Product page |
| 48 | 724 | 2026-10-04 23:02:36 | Cart |
| 49 | 725 | 2026-10-04 13:10:38 | Product page |
| 50 | 725 | 2026-10-04 13:12:27 | Product page |
| 51 | 725 | 2026-10-04 13:15:22 | Cart |
| 52 | 725 | 2026-10-04 13:16:00 | Checkout |
| 53 | 726 | 2026-10-04 11:30:43 | Product page |
| 54 | 727 | 2026-10-05 22:24:38 | Product page |
| 55 | 728 | 2026-10-05 15:29:45 | Product page |
| 56 | 728 | 2026-10-05 15:30:59 | Product page |
| 57 | 729 | 2026-10-05 18:41:06 | Product page |
| 58 | 731 | 2026-10-05 22:07:22 | Product page |
| 59 | 731 | 2026-10-05 22:09:37 | Product page |
| 60 | 732 | 2026-10-05 18:10:56 | Product page |
| 61 | 732 | 2026-10-05 18:11:48 | Product page |
| 62 | 732 | 2026-10-05 18:15:32 | Cart |
| 63 | 732 | 2026-10-05 18:17:28 | Checkout |
| 64 | 732 | 2026-10-05 18:18:53 | Purchase |
| 65 | 733 | 2026-10-05 12:43:21 | Product page |
| 66 | 733 | 2026-10-05 12:46:03 | Cart |
| 67 | 733 | 2026-10-05 12:46:46 | Cart |
| 68 | 735 | 2026-10-06 11:27:54 | Product page |
| 69 | 735 | 2026-10-06 11:31:39 | Cart |
| 70 | 735 | 2026-10-06 11:32:17 | Checkout |
| 71 | 735 | 2026-10-06 11:35:28 | Purchase |
| 72 | 736 | 2026-10-06 09:13:36 | Product page |
| 73 | 737 | 2026-10-06 22:24:40 | Product page |
| 74 | 737 | 2026-10-06 22:27:23 | Product page |
| 75 | 738 | 2026-10-06 09:46:34 | Product page |
| 76 | 738 | 2026-10-06 09:50:10 | Product page |
| 77 | 739 | 2026-10-06 22:11:37 | Product page |
| 78 | 739 | 2026-10-06 22:13:18 | Cart |
| 79 | 739 | 2026-10-06 22:16:59 | Checkout |
| 80 | 739 | 2026-10-06 22:20:01 | Purchase |
| 81 | 740 | 2026-10-07 22:00:56 | Product page |
| 82 | 740 | 2026-10-07 22:02:43 | Product page |
| 83 | 740 | 2026-10-07 22:04:59 | Cart |
| 84 | 740 | 2026-10-07 22:07:10 | Checkout |
| 85 | 741 | 2026-10-07 12:49:24 | Product page |
| 86 | 742 | 2026-10-07 21:23:26 | Product page |
| 87 | 742 | 2026-10-07 21:26:04 | Product page |
| 88 | 742 | 2026-10-07 21:29:01 | Cart |
| 89 | 743 | 2026-10-07 14:53:04 | Product page |
| 90 | 743 | 2026-10-07 14:56:53 | Product page |
| 91 | 743 | 2026-10-07 14:57:55 | Cart |
| 92 | 744 | 2026-10-07 15:48:42 | Product page |
| 93 | 744 | 2026-10-07 15:51:57 | Product page |
| 94 | 746 | 2026-10-08 21:45:55 | Product page |
| 95 | 746 | 2026-10-08 21:47:17 | Cart |
| 96 | 747 | 2026-10-08 13:53:04 | Product page |
| 97 | 747 | 2026-10-08 13:56:49 | Cart |
| 98 | 747 | 2026-10-08 13:59:14 | Checkout |
| 99 | 748 | 2026-10-08 07:45:31 | Product page |
| 100 | 749 | 2026-10-08 16:38:24 | Product page |
| 101 | 749 | 2026-10-08 16:41:35 | Product page |
| 102 | 750 | 2026-10-08 21:42:19 | Product page |
| 103 | 750 | 2026-10-08 21:42:48 | Product page |
| 104 | 750 | 2026-10-08 21:46:17 | Cart |
| 105 | 750 | 2026-10-08 21:49:27 | Checkout |
| 106 | 751 | 2026-10-08 10:24:53 | Product page |
| 107 | 751 | 2026-10-08 10:27:16 | Product page |
| 108 | 752 | 2026-10-08 14:01:42 | Product page |
| 109 | 752 | 2026-10-08 14:03:31 | Product page |
| 110 | 753 | 2026-10-09 22:10:47 | Product page |
| 111 | 753 | 2026-10-09 22:13:43 | Product page |
| 112 | 753 | 2026-10-09 22:14:20 | Cart |
| 113 | 755 | 2026-10-09 22:20:17 | Product page |
| 114 | 755 | 2026-10-09 22:21:19 | Cart |
| 115 | 755 | 2026-10-09 22:22:23 | Checkout |
| 116 | 756 | 2026-10-09 09:42:32 | Product page |
| 117 | 756 | 2026-10-09 09:44:56 | Product page |
| 118 | 757 | 2026-10-09 13:17:31 | Product page |
| 119 | 757 | 2026-10-09 13:20:09 | Product page |
| 120 | 757 | 2026-10-09 13:21:15 | Cart |
| 121 | 757 | 2026-10-09 13:22:06 | Checkout |
| 122 | 759 | 2026-10-10 12:03:35 | Product page |
| 123 | 760 | 2026-10-10 17:33:33 | Product page |
| 124 | 760 | 2026-10-10 17:34:19 | Product page |
| 125 | 761 | 2026-10-10 19:44:54 | Product page |
| 126 | 761 | 2026-10-10 19:48:11 | Cart |
| 127 | 761 | 2026-10-10 19:49:10 | Checkout |
| 128 | 762 | 2026-10-10 20:53:29 | Product page |
| 129 | 762 | 2026-10-10 20:53:57 | Product page |
| 130 | 763 | 2026-10-10 14:27:38 | Product page |
| 131 | 764 | 2026-10-10 22:29:14 | Product page |
| 132 | 764 | 2026-10-10 22:32:55 | Product page |
| 133 | 764 | 2026-10-10 22:33:52 | Cart |
| 134 | 765 | 2026-10-10 13:03:20 | Product page |
| 135 | 765 | 2026-10-10 13:07:18 | Cart |
| 136 | 766 | 2026-10-11 19:36:58 | Product page |
| 137 | 767 | 2026-10-11 22:13:09 | Product page |
| 138 | 768 | 2026-10-11 15:06:24 | Product page |
| 139 | 768 | 2026-10-11 15:09:20 | Cart |
| 140 | 768 | 2026-10-11 15:11:08 | Checkout |
| 141 | 768 | 2026-10-11 15:12:36 | Purchase |
| 142 | 769 | 2026-10-11 19:06:55 | Product page |
| 143 | 769 | 2026-10-11 19:09:27 | Cart |
| 144 | 770 | 2026-10-11 13:02:38 | Product page |
| 145 | 771 | 2026-10-11 09:52:03 | Product page |
| 146 | 772 | 2026-10-12 10:33:46 | Product page |
| 147 | 773 | 2026-10-12 19:04:27 | Product page |
| 148 | 773 | 2026-10-12 19:07:36 | Cart |
| 149 | 773 | 2026-10-12 19:11:08 | Cart |
| 150 | 774 | 2026-10-12 21:19:50 | Product page |
| 151 | 775 | 2026-10-12 15:56:57 | Product page |
| 152 | 775 | 2026-10-12 15:59:54 | Cart |
| 153 | 775 | 2026-10-12 16:03:50 | Checkout |
| 154 | 775 | 2026-10-12 16:05:59 | Purchase |
| 155 | 776 | 2026-10-12 10:24:01 | Product page |
| 156 | 777 | 2026-10-12 10:27:35 | Product page |
| 157 | 779 | 2026-10-13 21:44:12 | Product page |
| 158 | 779 | 2026-10-13 21:47:55 | Cart |
| 159 | 779 | 2026-10-13 21:50:26 | Checkout |
| 160 | 779 | 2026-10-13 21:54:08 | Purchase |
| 161 | 780 | 2026-10-13 11:53:50 | Product page |
| 162 | 780 | 2026-10-13 11:55:49 | Product page |
| 163 | 780 | 2026-10-13 11:58:52 | Cart |
| 164 | 780 | 2026-10-13 12:02:28 | Checkout |
| 165 | 780 | 2026-10-13 12:03:37 | Purchase |
| 166 | 781 | 2026-10-13 08:25:15 | Product page |
| 167 | 782 | 2026-10-13 16:20:14 | Product page |
| 168 | 782 | 2026-10-13 16:22:04 | Product page |
| 169 | 782 | 2026-10-13 16:24:05 | Cart |
| 170 | 784 | 2026-10-13 17:20:03 | Product page |
| 171 | 784 | 2026-10-13 17:22:42 | Product page |
| 172 | 784 | 2026-10-13 17:23:43 | Cart |
| 173 | 785 | 2026-10-14 16:04:48 | Product page |
| 174 | 785 | 2026-10-14 16:08:41 | Product page |
| 175 | 786 | 2026-10-14 16:42:35 | Product page |
| 176 | 786 | 2026-10-14 16:45:44 | Product page |
| 177 | 786 | 2026-10-14 16:48:27 | Cart |
| 178 | 786 | 2026-10-14 16:50:28 | Checkout |
| 179 | 787 | 2026-10-14 17:36:26 | Product page |
| 180 | 787 | 2026-10-14 17:37:15 | Product page |
| 181 | 787 | 2026-10-14 17:39:38 | Cart |
| 182 | 787 | 2026-10-14 17:42:12 | Checkout |
| 183 | 787 | 2026-10-14 17:44:19 | Purchase |
| 184 | 789 | 2026-10-14 15:07:13 | Product page |
| 185 | 789 | 2026-10-14 15:08:32 | Cart |
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:
| Database | 39 / 78 | To get a percentage |
|---|---|---|
| MySQL | decimal result | 100 * a / b is enough |
| PostgreSQL | 0, the fraction is truncated | 100.0 * a / b |
| SQL Server | 0, the fraction is truncated | 100.0 * a / b |
| SQLite (these exercises) | 0 | 100.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.
Back to the course overview.