…
…
From two weeks of sales records, find the biggest orders, the payment methods, the parts of the day, the best days and the products that earn the most.
Corner Café in Port Alder now writes every sale from the till into a table. You have the 291 sales of the two weeks from November 2 to 15, 2026: which day, which hour, which item, how many, the unit price and how it was paid. The owner wants answers from this table: which are the biggest orders, how much do the sales without payment information add up to, which part of the day earns more, is the weekend really busier, how many of an item does a customer buy in one sale, which days earn the most, and which items are the backbone of revenue.
Data: one table, sale (id, sale_date, sale_hour 0–23, item, category, quantity, unit_price, payment: card, cash or empty). Prices are in dollars.
Lessons used: SELECT, WHERE, sorting, NULL and CASE (3–8), aggregation, GROUP BY and HAVING (11–13), type conversion (25), date functions (27).
Every step only reads; the data does not change. You can run a cell as many times as you like.
What you'll do
cafe.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.
First get to know the table. The barista is curious about sales where 3 or more lattes were ordered at once; these are usually a table's group order.
List the id, sale_date, sale_hour and quantity of the sales where item is latte and quantity is at least 3. Sort by date, then hour, then id.
This step's code cell is for members.Sign up free
The table has no sale amount; it is calculated by multiplying quantity by unit price. You can give a calculated column a name with AS and sort by that name.
For every sale show id, item, quantity
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
amountidThis step's code cell is for members.Sign up free
At the till, some sales have no payment method selected; the payment column is NULL. If you group, the NULLs form a group of their own, but they show up as an empty cell in the report. With COALESCE you can give empty values a readable name.
Show the number of sales (sales) and the revenue (revenue) by payment method. An empty method should be written as unknown; name the column method. Sort from the most sales to the fewest.
This step's code cell is for members.Sign up free
The café is open from 8 in the morning to 10 at night. The owner wants to put staff on the right hours, so she wants to see how much is sold in each part of the day.
Sales before hour 12 are morning, hours 12–16 are afternoon, and hour 17 and later are evening. Show the number of sales (sales) and the revenue (revenue) for each part; the part with the highest revenue comes first.
This step's code cell is for members.Sign up free
The two weeks have 10 weekdays and 4 weekend days. Comparing total revenue is misleading because the number of days differs. The right comparison is the daily average.
EXTRACT(ISODOW FROM sale_date) gives 1 for Monday and 7 for Sunday. Label 6 and 7 as weekend and the others as weekday (day_type). For each type show the number of days (days, the number of distinct dates), the revenue (revenue) and the daily average revenue (daily_avg, rounded to a whole number). Sort by day_type.
This step's code cell is for members.Sign up free
While reorganizing the menu, the café owner asks: when a customer buys an item, how many do they buy? Items that are often bought several at a time will go at the front of the counter.
For each item calculate the average quantity in one sale: divide the total quantity by the number of sales. Show the item (item) and the average quantity (avg_quantity, rounded to two decimals); the item with the highest average comes first.
This step's code cell is for members.Sign up free
The café owner is thinking of calling in an extra waiter on the busiest days. First she wants to look at the past: which are the three days with the highest revenue in the two weeks, and which day of the week do they fall on?
List the three days with the highest revenue: the day (day, in MM/DD format: month and day), the weekday number (weekday, Monday 1, Sunday 7) and the revenue of that day (revenue). The day with the highest revenue comes first.
This step's code cell is for members.Sign up free
The last question: which items bring in the café's real income? You will find the items that earned at least $125 in the two weeks. To filter after grouping, you use HAVING, not WHERE.
Calculate the total quantity (quantity) and the revenue (revenue) by item and category. Show only the items with a revenue of at least $125, starting with the highest revenue.
This step's code cell is for members.Sign up free