Code / SQL with Data / Unit 5: Joins, changes and the final project

Changing data: INSERT, UPDATE, DELETE डेटा बदलना: INSERT, UPDATE, DELETE

Lesson 12 of 13 · about 14 minutes · Add a product, change a price, remove a sale.

⚡ 0 XP

Step 1 of 2

Adding a row with INSERT

TeamPPtsMI1418CSK1416RCB1414

So far we only read data. INSERT adds a new row:

INSERT INTO products (id, name, category, price) VALUES (16, 'Poha 500g', 'Grocery', 35);

List the columns, then the values in the same order. Text in single quotes, numbers without.

Don’t worry: every Run starts from a fresh copy of the sample database, so you can experiment freely. Add a SELECT after your INSERT to see the new row.

Example
INSERT INTO products
  (id, name, category, price)
VALUES (16, 'Poha 500g', 'Grocery', 35);
SELECT * FROM products;

🎯 Challenge: A new item on the shelf +15 XP

The shop starts selling a new item. Insert a row into products with id 16, name Jaggery 1kg, category Grocery and price 70.

Try twice to unlock the solution (0/2)

query.sqlCtrl ⏎
Tables (practice database)
Results

Press Run to see your query’s rows here.

Checklist

Press Run to check your code.

  • products has the new row
  • Uses INSERT INTO
Lesson notes (text version)

1. Adding a row with INSERT

So far we only read data. INSERT adds a new row:

INSERT INTO products (id, name, category, price) VALUES (16, 'Poha 500g', 'Grocery', 35);

List the columns, then the values in the same order. Text in single quotes, numbers without.

Don’t worry: every Run starts from a fresh copy of the sample database, so you can experiment freely. Add a SELECT after your INSERT to see the new row.

                INSERT INTO products
  (id, name, category, price)
VALUES (16, 'Poha 500g', 'Grocery', 35);
SELECT * FROM products;
              

Challenge: A new item on the shelf

The shop starts selling a new item. Insert a row into products with id 16, name Jaggery 1kg, category Grocery and price 70.

2. UPDATE and DELETE — always with WHERE

UPDATE changes values in existing rows, DELETE removes rows:

  • UPDATE products SET price = 50 WHERE id = 4;
  • DELETE FROM sales WHERE id = 7;

Golden rule: always write the WHERE. UPDATE products SET price = 50; without WHERE changes every product’s price, and DELETE FROM sales; empties the whole table! In a real shop’s database there is no undo button. Run a SELECT with the same WHERE first to check which rows you will touch.

                UPDATE products
SET price = 50
WHERE id = 4;
SELECT * FROM products
WHERE id = 4;
              

Challenge: Milk price up, one sale returned

Two jobs: (1) milk now costs more — set price to 70 for the product with id 9. (2) The customer of sale id 22 returned the tea — delete that row from sales. Change nothing else.

Search by task, format or tool name.