…
…
Use joins to find order totals, top customers, category revenue and loyal customers in a four-table online store.
Campus Stationery is an online store that sells notebooks, pens, books and small electronics to students. Between September and November 2026, 80 orders came in from 30 registered customers. The owner wants answers to plan a campaign: which orders are the biggest, which customers spend the most, who has never ordered, how much each category earns, how many orders are above average, and who the loyal customers are.
Data: four tables. customer (id, first_name, last_name, city, signup_date), product (id, name, category: stationery, electronics or books, price), shop_order (id, customer_id, order_date, status: delivered, shipped or canceled) and order_item (order_id, product_id, quantity, unit_price). The total of an order is the sum of quantity × unit price over its items. All values are fictional sample data.
Lessons used: grouping (11–13), joins (15–18), subqueries (20–21), set operations and CTEs (22–23).
Every step only reads data; you can rerun the cells as often as you like.
What you'll do
stationery.sqlDownload file
Reading file…
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
The order table has no total; the total is calculated from the order's items. First you will join the two tables and find each order's total.
Join shop_order with order_item. Show the id, order_date and total of the 5 orders with the highest totals; when totals are equal, the smaller id comes first.
This step's code cell is for members.Sign up free
Now you will join three tables: customer → order → item. Canceled orders and orders that are still shipped do not count as spending; only delivered ones do.
Based on delivered orders, show the first_name, last_name, number of orders (orders, distinct orders) and total spending (spent) of the 5 customers who spent the most. The highest spender comes first; when spending is equal, the customer with the smaller
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
idThis step's code cell is for members.Sign up free
The store will send a welcome discount to customers who signed up but never ordered. An inner join does not show these customers, because there are no matching order rows. A LEFT JOIN brings the unmatched customers with NULLs.
Show the id, first_name, last_name and city of customers who have no orders, sorted by id.
This step's code cell is for members.Sign up free
How much does each category earn? You will join items with the product (for the category) and with the order (for the status). Canceled orders are not counted as revenue; shipped ones are.
For orders that are not canceled, show each category's units sold (units) and its revenue (revenue). The category with the highest revenue comes first.
This step's code cell is for members.Sign up free
What is the average order total, how many orders are above it, and how much of the total revenue do these orders bring? If you calculate the order totals in a CTE, you can take the average and the total revenue from the same CTE with subqueries.
Calculate the total of every order that is not canceled in a CTE named order_total. Show the number of orders whose total is greater than the average of these orders (orders), the sum of their totals (total) and this sum's percentage share of the whole revenue (share, one decimal).
This step's code cell is for members.Sign up free
The new campaign will be a "book + electronics" bundle, sent to customers who have bought from both categories. The intersection of the list of people who bought from one category and the list of people who bought from the other is found with INTERSECT.
Find the intersection of the customer numbers who bought from the books category and the customer numbers who bought from electronics. Show the id, first_name and last_name of these customers, sorted by id.
This step's code cell is for members.Sign up free
In the last step you will pick the customers for the loyalty program: customers with at least 4 delivered orders. First calculate each customer's number of delivered orders and spending in a CTE, then filter in the outer query and join with the customer names.
Name the CTE delivered; for each customer it should calculate customer_id, orders (distinct orders) and spent. Show the first_name, last_name, orders and spent of customers with at least 4 orders; the customer with more orders comes first, and ties go to the higher spending.
This step's code cell is for members.Sign up free