Skip to content

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

Answer the questions every online shop asks, on the SHOP database: monthly revenue, average order value, repeat customers, top products, customers who never ordered and a simple cohort by join month. Then plan what comes next: practice sites, a portfolio of queries, the jobs that use SQL (with honest salary ranges) and certifications.

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.

sql
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.71
AOV (average order value) = revenue / number of orders.

Monthly revenue

sql
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 | 650
Topic 2 added month-over-month growth on top of this.

Repeat 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.

sql
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 Reddy

Top products

sql
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
-- ...
Group by id as well as name, in case two products share a name.

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:

sql
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
-- ...
The status rule sits in ON, not WHERE, so customers with no orders stay in the count. SHOP is tiny, so each cohort has one person; real shops have thousands.

Your next steps

  1. 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.
  2. 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.
  3. 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

RoleWhat you do with SQLRough pay in India (varies widely)
Data analystReports, dashboards, ad-hoc questionsAbout ₹3–7 LPA for freshers; ₹8–15 LPA with a few years
BI developerData models and dashboards in Power BI or TableauAbout ₹4–8 LPA for freshers; ₹10–20 LPA with experience
Backend developerSchemas, queries and migrations inside appsAbout ₹4–10 LPA for freshers; much higher at top product companies
Data engineerPipelines that move and transform data into warehousesAbout ₹5–10 LPA for freshers; ₹12–30 LPA mid-level
Database administrator (DBA)Backups, performance, security, upgradesAbout ₹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

E-commerce analytics

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.

sql
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.71
One view, one agreed definition, reused by every report.

Practice (0/4 done)

Task 1 · Code · SQL

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.

Task 2 · Code · SQL

Repeat customers: return id, name and delivered_orders for customers with 2 or more orders whose status is 'Delivered'.

Task 3 · Code · SQL

Top 2 products by revenue (SUM(qty * price) over lines of orders that are not 'Cancelled'). Return name and revenue, highest first.

Task 4 · Practice

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

Question 1

1.What is AOV (average order value)?

Question 2

2.Why is the status filter put in the ON clause of the LEFT JOIN in the cohort query?

Question 3

3.Which query pattern finds customers who never ordered?

Question 4

4.What is the most honest way to talk about SQL salaries?

Question 5

5.You want a database certification. What should you do first?

Read more (free)

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.

From Joining three or more tables

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.

From Writing reports with aggregation

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.

From Careers, salaries in India and your next steps

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.

From Design project: an online store

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

  1. This is the last topic of “Interview questions and practice projects”. The module exam comes next.
  2. Module 12 exam: interview questions and projects · 13 questions · 25 min, without help. 5 topics of this module still to finish first.
  3. 62 topics left in the course before the final exam.

← Previous: Project: a railway booking database (IRCTC-like)