…
…
Measure the repetition in the single-table orders of a plant shop, split the table into four, add the keys and join it back to check that no information was lost.
Leafy Corner is a small online plant shop. The owner keeps the October and November 2026 orders in one table: every row is one product in one order, and all the information about the customer and the product sits in that row. In November two products went up in price, one product will be renamed and the shop wants to add new products. With a single table, each of these is a problem. In this project you will examine the table with numbers, split it into the right tables, add their rules and prove that nothing is lost in the split.
Data: one table, fictional sample data. orders_raw: order_no, order_date, customer_no, customer_name, city, product_code, product_name, category, quantity, unit_price (30 orders, 75 rows; no keys or constraints). At the end of the project you will have four tables: customer, product, customer_order and order_item, which connects the two.
Lessons used: grouping and HAVING (11–13), INNER JOIN (15) and row multiplication (18), CREATE TABLE AS and keys (28–30), normalization (32), adding constraints with ALTER TABLE (33). No new concept.
The first three steps only read, and the first line of every cell starts with "Why this query:"; finish it in one sentence before you write the query (it is not checked). After step 4 the cells build and change tables. Project cells are permanent: every command you run stays in the database. So every cell that builds a table first drops the old one with DROP TABLE IF EXISTS; you can rerun a cell as often as you like, but if you rerun an earlier cell, rerun the ones after it in order too. Do not change the -- Check query lines at the end of the cells.
What you'll do
Required topics
plant-orders.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.
The owner of Leafy Corner keeps the October and November orders in a single table: every row is one product in one order. The table looks handy, but the customer's name, the product's name and its category are written again in every row. Before you start splitting, you want to see the size of the problem in numbers.
How many rows, how many distinct orders, how many distinct customer numbers, how many distinct customer names and how many distinct product codes are in the orders_raw table? The result should be one row; columns row_count, orders, customers, customer_names, products.
Look at the output and ask yourself: why is the number of customer numbers different from the number of customer names? This difference will help you when you choose the key of the customer table.
This step's code cell is for members.Sign up free
There are only 12 products in 75 rows. If the owner wants to rename "Monstera" to "Monstera deliciosa", how many rows would have to be corrected? You want to see how many times each product's information is written in the table.
For every product show product_code
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
product_namerepeatsLook at the end of the list too: which product appears in a single row only? If that order were canceled and its row deleted, the product's name and category would disappear from the table completely. This is the deletion anomaly from lesson 32.
This step's code cell is for members.Sign up free
You want to build a product table. But which columns really belong to the product? If a column depends on the product, there should be a single value in that column across all rows of the same product code. This is a claim; you can check it in the data.
For every product code count how many distinct values appear in the product_name, category and unit_price columns: name_count, category_count, price_count. List only the products where at least one of these three counts is greater than 1; order by product code.
Interpret the rows: which column does not depend on the product? If you like, in the same cell you can sort the rows of those products by date and see when the price changed (not checked). The price paid in an order is the product's price on that day; it belongs to the line of the sale, not to the product itself.
This step's code cell is for members.Sign up free
You have seen the dependencies; now start splitting. There are two entities that exist on their own: the customer and the product. Each goes into its own table, with every fact written once. The tool is CREATE TABLE ... AS SELECT DISTINCT from lesson 32.
The ready line at the top of the cell drops all four tables, so when you rerun the cell the tables are built again from scratch. The customer table is ready. You build the product table: named product, columns product_code, product_name, category. Remember the finding from the previous step: which column must not go into this table?
The check query shows the columns and row counts of the two tables. In an entity table the row count must equal the number of distinct keys: how many customers and how many products did you count in step 1?
This step's code cell is for members.Sign up free
A friend who helps at the shop wrote the order table for you. The cell runs without an error, but look at the check query: there are 30 orders and the table has 75 rows. In an order table every order should be one row.
Ask for each column: does this fact depend on the order? The order's date and the customer who placed it depend on the order. But the table has two columns that do not belong to the order: one multiplies the rows, the other does not break the row count but repeats the information of another table (a transitive dependency). Find both and fix the query. In the end the columns should be order_no, order_date, customer_no.
This step's code cell is for members.Sign up free
What remains is the information that connects an order to a product: which product, how many units, at what price in which order. This link table will be order_item. Quantity and unit price depend on the order and the product together; in step 3 you saw that the price belongs to the sale, not to the product.
Build the order_item table: columns order_no, product_code, quantity, unit_price. Do you need DISTINCT here? Every row is already a separate sale; there is nothing to deduplicate. The check query shows the table's revenue next to the raw table's: if the two are not the same, you lost money while splitting.
This step's code cell is for members.Sign up free
CREATE TABLE ... AS copies the columns and rows, not the rules (lesson 28). None of the four tables has a key: tomorrow someone could add a second row with the same product code, or write an order for a customer that does not exist. You will add the rules to tables that already hold data with ALTER TABLE ... ADD CONSTRAINT (lesson 33).
The customer's primary key and the order's primary key and link to the customer are ready. You add these:
product over product_code (product_pk)order_item made of the order number and product code (order_item_pk)order_item to customer_order (order_item_order_fk) and to product (order_item_product_fk)Use the constraint names exactly; the ready lines at the top of the cell remove the old constraints with these names, so the cell can run again. Think about one more thing: if you had put the price in the product table too, could product_pk have been added?
This step's code cell is for members.Sign up free
You split the raw table into four. The shop owner has one question: "Did anything get lost?" The answer comes from a query, not a guess. Join the four tables on their keys: every order item should find its own order and product, and every order should find its own customer. If the join loses rows, something does not match; if it multiplies rows, there is an unexpected repeat on one side (lesson 18).
From the joined result produce one row: the number of rows (row_count), the number of distinct cities (cities), the number of distinct categories (categories), the total revenue (revenue = sum of quantity × unit price) and, for comparison, the raw table's row count (raw_rows). Because the city lives in the customer table and the category in the product table, you really need all four tables.
When the result holds, step back: a join that gives as many rows as the raw table shows no information was lost. Does it prove by itself that the tables are designed correctly? Why is a dependency check like the one in step 3 still needed?
This step's code cell is for members.Sign up free