…
…
Review the price, product, order and supplier records of a garden shop with SELECT first, then change them safely with INSERT ... SELECT, UPDATE and DELETE, and catch two wrong-WHERE traps.
Green Pot is a small online shop that sells seeds, seedlings, fertilizer, pots and garden tools. The year is ending and the owner wants to tidy the records: the suppliers' new price list will be applied, some products that ran out of stock will come off sale, canceled orders will move to an archive, and suppliers without products will leave the list. You will make every change in the same pattern: first write a SELECT with the same condition to see and count the rows that will be affected, then make the change, then check the result with RETURNING or a check query. Along the way you will catch two wrong-WHERE traps.
Data: four tables, fictional sample data.
supplier: id, name, city (7 suppliers)product: id, name, category ('Seed', 'Seedling', 'Fertilizer', 'Pot', 'Tool'), price, stock, active (on sale or not), supplier_id (empty for products the shop makes itself) (30 products)customer_order: id, product_id, quantity, order_date, status ('delivered', 'pending' or 'canceled') (60 orders, November 2026)price_quote: product_id, new_price (12 quotes from the suppliers, not yet reviewed)Lessons used: subqueries and NOT EXISTS (20–21), INSERT (34), UPDATE (35), DELETE and safe changes (36). No new concept.
Project cells are permanent: Every command you run here stays in the database; it is not rolled back as in lesson exercises. So each command is built to do no harm when run a second time: the ledger and the archive are protected with IF NOT EXISTS, rows are added to the ledger with ON CONFLICT DO NOTHING so they are not repeated, and updates write the same value again. Run the cells in order; if you rerun an earlier cell, rerun the later ones in order too. In the first comment line of the write cells ("Why this query: ..."), write your own sentence before you write the query (it is not checked). Do not change the -- Check query lines at the end of the cells.
What you'll do
Required topics
green-pot.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.
Two suppliers sent a new price list; it is waiting in the price_quote table. The shop owner wants to apply it as it is, but you will look first: writing an unchecked list into prices means carrying one wrong row into hundreds of orders.
For every quote, show the product id, the product name, today's price, the quoted new price and the difference between them (diff). If the list contains a product the shop does not carry, that quote should still appear, with the name and today's price left empty. Sort the result by product id.
Look at the result carefully: how many quotes can really be applied? You will act on that answer in the next step.
This step's code cell is for members.Sign up free
The review showed three problems: product 31 is not in the shop, the new price for liquid plant food is empty, and the price for basil seeds is already the same. Those three quotes will not be applied. Before writing the rest into the prices, you will record them in a price ledger: which product's price was what, and what it becomes. The ledger is both the plan for the change and the way back to the old prices if something goes wrong.
The ledger table is already in the cell. Thanks to IF NOT EXISTS, running the cell again does not drop and rebuild the table, so the rows you wrote stay. You will add the usable quotes to the ledger with a single INSERT ... SELECT
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
ON CONFLICT DO NOTHINGHow many rows should the ledger have? Take the 12 quotes from the review query and subtract three. If the row count in the check query is different, rethink your condition.
This step's code cell is for members.Sign up free
The ledger is ready and holds 9 rows. Now you will update the prices in the product table with the new prices from the ledger. Each product's new price is in its own ledger row, so a subquery that looks up the id of the product being updated produces the new value inside SET.
Two things to watch. First, only the products in the ledger should change: without a WHERE, the subquery returns an empty value (NULL) for the 21 products that are not in the ledger, and the command tries to blank their prices too. Here the price column is NOT NULL, so the command is rejected; in a column that allows NULL it would be written silently. Second, you must see the change with your own eyes: add RETURNING at the end with the product id, name and new price.
The number of rows RETURNING gives back should equal the number of rows in the ledger. When the cell runs again, the command writes the same prices again, so the result does not change.
This step's code cell is for members.Sign up free
The owner wants to simplify the seed and fertilizer shelves: active products in these two categories that are out of stock will be taken off sale. Before making the change, a friend wrote the SELECT that lists them. The query does not fail and it lists nine products.
Look at the result: the worm compost with 25 in stock is on the list. If this query were moved into the WHERE of an UPDATE, the fertilizers in stock would be taken off sale too. Fix the condition: the list must show only active seeds and fertilizers whose stock is 0. This is a read query; you can run the cell as often as you like.
This step's code cell is for members.Sign up free
The list you fixed shows five products. You will not delete these from the product table: past orders refer to them, and they may go back on sale when stock arrives. Instead you will set the active column to FALSE; the product stays in the table and disappears from the storefront.
Write the UPDATE using exactly the condition you checked in the previous step. The command should change 5 rows: the number the SELECT counted. If you like, add RETURNING id, name at the end to see the changed products; since the cell shows only the result of the last query, you will see the check query on screen.
The check query lists every product with stock 0. All the seeds and fertilizers should be false (sunflower seeds were already off sale); pots, seedlings and tools should stay true even without stock.
This step's code cell is for members.Sign up free
The canceled orders of November crowd the order list; accounting wants them out of the main table, but kept somewhere. The order of an important delete is fixed: first copy the rows to be deleted into a backup table, then delete with the same condition, and finally check with counts.
You will write two commands. The first creates the order_archive table from the canceled orders; use the CREATE TABLE IF NOT EXISTS ... AS SELECT form so that running the cell again does not overwrite the backup (by then no canceled orders are left, so an overwrite would empty the backup). The second deletes the canceled orders with the same condition.
The check query shows three numbers: the orders in the archive, the canceled orders left in the main table, and all remaining orders. The archive count and the deleted count must be the same; no canceled order should remain in the main table.
This step's code cell is for members.Sign up free
The last cleanup is in the supplier list: suppliers that have no products in the shop will be removed. Your friend wrote the command and ran it. It raised no error, but RETURNING returned no rows, and the check query still shows all seven suppliers. Yet a SELECT shows that at least two suppliers have no products at all.
Find out why the command deleted no rows and fix it. A hint from the data: some products are made by the shop itself, so they have no supplier. After your fix, RETURNING shows the deleted suppliers once; the check query should leave only suppliers that have products. If you tried to delete a supplier that has products, the foreign key would reject the command.
This step's code cell is for members.Sign up free
All the changes are done. The shop owner finally wants a summary per category: how many products each category has, how many of them are on sale (active) and what the average price of the products on sale is. The report also shows that the changes settled as expected: the active count of seeds and fertilizers should have dropped, and prices should follow the new list.
Columns: category, products (total products), active_products (products on sale) and avg_price (average price of the active products only, rounded to 2 decimals). Sort by category name. After you write the report, put one sentence in the first line of the cell saying which number was affected by which change.
This step's code cell is for members.Sign up free