Complete Guide to DML Statements in SQL (with Tables and All Use Cases)

1. INSERT Statement – All Use Cases

a) Insert a Single Row (Full Columns)

Before: Students

StudentID Name Age
101 Ravi 18
					INSERT INTO Students (StudentID, Name, Age)
VALUES (102, 'Neha', 19);
				

After:

StudentID Name Age
101 Ravi 18
102 Neha 19
b) Insert a Row with Partial Columns
					INSERT INTO Students (Name)
VALUES ('Priya');
				

Result:

StudentID Name Age
103 Priya NULL
c) Insert Multiple Rows
					INSERT INTO Students (StudentID, Name, Age)
VALUES
(104, 'Anil', 20),
(105, 'Karan', 21);
				

New Rows Added:

StudentID Name Age
104 Anil 20
105 Karan 21
d) Insert from Another Table
					INSERT INTO Graduates (StudentID, Name)
SELECT StudentID, Name FROM Students WHERE Age > 20;
				

Copies students aged above 20 to Graduates table.

2. UPDATE Statement – All Use Cases

a) Update a Single Column for a Row
					UPDATE Students
SET Age = 22
WHERE StudentID = 105;
				

Before:

StudentID Name Age
105 Karan 21

After:

StudentID Name Age
105 Karan 22
b) Update Multiple Columns
					UPDATE Students
SET Name = 'Karan Sharma', Age = 23
WHERE StudentID = 105;
				
StudentID Name Age
105 Karan Sharma 23
c) Update Multiple Rows
					UPDATE Students
SET Age = Age + 1
WHERE Age < 21;
				

Increases age by 1 for all students below 21.

d) Update Using Subquery
					UPDATE Students
SET Age = (SELECT MAX(Age) FROM Students)
WHERE StudentID = 104;
				

Sets Anil’s age to the highest in the table.

3. DELETE Statement – All Use Cases

a) Delete Specific Row
					DELETE FROM Students
WHERE StudentID = 103;
				

Removes Priya.

b) Delete Multiple Rows
					DELETE FROM Students
WHERE Age < 20;
				

Removes all students under 20.

c) Delete All Rows
					DELETE FROM Students;
				

Note: Table remains, but all data is removed.

d) Delete Using Subquery
					DELETE FROM Students
WHERE StudentID IN (SELECT StudentID FROM Graduates);
				

Removes students already in Graduates .

4. MERGE Statement – Advanced Use Case

Used to insert or update rows based on matching conditions.

Source Table: TempStudents

StudentID Name Age
105 Karan 24
106 Alia 21
					MERGE INTO Students AS target
USING TempStudents AS source
ON target.StudentID = source.StudentID
WHEN MATCHED THEN
    UPDATE SET target.Age = source.Age
WHEN NOT MATCHED THEN
    INSERT (StudentID, Name, Age)
    VALUES (source.StudentID, source.Name, source.Age);
				

Result:

  • Updates Karan’s age
  • Inserts Alia as a new student

DML Summary Table

Statement Use Case Description
INSERT Single row Add one new record
INSERT Partial column Add row with default/NULL values
INSERT Multiple rows Bulk insert
INSERT From another table Copy data using SELECT
UPDATE Single row Change one field
UPDATE Multiple columns Change multiple fields together
UPDATE Multiple rows Update many rows with condition
UPDATE Subquery Dynamic updates from other data
DELETE Specific row Remove one record
DELETE Multiple rows Conditional delete
DELETE All rows Empty the table
DELETE Using subquery Delete based on external condition
MERGE Insert or update Sync data from source to target table