Level 4 project: inventory
To wrap up level 4 you work the way a real shop's stock team does: book a goods receipt into a new table and into the stock levels, then tidy up the product range and create a view for stock value.
You'll need: CREATE TABLE, primary and foreign keys, INSERT (including INSERT … SELECT), UPDATE, DELETE, views, transactions and subqueries from Level 3.
The situation
A delivery arrived at the office supplies shop this morning. Goods-in has entered every line of the delivery notes in a table, along with the condition of the goods. But there's no table yet for recording stock movements, and the stock levels in products haven't been updated.
In the afternoon, purchasing wants to tidy up the range: delete old items with no stock, mark down slow sellers, and be able to see stock value per category at any time.
Both tasks change data. So after your statements run, the exercise doesn't show your last SELECT. It shows a check instead: a report, or your view after the check has changed some data.
The project
Part 1: Book the goods receipt
Three steps: create the table, copy the movements over from goods_receipt, raise the stock levels. The last two belong together, so they go in one transaction. Run after each step and read the report.
Task Goods arrived on 2026-06-16, and the goods_receipt table lists every line of the delivery notes. Book the receipt: (1) Create the table stock_movements with movement_id (INTEGER PRIMARY KEY), product_id (INTEGER, required, foreign key to products), quantity (INTEGER, required) and movement_date (TEXT, required). (2) Copy every line with status 'ok' into it with INSERT … SELECT, one movement per line, dated 2026-06-16. Damaged goods are not booked. (3) Raise stock in products by the booked quantities; one product is on two delivery notes. Steps 2 and 3 run in one transaction. The check then shows a report: table structure, movements, stock levels.
goods_receipt 5 rows, 4 columns
| delivery_note | product_id | quantity | status |
|---|---|---|---|
| DN-4711 | 7 | 60 | ok |
| DN-4711 | 12 | 4 | ok |
| DN-4711 | 4 | 2 | damaged |
| DN-4712 | 7 | 40 | ok |
| DN-4712 | 8 | 3 | ok |
products 12 rows, 5 columns
| product_id | name | category | price | stock |
|---|---|---|---|---|
| 1 | Copy paper, 500 sheets | Paper | 4.99 | 420 |
| 2 | Blue ballpoint pens, 10-pack | Writing | 3.49 | 310 |
| 3 | Lever arch file, gray | Filing | 2.79 | 185 |
| 4 | Comfort office chair | Furniture | 189 | 12 |
| 5 | Standing desk | Furniture | 449 | 5 |
| 6 | Black ink cartridge | Printer supplies | 24.9 | 64 |
| 7 | Yellow sticky notes, 12 pads | Writing | 6.99 | 0 |
| 8 | Mono laser printer L30 | Electronics | 219 | 8 |
| 9 | 27-inch monitor | Electronics | 279 | 15 |
| 10 | Notebook A5, ruled | Paper | 5.49 | 140 |
| 11 | Black toner XL | Printer supplies | 89.9 | 22 |
| 12 | Cross-cut shredder | Electronics | 129 | 0 |
This exercise is part of Premium.
Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.
See Premium Already Premium? Log in
Part 2: Clean up the product range
Delete, mark down, create a view. Write each condition as a SELECT first and look at which products it hits before you put DELETE or UPDATE in front of it.
Task Purchasing is tidying up the product range. (1) Delete every product with no stock that has never been ordered, meaning it appears in no order item. Products from earlier orders stay, even with no stock. (2) Products that were ordered before but not since 2026-04-01 go on clearance: new price ROUND(price * 0.85, 2). Cancelled orders don't count. (3) Create the view stock_value_by_category with category, products (number of products) and stock_value (sum of price times stock) per category. Afterwards the check adds stock to one product and reads your view.
products 14 rows, 5 columns
| product_id | name | category | price | stock |
|---|---|---|---|---|
| 1 | Copy paper, 500 sheets | Paper | 4.99 | 420 |
| 2 | Blue ballpoint pens, 10-pack | Writing | 3.49 | 310 |
| 3 | Lever arch file, gray | Filing | 2.79 | 185 |
| 4 | Comfort office chair | Furniture | 189 | 12 |
| 5 | Standing desk | Furniture | 449 | 5 |
| 6 | Black ink cartridge | Printer supplies | 24.9 | 64 |
| 7 | Yellow sticky notes, 12 pads | Writing | 6.99 | 0 |
| 8 | Mono laser printer L30 | Electronics | 219 | 8 |
| 9 | 27-inch monitor | Electronics | 279 | 15 |
| 10 | Notebook A5, ruled | Paper | 5.49 | 140 |
| 11 | Black toner XL | Printer supplies | 89.9 | 22 |
| 12 | Cross-cut shredder | Electronics | 129 | 0 |
| 15 | Thermal fax paper, 3 rolls | Paper | 8.99 | 0 |
| 16 | Overhead transparencies, 50-pack | Printer supplies | 12.49 | 0 |
orders 18 rows, 5 columns
| order_id | customer_id | order_date | status | employee_id |
|---|---|---|---|---|
| 1001 | 1 | 2026-01-08 | delivered | 3 |
| 1002 | 4 | 2026-01-12 | delivered | 4 |
| 1003 | 3 | 2026-01-20 | delivered | 3 |
| 1004 | 7 | 2026-02-03 | delivered | 4 |
| 1005 | 2 | 2026-02-11 | cancelled | 8 |
| 1006 | 1 | 2026-02-25 | delivered | 3 |
| 1007 | 10 | 2026-03-04 | delivered | 2 |
| 1008 | 5 | 2026-03-17 | delivered | 4 |
| 1009 | 8 | 2026-03-30 | delivered | 3 |
| 1010 | 6 | 2026-04-07 | delivered | 8 |
| 1011 | 12 | 2026-04-22 | delivered | 4 |
| 1012 | 3 | 2026-05-06 | delivered | 3 |
| 1013 | 9 | 2026-05-19 | cancelled | 8 |
| 1014 | 7 | 2026-06-02 | shipped | 4 |
| 1015 | 1 | 2026-06-10 | shipped | 3 |
| 1016 | 11 | 2026-06-12 | open | NULL |
| 1017 | 4 | 2026-06-13 | open | NULL |
| 1018 | 10 | 2026-06-14 | open | NULL |
order_items 38 rows, 4 columns
| order_id | product_id | quantity | unit_price |
|---|---|---|---|
| 1001 | 1 | 40 | 4.79 |
| 1001 | 3 | 25 | 2.79 |
| 1001 | 6 | 4 | 24.9 |
| 1002 | 2 | 2 | 3.49 |
| 1002 | 10 | 3 | 5.49 |
| 1003 | 1 | 60 | 4.69 |
| 1003 | 11 | 6 | 84.9 |
| 1003 | 3 | 50 | 2.59 |
| 1004 | 4 | 3 | 179 |
| 1004 | 9 | 3 | 269 |
| 1005 | 5 | 1 | 449 |
| 1006 | 1 | 30 | 4.79 |
| 1006 | 7 | 10 | 6.99 |
| 1007 | 8 | 2 | 219 |
| 1007 | 11 | 4 | 89.9 |
| 1007 | 1 | 20 | 4.99 |
| 1008 | 4 | 6 | 175 |
| 1008 | 2 | 5 | 3.49 |
| 1009 | 10 | 120 | 4.99 |
| 1009 | 2 | 40 | 3.19 |
| 1009 | 9 | 2 | 279 |
| 1010 | 7 | 2 | 6.99 |
| 1010 | 2 | 1 | 3.49 |
| 1011 | 12 | 1 | 129 |
| 1011 | 1 | 10 | 4.99 |
| 1011 | 3 | 12 | 2.79 |
| 1012 | 6 | 8 | 23.9 |
| 1012 | 1 | 40 | 4.69 |
| 1013 | 9 | 1 | 279 |
| 1014 | 5 | 2 | 439 |
| 1014 | 4 | 2 | 179 |
| 1015 | 1 | 50 | 4.79 |
| 1015 | 11 | 2 | 89.9 |
| 1016 | 10 | 4 | 5.49 |
| 1017 | 2 | 3 | 3.49 |
| 1017 | 7 | 2 | 6.99 |
| 1018 | 1 | 80 | 4.79 |
| 1018 | 6 | 10 | 24.9 |
This exercise is part of Premium.
Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.
See Premium Already Premium? Log in
What matters
INSERT … SELECT instead of typing
Rather than entering every movement with VALUES, you pull them straight out of goods_receipt with SELECT. The column list after INSERT INTO leaves out movement_id, which the database assigns itself. The date is the same for every row, so it sits in the SELECT as a fixed piece of text.
One product, two delivery notes
The sticky notes arrive on two delivery notes. That's why the UPDATE needs the total per product: (SELECT SUM(quantity) FROM stock_movements m WHERE m.product_id = products.product_id). Without SUM, SQLite quietly takes the first matching row and the stock comes out wrong.
The WHERE on the UPDATE
For products with no movement, the subquery returns NULL, and stock + NULL is NULL. Since stock is a required column, the database refuses: "products.stock must not be empty (NOT NULL)". Limit the UPDATE to the products that were booked.
The foreign key protects you when deleting
The sticky notes and the shredder have no stock but appear in old orders. A DELETE based only on stock = 0 fails, and the exercise reports "Other rows still point to this row (foreign key)": order items still point to those products. With product_id NOT IN (SELECT product_id FROM order_items) you delete only what was never ordered.
The last valid order
"Not ordered since 1 April" means the latest order per product that wasn't cancelled falls before that date. That's a group with MAX(order_date) and HAVING. Forget about cancellations and the cancelled monitor order from May counts, so the monitor keeps its old price.
If it doesn't work
- If part 1 says there is no table "stock_movements", check the name in CREATE TABLE. If it says the column list doesn't match ("4 expected"), your table doesn't have exactly the four columns from the task.
- Compare the report line by line with the task. If damaged office chairs ended up in stock, the condition on status is missing. If a required column is missing from the Required line, it lacks NOT NULL.
- If the exercise says "Solve this one with COMMIT", the transaction around steps 2 and 3 is missing.
- In part 2, if a category's product count is off, look at your DELETE. If the electronics stock value is off, look at the markdown.
- Both projects are also checked against different data. Typing in product IDs or quantities fails with "The result is right with this data but not with other data".
What you learned in level 4
You can now do more than query: you can maintain data. You add, change and delete rows, build tables with keys and rules, save queries as views, and protect changes that belong together with a transaction. That's the toolkit for looking after a small database yourself.
Frequently asked questions
How many points is the project worth?
Each of the two project tasks counts three times as much as a regular exercise, because you have to solve several steps at once.
Is the project free?
No. Projects from level 2 onwards are part of Premium. The first exercise of every lesson is free, the projects are not.
Is there only one correct answer?
No. The checker looks at the state of the data after your statements, not at their text. Whether you sum the stock from stock_movements or from goods_receipt, whether you round with ROUND or not: if the result is right, the answer is right. Only the commands from the task have to appear, such as COMMIT in part 1 and CREATE VIEW in part 2.
Why does the check change data after my statements?
That's how it tells whether you created a real view. A view recalculates every time it's read and shows the change. A table from CREATE TABLE … AS SELECT would be a copy stuck at the old state.