Expert: design, speed and engines · Interview questions and practice projects · topic 62 of 62 · 40 min
Project: e-commerce analytics, and your next steps
Explanation
The questions a shop asks
An e-commerce team looks at a few numbers every week. Each is a short query once you have the building blocks from this course. In all of them, Cancelled orders are left out of revenue; agree such rules with the business first, because a different rule gives a different number.
Order totals first
Most shop metrics start from one number per order: SUM(qty * price) of its lines. Put it in a CTE called totals and reuse it.
WITH totals AS (
SELECT o.id, o.customer_id, SUM(oi.qty * oi.price) AS total
FROM orders o JOIN order_items oi ON oi.order_id = o.id
WHERE o.status <> 'Cancelled'
GROUP BY o.id)
SELECT COUNT(*) AS orders, SUM(total) AS revenue, ROUND(AVG(total), 2) AS aov
FROM totals;
-- 7 | 13844 | 1977.71Monthly revenue
SELECT strftime('%Y-%m', o.order_date) AS month, SUM(oi.qty * oi.price) AS revenue
FROM orders o JOIN order_items oi ON oi.order_id = o.id
WHERE o.status <> 'Cancelled'
GROUP BY month ORDER BY month;
-- 2025-01 | 3779
-- 2025-02 | 2248
-- 2025-03 | 7167
-- 2025-04 | 650Repeat customers and customers with no orders
A repeat customer has two or more (delivered) orders: GROUP BY customer, HAVING COUNT(*) >= 2. Asha Verma and Ravi Kumar qualify. Customers with no orders need an anti-join: Arjun Reddy has never ordered.
SELECT c.id, c.name
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.id IS NULL; -- 6 | Arjun ReddyTop products
SELECT p.name, SUM(oi.qty * oi.price) AS revenue
FROM order_items oi
JOIN orders o ON o.id = oi.order_id
JOIN products p ON p.id = oi.product_id
WHERE o.status <> 'Cancelled'
GROUP BY p.id, p.name
ORDER BY revenue DESC;
-- Bluetooth Earbuds | 3998
-- Cotton Kurta | 2697
-- Running Shoes | 2499
-- ...A simple cohort
A cohort is a group of customers who started at the same time, such as everyone who joined in January 2024. Comparing cohorts shows whether newer customers buy as much as older ones. The simplest version counts, per join month, how many customers joined and how many of them have ordered:
SELECT strftime('%Y-%m', c.joined) AS cohort,
COUNT(DISTINCT c.id) AS customers,
COUNT(DISTINCT o.customer_id) AS buyers
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id AND o.status <> 'Cancelled'
GROUP BY cohort ORDER BY cohort;
-- 2024-01 | 1 | 1
-- 2024-02 | 1 | 1
-- 2024-03 | 1 | 0
-- ...Your next steps
- Practise daily for a few weeks: LeetCode's database problems, HackerRank's SQL track, SQLZoo, SQLBolt and StrataScratch all have free questions. Mix easy and medium.
- Build a portfolio: pick a public dataset (for example from data.gov.in or Kaggle), load it into SQLite or PostgreSQL, write 10–15 queries that answer real questions, and publish the SQL with short explanations on GitHub.
- Learn one server engine well (PostgreSQL is a good default), plus one BI tool such as Power BI or Tableau if you aim for analyst roles, or one backend framework if you aim for developer roles.
Jobs that use SQL every day
| Role | What you do with SQL | Rough pay in India (varies widely) |
|---|---|---|
| Data analyst | Reports, dashboards, ad-hoc questions | About ₹3–7 LPA for freshers; ₹8–15 LPA with a few years |
| BI developer | Data models and dashboards in Power BI or Tableau | About ₹4–8 LPA for freshers; ₹10–20 LPA with experience |
| Backend developer | Schemas, queries and migrations inside apps | About ₹4–10 LPA for freshers; much higher at top product companies |
| Data engineer | Pipelines that move and transform data into warehouses | About ₹5–10 LPA for freshers; ₹12–30 LPA mid-level |
| Database administrator (DBA) | Backups, performance, security, upgrades | About ₹4–8 LPA early; ₹10–25 LPA with experience |
These ranges are rough, from public job listings and salary surveys; pay depends a lot on city, company, skills and the market in the year you apply, so check current listings.
Certifications
Certifications are optional: a good portfolio often matters more. If you want one, Microsoft, Oracle, AWS and Google all offer database and data certifications, from fundamentals to administrator and data engineer levels. Names and exam codes change often, so check the current exam page on the vendor's website before you prepare.
Common mistakes
- Counting cancelled orders as revenue. Agree the rule and write it in every query (or in a view).
- Computing AOV as the average of order lines instead of orders. Total each order first.
- Putting a filter on the right-hand table of a LEFT JOIN in WHERE, which silently turns it into an inner join.
- Collecting certificates instead of building and showing real queries.
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
Project: e-commerce analytics, and your next steps · 1/8
Our last project is e-commerce analytics on the SHOP database, followed by your next steps.
Prefer a video by a teacher? Find free videos on “sql ecommerce analytics project average order value cohort repeat customers” on YouTube ↗
In real life: The weekly business review at an online store
At many e-commerce companies, an analyst prepares a weekly sheet: revenue and orders by week, AOV, new vs repeat customers, top products and categories, and cancellations. Each number comes from a saved SQL query, often turned into views or a dashboard, so the same definition is used every week. Writing such a set for SHOP is a good first portfolio project.
CREATE VIEW order_totals AS
SELECT o.id, o.customer_id, o.order_date, SUM(oi.qty * oi.price) AS total
FROM orders o JOIN order_items oi ON oi.order_id = o.id
WHERE o.status <> 'Cancelled'
GROUP BY o.id;
SELECT ROUND(AVG(total), 2) AS aov FROM order_totals; -- 1977.71Practice (0/4 done)
Average order value per customer. An order's total is SUM(qty * price) of its lines; leave out 'Cancelled' orders. Return name, orders (the customer's number of orders) and aov (the average order total, rounded to 2 decimals), highest aov first.
Repeat customers: return id, name and delivered_orders for customers with 2 or more orders whose status is 'Delivered'.
Top 2 products by revenue (SUM(qty * price) over lines of orders that are not 'Cancelled'). Return name and revenue, highest first.
Plan your SQL portfolio project. Choose a public dataset you find interesting (for example Indian rainfall, crop prices, cricket scores or a city's bus routes), and write down: the tables you will create, five business questions you will answer, the SQL features each question uses, and where you will publish it.
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Theory questions you will be askedInterview questions and practice projects · same module
- Classic interview queries, part 2Interview questions and practice projects · same module
- Careers, salaries in India and your next stepsLinux and Red Hat (RHCSA to RHCE) · Certifications and career
- Design project: an online storeSchema design and normalisation
- Projects and a Java careerJava Programming (Core to Spring Boot) · Design patterns, JVM internals and interviews
- GROUP BYAggregation: GROUP BY and HAVING
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
What is AOV (average order value)?
Answer: Revenue divided by the number of orders
Total each order first, then average those totals.
Why is the status filter put in the ON clause of the LEFT JOIN in the cohort query?
Answer: So customers with no matching orders are kept instead of being filtered out
A WHERE condition on the right-hand table removes the NULL rows and turns the LEFT JOIN into an inner join.
Which query pattern finds customers who never ordered?
Answer: customers LEFT JOIN orders ... WHERE o.id IS NULL
The anti-join keeps customers with no matching order.
What is the most honest way to talk about SQL salaries?
Answer: Give rough ranges and say they vary by city, company, skills and year
Pay varies widely; check current listings for your city and role.
You want a database certification. What should you do first?
Answer: Check the current exam page on the vendor's website
Exam names, codes and content change; the vendor page is the source of truth.
To get each customer's product categories in SHOP, which tables must the query pass through?Related
Answer: customers, orders, order_items and products
Keys connect customers → orders → order_items → products; you cannot skip a link.
Which is a good check after building revenue per month?Related
Answer: The months add up to the total revenue with the same WHERE
Groups must add up to the total worked out separately.
Which statement about salaries is most honest?Related
Answer: Pay ranges vary widely by city, company type, skills and interviews
Ranges are guides, not promises.
Which constraint enforces "a customer can review a product only once"?Related
Answer: UNIQUE (product_id, customer_id) on reviews
The UNIQUE pair rejects a second review by the same customer for the same product.
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- This is the last topic of “Interview questions and practice projects”. The module exam comes next.
- Module 12 exam: interview questions and projects · 13 questions · 25 min, without help. 5 topics of this module still to finish first.
- 62 topics left in the course before the final exam.
← Previous: Project: a railway booking database (IRCTC-like)