Skip to content
Lesson 10 of 10 · SQL for Beginners

Final project: analyze the bookshop

Answer real business questions with joins, grouping, subqueries and CASE on the whole dataset.

28 min

You now have the full toolkit. In this lesson you play analyst for the bookshop. The owner wants answers, and each answer is a query on the whole dataset: books, customers, orders and order_items.

Two rules from the owner:

  • Revenue counts only shipped orders. Cancelled and pending orders are not income yet.
  • A line's revenue is qty * price from the book it points at.

Plan each query out loud before you type it: which table do the rows come from, which joins connect them, what filters apply, what is grouped, and how the result is sorted. If a query is long, build it up one clause at a time and run it after each step. That is how professionals write SQL too.

The thinking order for any analysis query.

Exercise1 check
sql

Question 1. Which genre earns the most? Show each genre and its revenue (the sum of qty times price on shipped orders, rounded to 2 decimals), biggest first.

Tables you can query

  • books (id, title, author, genre, price, year, stock)
  • customers (id, name, city, joined, email)
  • orders (id, customer_id, order_date, status)
  • order_items (order_id, book_id, qty)
starter code2 lines
SELECT b.genre,
FROM order_items oi
Exercise1 check
sql

Question 2. Who are the best customers? Show each customer's name and spent (shipped orders only, rounded to 2 decimals), biggest first. Customers with no shipped orders should not appear.

Tables you can query

  • books (id, title, author, genre, price, year, stock)
  • customers (id, name, city, joined, email)
  • orders (id, customer_id, order_date, status)
  • order_items (order_id, book_id, qty)
starter code2 lines
SELECT c.name,
FROM customers c
Exercise1 check
sql

Question 3. How is revenue moving? Show each month (the first 7 characters of order_date, such as 2024-01) and its revenue on shipped orders, rounded to 2 decimals, oldest month first.

Use substr(order_date, 1, 7) to cut the month out.

Tables you can query

  • books (id, title, author, genre, price, year, stock)
  • customers (id, name, city, joined, email)
  • orders (id, customer_id, order_date, status)
  • order_items (order_id, book_id, qty)
starter code2 lines
SELECT substr(o.order_date, 1, 7) AS month,
FROM orders o
Exercise1 check
sql

Question 4. What should be restocked? Show the title and the units sold (all orders) of books that have been sold at least 2 times in total and have fewer than 10 left in stock.

Tables you can query

  • books (id, title, author, genre, price, year, stock)
  • customers (id, name, city, joined, email)
  • orders (id, customer_id, order_date, status)
  • order_items (order_id, book_id, qty)
starter code2 lines
SELECT b.title,
FROM books b
Exercise1 check
sql

Question 5. Who should get a reminder email? Show the name of every customer who has no shipped order at all (they may have cancelled or pending orders, or none).

Tables you can query

  • books (id, title, author, genre, price, year, stock)
  • customers (id, name, city, joined, email)
  • orders (id, customer_id, order_date, status)
  • order_items (order_id, book_id, qty)
starter code3 lines
SELECT c.name
FROM customers c
WHERE

Check your own answer to Question 1: add up the genre revenues from Question 1 and compare with the total of all shipped order lines calculated a different way.

  1. 1

    Revenue by genre: fiction 81.97 + science 69.50 + cooking 67.50 + nonfiction 18.00

    81.97 + 69.50 = 151.47; plus 67.50 = 218.97; plus 18.00 = 236.97.

1/3
  • Start from the question, then choose tables, joins, filters, grouping and ordering.
  • Filter rows with WHERE before grouping; filter groups with HAVING after.
  • Check a result by recalculating it a second way: two groupings of the same rows must give the same total.
  • Make business rules (such as "shipped only") explicit and apply them in every query.
Check yourself

Last checks.

1Multiple choice

Why must a revenue query that joins order_items to orders filter status?

  • ASQL requires it
  • BCancelled and pending orders are not income
  • CIt makes the query faster
  • DOtherwise it returns no rows
2Multiple choice

Which finds "customers with no shipped order"?

  • ALEFT JOIN with no filter
  • BNOT EXISTS with a shipped-status test inside
  • CWHERE status <> 'shipped'
  • DGROUP BY status
3Multiple choice

A join accidentally multiplies rows. What is the first symptom?

  • AAn error message
  • BTotals look too high
  • CSlower typing
  • DMissing columns
4Numeric answer

Fiction revenue in Question 1 is 81.97 and total revenue is 236.97. What percent of revenue is fiction, to the nearest whole number?

5Multiple choice

What does GROUP BY month (an alias) rely on?

  • AAn SQLite feature that allows aliases in GROUP BY
  • BNothing: it is standard everywhere
  • CA missing WHERE
  • DDISTINCT
For you · never graded

Which of the five questions would your own business, club or team ask about its data? Write it as one sentence, and name the tables you would need.

Practise this lesson, not just read it

In BiliMind the questions above are marked when you answer, the flashcards come back on a schedule, and the rest of SQL for Beginners is planned around your week. Free.