SQL for marketing and e-commerce
Six case studies from an online sports store. You compare acquisition channels, work out ROAS and cost per new customer for each campaign, measure repeat purchases and cohorts, build RFM segments, find products that sell together and calculate what a customer is worth over twelve months.
In short: almost every marketing question comes down to three tables. Who is the customer, where did they come from, and what did they buy? In this course you answer those questions yourself in SQL instead of waiting for the next report. Every query is also checked against hidden data, so typed-in numbers will not pass.
Who this course is for
People in digital marketing, CRM, e-commerce and performance marketing who have, or want, access to the store or customer database. It also suits marketing analysts and anyone preparing for a marketing analyst role. Ad platforms report their own numbers. Which customers actually come back, and what they are worth after a year, only lives in your own database.
The practice database
Every case takes place in a fictional online store for sports gear, with no real brands. Orders run from 16 January 2025 to 31 October 2026. Amounts are net and carry no currency, so they read the same in dollars or pounds. The store is deliberately small so you can check every number: 140 customers and a few hundred in spend per campaign is far less than a real store. What matters are the ratios, and the queries stay the same with millions of rows.
| Table | Contents | Key columns |
|---|---|---|
| customers | 140 customers with the channel that won them | customer_id, name, city, signup_date, channel, campaign_id |
| campaigns | 14 campaigns with dates and cost | campaign_id, name, channel, start_date, end_date, cost |
| orders | 239 orders, 16 of them cancelled | order_id, customer_id, order_date, campaign_id, status, amount |
| order_items | 297 order lines | order_id, product_id, quantity, unit_price |
| products | 24 products in 7 categories | product_id, name, category, price, unit_cost |
| sessions | 300 store visits in October 2026 | session_id, session_date, channel, campaign_id, order_id |
Two ground rules apply throughout. Revenue only counts orders with the status completed. And there are two different links to campaigns: customers.campaign_id is the campaign that won the customer, while orders.campaign_id is the campaign a single order is attributed to. Keeping those apart is what case 2 is all about.
What you will be able to do
- count new customers, revenue and conversion rate per channel, comparing periods of equal length;
- calculate ROAS, cost per order and CAC per campaign, and use LEFT JOIN to surface campaigns that produced nothing;
- measure the repeat rate and cohort retention with a fixed 90-day window;
- build RFM values and scores per customer and split customers into revenue quartiles with NTILE;
- use a self join to find products that often end up in the same basket;
- work out 12-month customer value per acquisition channel, on revenue and on gross margin.
The six cases
- Comparing channels: new customers year over year, revenue per customer and conversion rate from web analytics.
- Evaluating campaigns: ROAS, campaigns with a ROAS below 1, and cost per new customer (CAC).
- Customer retention: repeat rate per channel and cohorts with a repeat purchase within 90 days.
- RFM segments: recency, frequency and monetary per customer, scores with CASE and revenue quartiles with NTILE.
- Basket analysis: frequent product pairs and a "frequently bought together" list for a single product.
- Customer lifetime value: first-year revenue per customer and gross margin per acquisition channel.
The order follows a customer's journey: they arrive through a channel and a campaign, may buy again, turn into a loyal or a lapsed customer, buy products together and end up with a certain value. Each case has two or three tasks. The tables are shown above the editor, Run lets you try intermediate steps, and Check compares your result with the model answer.
Before you start
Levels 1 to 3 and 5 of the SQL course are a good basis: filtering, grouping, LEFT JOIN, self joins, CTEs and window functions. They are not required. Each case links to the lesson behind the technique it uses, and every task has three hints that lead step by step to the answer.
The certificate
The course has its own certificate. You earn it by solving every case task, at least 80% of them without revealing the answer. It shows your name, the date and the list of cases, and comes with a public verification link you can add to a resume or profile. The certificate is part of Premium and needs a confirmed email address; the first task of every case is free to try. It is issued by LearnThisNext and is not a certification from any database or ad platform vendor.
Your progress and the certificate form are in the course view in your account. SQL courses for other roles are listed under SQL intensive courses.
FAQ
Why SQL when the ad platforms have dashboards?
Each platform counts the orders it credits to itself. Cancellations, repeat purchases, customers who came through several channels and the cost of the goods only exist in your own database. If you can query it yourself, a question like "what is a paid social customer worth after a year?" takes minutes, not a ticket.
Which database do the exercises use?
They run on SQLite on our server, so there is nothing to install. Where MySQL, PostgreSQL or SQL Server behave differently, for example with date arithmetic or division, the case page says so.
Is the data real?
No. Customers, campaigns and orders are made up and do not belong to any real store. They are built so that typical patterns show up, such as a campaign with no orders or a customer whose only order was cancelled. We deliberately give no industry benchmarks for ROAS or repeat rates; they depend too much on product range, margin and price.
How long does the course take?
That depends on your experience. There are six cases with 15 tasks in total and no time limit. Your progress is saved, so you can stop at any point.
Does my query have to match the model answer?
No. The result is checked, not the wording of the query. Whether you use a CTE, a subquery or a join is up to you, as long as the columns and values are right.