Back
Case study
SQL + Python

Olist E-Commerce Sales Analysis

Where is the business doing well, and where is it losing money? Here's how I answered that for a Brazilian marketplace, step by step, from messy raw data to findings the team can act on.

Role
Data Analyst
Data
~100k orders
Output
11 SQL queries
Stack
View code on GitHub
Step 01

The question

Olist is a Brazilian online marketplace that connects small sellers with buyers all over the country. I pictured myself as a data analyst there, with my manager asking “How is the business doing, and what should we fix first?”

To answer that, I used Olist's public data on Kaggle: about 100,000 real orders from 2016 to 2018, split across seven tables.

Orders99,441
Order items112,650
Payments103,886
Customer reviews99,224
Customers99,441
Products32,951
Sellers3,095
Step 02

Breaking it down

One number can't answer a question that big. So before writing any code, I broke it into six smaller questions, each one small enough to answer with a single SQL query.

Is the business growing?Revenue and orders per month
What sells?Revenue and items sold per category
Where are the customers, and does distance matter?Orders, delivery days and freight per state
Do late deliveries really hurt?Review score by how late the order arrived
Do customers come back?Number of orders per customer
Do a few sellers bring in most of the money?How much each group of sellers earns (top 10%, next 10%, …)

I used SQL (SQLite) for the analysis itself, and Python (pandas, Matplotlib) to load the data, double-check the results and draw the charts.

Step 03

Cleaning the data

Raw data is almost never ready to use. Before analyzing anything, I looked for problems that could quietly make the numbers wrong. This card from the dashboard counts what I found:

Data checks

found before analysis
  • Orders not delivered2,963
  • Repeat buyers hidden by customer ID3,345
  • Orders without any item775
  • Products without a category610
  • Orders with 2+ reviews547
  • Payment ≠ items + freight249
  • Delivered, but no delivery date8

And this is what I decided to do about each one:

2,963 orders were never delivered (shipped, canceled, unavailable…)Left out of revenue and delivery numbers
8 delivered orders have no delivery dateLeft out, since delivery time can't be calculated
775 orders have no itemsThey drop out on their own when I join the items table
547 orders have more than one reviewKept only the latest review per order
A new customer_id is created for every orderUsed customer_unique_id to recognize the same person. Otherwise, 3,345 repeat buyers disappear.
610 products have no categoryLabeled them “unknown” so they still count
249 orders where payments don't match items + freight (by more than R$1)Calculated revenue from item prices, and only used payments for the payment mix
2016 and late 2018 are incompleteMonthly trends only cover Jan 2017 to Aug 2018
What counts as “late”?Arriving after the promised date. Arriving on the day itself isn't late.

I wrote each decision once, as a SQL view, so all 11 queries use the exact same clean data. Here's the main one:

-- Only orders that really reached the customer.
-- Everything about revenue and delivery is based on this view.
CREATE VIEW delivered_orders AS
SELECT o.order_id,
       c.customer_unique_id,  -- the real person (customer_id changes per order)
       c.customer_state,
       strftime('%Y-%m', o.order_purchase_timestamp) AS purchase_month,
       julianday(o.order_delivered_customer_date)
         - julianday(o.order_purchase_timestamp)     AS delivery_days,
       -- arriving on the promised day is NOT late
       CASE WHEN date(o.order_delivered_customer_date)
                 > date(o.order_estimated_delivery_date)
            THEN 1 ELSE 0 END                        AS is_late
FROM orders o
JOIN customers c ON c.customer_id = o.customer_id
WHERE o.order_status = 'delivered'
  AND o.order_delivered_customer_date IS NOT NULL;
Step 04

Double-checking

A wrong number is worse than no number. So I calculated the main numbers a second time in pandas, straight from the raw files. If SQL and pandas didn't match, the script would stop with an error.

Delivered orders96,470 vs. 96,470 ✓
RevenueR$13,220,248.93 vs. R$13,220,248.93 ✓
Late deliveries6.80% vs. 6.77% ✓ (rounding)
Repeat customers3.00% vs. 3.00% ✓

All four matched, so I could trust the numbers in the next step.

Step 05

Reading the dashboard

The dashboard on the home page sums up the whole analysis. Below is every card from it: what it shows, how to read it, and what I learned.

The four KPI cards

Orders96,470
Up3.2%vs Jul 2018
RevenueR$13.2M
Down3.3%vs Jul 2018
Avg. order valueR$137
Down6.3%vs Jul 2018
Avg. review4.16 / 5
Stablevs Jul 2018

How to read a KPI card: the big number covers the whole period (Jan 2017 to Aug 2018). The badge compares the last month with the one before (Aug vs. Jul 2018): green means better for the business, red means worse, grey means flat. The line shows the trend month by month.

Orders · 96,470Orders that reached the customer, from 93,350 different people.
Revenue · R$13.2MMoney from item prices on delivered orders. Freight isn't included.
Avg. order value · R$137Revenue divided by orders. It stayed between R$124 and R$149 every month.
Avg. review · 4.16 / 5The latest review on each delivered order, from 95,824 reviews.

Monthly revenue

Monthly revenue

BRL
R$1MR$500k0
Jan 2017Jan 2018Aug 2018
What it shows
Revenue from delivered orders each month, from January 2017 to August 2018.
How to read it
Each point on the line is one month. Higher means more sales. The labelled dots mark the best month and the latest one.
What it tells us
Revenue grew about 9× in 2017, from R$112k in January to a peak of R$988k in November. In 2018 it stayed flat at around R$826k–978k a month, ending at R$839k in August. The average order value barely changed, so the growth came from more orders, not bigger ones.
Why it matters
The fast growth is over, and revenue has been slowly dropping since May 2018. Future growth has to come from keeping customers and fixing the weak spots. The November spike is probably Black Friday, but I haven't checked that in the data.

Sales by category

Sales by category

% of revenue
  • Health & beauty9.3%
  • Watches & gifts8.8%
  • Bed, bath & table7.7%
  • Sports & leisure7.2%
  • Computers6.7%
  • Furniture & decor5.4%

These 6 categories bring in 45.2% of revenue; all the others 54.8%.

What it shows
The six product categories that bring in the most revenue, as a share of all revenue.
How to read it
A longer bar means a bigger share.
What it tells us
Health & beauty, watches & gifts and bed, bath & table lead. Together, the top six bring in 45.2% of revenue, so sales are spread across many categories rather than depending on one.
Why it matters
A spread-out mix is safer, but the top six are where improvements pay off most.

Top revenue categories

Top revenue categories

BRL, no freight
  • Health & beautyR$1.23M
  • Watches & giftsR$1.17M
  • Bed, bath & tableR$1.02M
  • Sports & leisureR$955k
  • ComputersR$889k

Watches & gifts is #2 in revenue thanks to a high price: R$199 per item on average.

What it shows
The five categories with the highest revenue, in Brazilian reais (item price only, no freight).
How to read it
A longer bar means more money.
What it tells us
Health & beauty leads with R$1.23M. Watches & gifts is close behind at R$1.17M, even though it sells far fewer items, because each item costs R$199 on average.
Why it matters
High-price categories can earn as much as high-volume ones, so they deserve the same attention.

Best-selling categories

Best-selling categories

items sold
  • Bed, bath & table10,953
  • Health & beauty9,465
  • Sports & leisure8,430
  • Furniture & decor8,160
  • Computers7,643

Bed, bath & table sells the most items, but at only R$93 each on average.

What it shows
The five categories that sell the most items.
How to read it
A longer bar means more items sold.
What it tells us
Bed, bath & table sells the most items (10,953), but at only R$93 each. Watches & gifts isn't in this top five at all, yet it's #2 in revenue.
Why it matters
Revenue alone doesn't show how a category makes money: some sell lots of cheap items, others sell fewer expensive ones. That changes where you'd invest.

Rating distribution

Rating distribution

orders per star rating
9.4k
2.9k
7.9k
18.9k
56.7k
1 ★2 ★3 ★4 ★5 ★
What it shows
How many delivered orders got each star rating, using the latest review per order (95,824 reviews).
How to read it
Each bar is one star rating. A taller bar means more orders.
What it tells us
Most customers are happy: 56.7k orders got 5 stars and 18.9k got 4. But 9.4k orders got just 1 star, more than 2 and 3 stars combined.
Why it matters
Those 1-star reviews are worth a closer look. The next card shows that many of them come from late deliveries.

Review vs. delivery

Review vs. delivery

avg. score / 5
4.29
3.29
2.10
1.70
On time6.6%1–3d late25.1%4–7d late58.6%8+d late69.8%

Number under each group: share of 1-star reviews.

What it shows
The average review score (1 to 5), grouped by how late the order arrived compared with the promised date. The number under each group is the share of 1-star reviews.
How to read it
Each bar is one delay group. A taller bar means happier customers.
What it tells us
Arriving just 1–3 days late drops the score from 4.29 to 3.29, and the share of 1-star reviews goes up almost 4× (6.6% → 25.1%). When an order is 8+ days late, the score falls to 1.70, and 69.8% of those reviews are 1-star.
Why it matters
This is the clearest pattern in the data. Only 6.8% of orders arrive late, but they cause a big part of the bad reviews. Fixing delivery is the fastest way to protect ratings.
Step 06

Recommendations

Based on what I found, here's what I'd fix first, in order.

Fix logistics in remote regionsMissing the promised date by just 1–3 days costs about a star. Better courier SLAs and more realistic delivery estimates would help.
Build a retention program97% of customers buy once. Test a voucher or email right after a good delivery experience.
Manage seller concentrationThe top 10% of sellers bring in 67% of revenue. Give them account managers, and review the big sellers with low ratings.
Test the impact of high freightFreight is 24–28% of revenue in some remote states and may put buyers off there. That's a hypothesis to test; this data can't prove it.
Step 07

What the data can't prove

Knowing what the data can't tell us is just as important as the findings.

It only shows patternsIt shows what happens together, not what causes what. For example, late orders might also come from different products, sellers or regions.
Revenue is item price onlyFreight is analyzed separately, so revenue looks smaller than total payments.
Some data wasn't used yetI didn't use the review text (it's in Portuguese) or the location data.
The last months are incompleteTrends stop at August 2018, and August delivery times look better than they were because orders in transit aren't counted.