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_noteproduct_idquantitystatus
DN-4711760ok
DN-4711124ok
DN-471142damaged
DN-4712740ok
DN-471283ok
products 12 rows, 5 columns
product_idnamecategorypricestock
1Copy paper, 500 sheetsPaper4.99420
2Blue ballpoint pens, 10-packWriting3.49310
3Lever arch file, grayFiling2.79185
4Comfort office chairFurniture18912
5Standing deskFurniture4495
6Black ink cartridgePrinter supplies24.964
7Yellow sticky notes, 12 padsWriting6.990
8Mono laser printer L30Electronics2198
927-inch monitorElectronics27915
10Notebook A5, ruledPaper5.49140
11Black toner XLPrinter supplies89.922
12Cross-cut shredderElectronics1290

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_idnamecategorypricestock
1Copy paper, 500 sheetsPaper4.99420
2Blue ballpoint pens, 10-packWriting3.49310
3Lever arch file, grayFiling2.79185
4Comfort office chairFurniture18912
5Standing deskFurniture4495
6Black ink cartridgePrinter supplies24.964
7Yellow sticky notes, 12 padsWriting6.990
8Mono laser printer L30Electronics2198
927-inch monitorElectronics27915
10Notebook A5, ruledPaper5.49140
11Black toner XLPrinter supplies89.922
12Cross-cut shredderElectronics1290
15Thermal fax paper, 3 rollsPaper8.990
16Overhead transparencies, 50-packPrinter supplies12.490
orders 18 rows, 5 columns
order_idcustomer_idorder_datestatusemployee_id
100112026-01-08delivered3
100242026-01-12delivered4
100332026-01-20delivered3
100472026-02-03delivered4
100522026-02-11cancelled8
100612026-02-25delivered3
1007102026-03-04delivered2
100852026-03-17delivered4
100982026-03-30delivered3
101062026-04-07delivered8
1011122026-04-22delivered4
101232026-05-06delivered3
101392026-05-19cancelled8
101472026-06-02shipped4
101512026-06-10shipped3
1016112026-06-12openNULL
101742026-06-13openNULL
1018102026-06-14openNULL
order_items 38 rows, 4 columns
order_idproduct_idquantityunit_price
10011404.79
10013252.79
10016424.9
1002223.49
10021035.49
10031604.69
100311684.9
10033502.59
100443179
100493269
100551449
10061304.79
10067106.99
100782219
100711489.9
10071204.99
100846175
1008253.49
1009101204.99
10092403.19
100992279
1010726.99
1010213.49
1011121129
10111104.99
10113122.79
10126823.9
10121404.69
101391279
101452439
101442179
10151504.79
101511289.9
10161045.49
1017233.49
1017726.99
10181804.79
101861024.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.

Level 4 complete

Next up is level 5, advanced queries. It starts with WITH: you give intermediate results a name and build long queries in clear, readable steps.

Learn WITH (CTE)

Write to us

Choose language

Select your preferred language