Free Study Material

Manipulating Data (DML)

Introduction

CREATE TABLE builds an empty structure — INSERT INTO is how you actually put data into it, one row at a time.

Concept

-- All columns, in table order
INSERT INTO STUDENT VALUES (101, 'Ravi', 80);

-- Only some columns -- must name them explicitly
INSERT INTO STUDENT (AdmNo, Name) VALUES (102, 'Amit');

If you list values for every column, you can skip naming them, as long as you provide them in the table's exact column order. If you only want to fill in some columns, you must explicitly name which ones you're providing.

Explanation

Whatever column you don't provide a value for gets NULL automatically (unless that column has a DEFAULT value defined) — it isn't zero, it isn't an empty string, it's specifically NULL, meaning "no value at all."

Note: INSERT INTO always adds exactly one new row. There's no version of INSERT that modifies an existing row — changing data that's already there is UPDATE's job.

Summary

INSERT INTO table VALUES (...) adds a new row — list every column's value in order, or name specific columns explicitly if skipping others. Any column left out becomes NULL, unless a DEFAULT is defined for it.

The INSERT Command

We built the table (House). Now we move the data (Furniture) in.
The INSERT INTO command adds new rows (tuples) to the table.

Method 1: Insert All Values (The Shortcut)

If you have data for every single column in the correct order, you don't need to list the column names.

INSERT INTO table_name VALUES (val1, val2, val3...);
Example:
INSERT INTO STUDENT VALUES (101, 'Ravi', 95);

Method 2: Insert Specific Columns (The Safe Way)

If you only have data for some columns (e.g., you don't know the Marks yet), you must specify the column names. The missing columns will get NULL.

INSERT INTO table_name (col1, col2) VALUES (val1, val2);
Example:
INSERT INTO STUDENT (AdmNo, Name) VALUES (102, 'Amit');
(Marks will automatically become NULL)

Challenge: Add Students

The STUDENT table is ready with columns: AdmNo (INT), Name (TEXT), Marks (INT).

Your Task:
  1. Insert Row 1: 101, 'Ravi', 95 (All values).
  2. Insert Row 2: 102, 'Amit' (Marks are unknown/NULL).

↓ Write the 2 queries below ↓

/* ==================================================
   SYSTEM SETUP (Table Created Automatically)
   Columns: AdmNo, Name, Marks
   ================================================== */
DROP TABLE IF EXISTS STUDENT;
CREATE TABLE STUDENT (AdmNo INT, Name VARCHAR(20), Marks INT);

/* ==================================================
   YOUR TASK STARTS HERE
   ================================================== */

-- 1. Insert Ravi (All 3 values)
-- INSERT INTO ...


-- 2. Insert Amit (Only AdmNo and Name)
-- INSERT INTO ...


/* ==================================================
   VERIFICATION (See your data)
   ================================================== */
SELECT * FROM STUDENT;

Introduction

UPDATE changes the value(s) of existing rows — it's the command for correcting or adjusting data that's already in a table, without touching the table's structure.

Concept

UPDATE STUDENT SET Marks = 95 WHERE AdmNo = 101;

-- Updating multiple columns at once -- separate with commas
UPDATE STUDENT SET Marks = Marks + 5, City = 'Delhi' WHERE AdmNo = 101;

SET specifies which column(s) to change and their new value; WHERE specifies exactly which row(s) that change applies to.

Explanation

The WHERE clause isn't optional in practice — it's the only thing standing between "update one specific row" and "update every single row in the table." Leaving it out doesn't cause an error; SQL will happily and silently apply the change to every row, which is one of the most common, costly real-world mistakes with this command.

Warning: UPDATE STUDENT SET Marks = 0; with no WHERE clause sets every single student's marks to 0, not just one. Always double-check the WHERE clause before running an UPDATE.

Summary

UPDATE table SET column = value WHERE condition changes existing data — multiple columns can be updated at once, separated by commas. Omitting WHERE applies the change to every row in the table, not just one.

The UPDATE Command

The INSERT command adds new rows. But what if you need to change data that is already there?
We use the UPDATE command.

The Safety Lock: WHERE Clause

The most important part of an Update is the Condition. Without it, you destroy data.

UPDATE table_name
SET column_name = new_value
WHERE condition;
Correct Usage

UPDATE Student SET Marks=90 WHERE AdmNo=101;

(Only Student 101 gets updated).

The Disaster

UPDATE Student SET Marks=90;

(EVERY student in the school now has 90 marks!)

Challenge: Fix the Marks

Look at the table below. The student 'Ravi' (AdmNo 101) has Marks: 80.
This is an error. His actual marks are 95.

Your Task:

Write a query to change Ravi's marks to 95.
(Hint: Use WHERE AdmNo = 101 to be safe!)

↓ Write your UPDATE query below ↓

/* ==================================================
   SYSTEM SETUP (Table with Data)
   101 | Ravi | 80
   102 | Amit | 75
   ================================================== */
DROP TABLE IF EXISTS STUDENT;
CREATE TABLE STUDENT (AdmNo INT, Name VARCHAR(20), Marks INT);
INSERT INTO STUDENT VALUES (101, 'Ravi', 80);
INSERT INTO STUDENT VALUES (102, 'Amit', 75);

/* ==================================================
   YOUR TASK STARTS HERE
   Change Ravi's Marks (80) to 95.
   ================================================== */

-- Write your UPDATE command here:
-- UPDATE STUDENT SET ...


/* ==================================================
   VERIFICATION (Did it change?)
   ================================================== */
SELECT * FROM STUDENT;

Calculation Updates

You can also perform math inside an update.

Scenario: Bonus Marks

The Principal decides to give 5 grace marks to ALL students.

UPDATE STUDENT SET Marks = Marks + 5;

(Notice: No WHERE clause = Applies to Everyone).

Introduction

DELETE removes rows of data from a table — but, unlike DROP TABLE, it leaves the table's structure completely intact and ready to hold new data.

Concept

-- Delete one specific row
DELETE FROM STUDENT WHERE AdmNo = 102;

-- Delete every row, but keep the table structure
DELETE FROM STUDENT;

Explanation

This is the cleanest way to separate the three data-removal commands you'll meet across this unit: DELETE removes rows (data), optionally filtered by WHERE; DROP removes the entire table (structure and data both); TRUNCATE (a related command) also removes all rows but does so faster, without the ability to filter by WHERE. For CBSE purposes, the key contrast is DELETE (data only, structure survives) vs. DROP (both structure and data gone).

Note: DELETE FROM STUDENT; with no WHERE clause removes every row, but STUDENT still exists afterward as an empty table — you can immediately INSERT into it again. DROP TABLE STUDENT; leaves nothing to insert into at all.

Summary

DELETE FROM table WHERE condition removes matching rows; without WHERE, it removes all rows — but the table itself, its columns and constraints, remain. That's the core distinction from DROP TABLE, which removes the structure too.

The DELETE Command

When a student leaves the school, we don't destroy the whole table. We just remove their specific row.
The DELETE FROM command is used for this.

Case 1: Safe Deletion (Using WHERE)

Always use a condition (like ID) to pinpoint the row you want to remove.

DELETE FROM STUDENT WHERE AdmNo = 101;

BEFORE

AdmNo Name
101 Ravi
102 Amit

AFTER

AdmNo Name
102 Amit
(Row 101 Removed)

Case 2: The Disaster (Omitting WHERE)

If you forget the WHERE clause, SQL assumes you want to delete ALL rows.

DELETE FROM STUDENT;

BEFORE

AdmNo Name
101 Ravi
102 Amit

AFTER


Empty Set (0 rows)
(Table structure still exists!)

Challenge: Remove a Record

Student 'Amit' (AdmNo 102) has transferred to another school.
Remove his record from the database.

Warning: Do not forget the WHERE clause, or you will delete Ravi too!

↓ Write your DELETE query below ↓

/* ==================================================
   SYSTEM SETUP (Current Data)
   101 | Ravi
   102 | Amit
   ================================================== */
DROP TABLE IF EXISTS STUDENT;
CREATE TABLE STUDENT (AdmNo INT, Name VARCHAR(20));
INSERT INTO STUDENT VALUES (101, 'Ravi');
INSERT INTO STUDENT VALUES (102, 'Amit');

/* ==================================================
   YOUR TASK STARTS HERE
   Delete Amit (AdmNo 102)
   ================================================== */

-- Write your DELETE command here:
-- DELETE FROM STUDENT ...


/* ==================================================
   VERIFICATION (Is Amit gone?)
   ================================================== */
SELECT * FROM STUDENT;

Want the full course?

Unlock all chapters, video lectures, and SQL practice exercises.

Get Full Access