You have learned how Subqueries works in the previous article. Now itâs time to explore how to insert, modify and delete data in your tables. This is where SQL becomes a powerful tool for maintaining and managing your database.
Letâs walk through how to add, change, and remove data with clear examples using SQL Server.
Inserting Data into Tables
Adding new data to a table is done using the INSERT statement. Think of it as adding a new row to your spreadsheet.
Syntax:
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
Example: Adding a New Product
Imagine youâve just launched a new product, âFitness Band,â and want to add it to the Products table.
INSERT INTO products (product_id, product_name, category, price, stock)
VALUES (6, 'Fitness Band', 'Fitness', 3500, 20);
Result: A new row is added to the table:
| product_id | product_name | category | price | stock |
| 6 | Fitness Band | Fitness | 3500 | 20 |
This is how simple it is to add new data!
Updating Data in Tables
What if your inventory changes, and you need to update the quantity of products in stock? Thatâs where the UPDATE statement comes in.
Syntax:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Example: Restocking a Product
Suppose a new shipment of Yoga Mats arrives, and you need to increase their stock by 30 units.
UPDATE products
SET stock = stock + 30
WHERE product_name = 'Yoga Mat';
Result: The stock for âYoga Matâ is updated:
| product_name | stock |
| Yoga Mat | 80 |
Deleting Data from Tables
Sometimes, youâll need to remove outdated or incorrect data. For this, you use the DELETE statement.
WHERE Clause is not mandatory in DELETEstatement. But without the WHEREclause you will end up removing all the rows from the table.
Syntax:
DELETE FROM table_name
WHERE condition;
Example: Removing a Discontinued Product
Letâs say the âWireless Mouseâ is no longer sold. Hereâs how you can remove it from the Products table:
DELETE FROM products
WHERE product_name = 'Wireless Mouse';
Result: The âWireless Mouseâ is gone from the table.
Scenario: Managing Inventory
Letâs tie everything together with a realistic example:
- Adding a new product:
You receive a new product, âTreadmill,â and want to add it to the database:
INSERT INTO products (product_id, product_name, category, price, stock)
VALUES (7, 'Treadmill', 'Fitness', 45000, 5);
- Updating existing stock:
A shipment of âBluetooth Speakersâ arrives, so you increase their stock by 10 units:
UPDATE products
SET stock = stock + 10
WHERE product_name = 'Bluetooth Speaker';
- Removing a discontinued product:
The âSmartwatchâ is discontinued, so you delete it from the table:
DELETE FROM products
WHERE product_name = 'Smartwatch';
These commands together allow you to fully manage the data in your tables.
Summary
- Use
INSERT to add new rows to your tables.
- Use
UPDATE to modify existing data, but always double-check your WHERE clause to avoid accidental updates.
- Use
DELETE to remove rows, but be cautiousâdeleting without a WHERE clause removes all rows in the table.
Whatâs Next?
Now that youâve mastered modifying data, the next step is to learn about creating and altering tables. Stay tuned!