Skip to content
wannacode

The bakery, stage 3: the questions

In short: Stage 3 answers the bakery's questions with JOIN, GROUP BY, LEFT JOIN, WITH and COALESCE: the menu, the bills, the best sellers and the revenue by city.

Problem

What they want to know

The bakery has data. Now it wants answers: what to write in the menu, how much each customer paid, what to bake more of, whom to send a discount, which orders are big and where the money comes from.

Every question is one query to the database from stage 2. Nothing is written: the database is the same after every query.

The idea

The cheap things

The simplest question is to one table: the products under €2, from the cheapest up.

SQLempty database

Try it

The menu

Make the menu: the category (column category), the name and the price of every product. Order it by category in the order of their numbers (bread, pastries, drinks), and within the category from the cheapest product.

SQLempty database

The idea

The bill

Every row of the order costs quantity * unit_price. Desislava's order 5:

SQLempty database

The sum of the order is the total of these rows: SUM(quantity * unit_price) with GROUP BY the order.

Try it

Everyone's bills

For every order that is not cancelled: the number, the name of the customer, the date and the sum (column total, rounded to 2 places). Order by number.

SQLempty database
More precisely, if you are curious

GROUP BY o.id, while SELECT has c.name and o.ordered_at, which are not in the group. SQLite allows it. PostgreSQL accepts o.ordered_at, because the primary key of orders is in the group, but not c.name, which is from another table. There you write GROUP BY o.id, c.name, and then the query works everywhere.

Try it

What to bake more of

The three best-selling products by pieces, without the cancelled orders: the name and the count (column pieces), from the best-selling. With equal pieces by name.

SQLempty database

The idea

Who has not ordered

LEFT JOIN keeps every customer, even without an order. There the columns of the order are NULL, and WHERE o.id IS NULL leaves exactly those:

SQLempty database

Try it

A discount for the new ones

The bakery sends a discount to everyone who has no real order: no orders or only cancelled ones. Show their names in alphabetical order.

SQLempty database

Try it

The big orders

Which orders (without the cancelled ones) are above the average sum per order? The number and the sum (total, rounded to 2 places), by number. Make the sums with WITH totals AS (…).

SQLempty database

Exercise

Where the money comes from

The revenue by city from the orders that are not cancelled, for all the cities of the customers. A city without revenue has 0, and the customers without a city are under 'no city'. Columns city and revenue (rounded to 2 places), from the largest revenue.

SQLempty database
More precisely, if you are curious

SUM over a group with only NULL gives NULL, not 0. That is why it is COALESCE(SUM(…), 0). The sum of an order is a decimal, so it is ROUND: 0.1 + 0.2 in REAL is not exactly 0.3.

The idea

What comes next

All these queries only read. The next stage changes the data: prices, statuses, stock, deleted orders.

Test

Check yourself

1. Why does the bill of the order add up quantity * unit_price and not price from products?
2. LEFT JOIN orders o ON o.customer_id = c.id and then WHERE o.status <> 'cancelled'. What happens to the customers without orders?
3. The average per order is €9.40. Why not write WHERE total > 9.4?

Made by Ivo Gergov as part of Evo Tech's campaign for accessible programming education ·