SQL Data Modification: INSERT, UPDATE, DELETE, TRUNCATE Statements with Comprehensive Examples

Table of Contents

1. Modifying Data Overview

Q: What are data modification operations in SQL?

Data modification operations in SQL, part of Data Manipulation Language (DML), alter data in tables:

These operations contrast with Data Definition Language (DDL) commands (e.g., CREATE, ALTER), which modify table structures.

Q: Why are data modification operations important?

2. INSERT INTO

Q: What is the INSERT INTO statement?

The INSERT INTO statement adds new rows to a table, specifying column values or copying data from another query.

Syntax:

-- Insert specific values
INSERT INTO table_name (column1, column2) VALUES (value1, value2);
-- Insert multiple rows
INSERT INTO table_name (column1, column2) VALUES (value1, value2), (value3, value4);
-- Insert from a query
INSERT INTO table_name (column1, column2) SELECT column1, column2 FROM another_table WHERE condition;

Columns can be omitted if all are populated in order, but specifying columns is safer.

Q: Can you give an example of INSERT INTO?

-- Create sample table
CREATE TABLE Employees ( EmployeeID INT PRIMARY KEY, FirstName VARCHAR(50), LastName VARCHAR(50), DepartmentID INT, Salary DECIMAL(10, 2)
); -- Insert single row
INSERT INTO Employees (EmployeeID, FirstName, LastName, DepartmentID, Salary)
VALUES (1, 'John', 'Doe', 1, 60000.00); -- Insert multiple rows
INSERT INTO Employees (EmployeeID, FirstName, LastName, DepartmentID, Salary)
VALUES (2, 'Jane', 'Smith', 2, 55000.00), (3, 'kristal', 'Johnson', 1, 65000.00); -- Insert from another table
CREATE TABLE NewHires ( EmployeeID INT PRIMARY KEY, FirstName VARCHAR(50), LastName VARCHAR(50), DepartmentID INT, Salary DECIMAL(10, 2)
); INSERT INTO NewHires (EmployeeID, FirstName, LastName, DepartmentID, Salary)
VALUES (4, 'Ram', 'Williams', 2, 55000.00); INSERT INTO Employees (EmployeeID, FirstName, LastName, DepartmentID, Salary)
SELECT EmployeeID, FirstName, LastName, DepartmentID, Salary
FROM NewHires
WHERE Salary > 50000.00; -- Verify inserted data
SELECT * FROM Employees;

Output (SELECT * FROM Employees):

EmployeeID | FirstName | LastName | DepartmentID | Salary
-----------|-----------|-----------|--------------|--------
1 | John | Doe | 1 | 60000.00
2 | Jane | Smith | 2 | 55000.00
3 | kristal | Johnson | 1 | 65000.00
4 | Ram | Williams | 2 | 55000.00

Note: The INSERT ... SELECT copies Ram from NewHires to Employees.

3. UPDATE

Q: What is the UPDATE statement?

The UPDATE statement modifies existing rows in a table, setting new values for specified columns, often with a WHERE clause to target specific rows.

Syntax:

UPDATE table_name SET column1 = value1, column2 = value2 WHERE condition;

Without WHERE, all rows are updated, so use cautiously.

Q: Can you give an example of UPDATE?

-- Update single column for specific employees
UPDATE Employees
SET Salary = 62000.00
WHERE DepartmentID = 2; -- Update multiple columns with a condition
UPDATE Employees
SET Salary = Salary * 1.1, -- 10% raise LastName = UPPER(LastName)
WHERE FirstName LIKE 'J%'; -- Verify updated data
SELECT * FROM Employees;

Output (SELECT * FROM Employees):

EmployeeID | FirstName | LastName | DepartmentID | Salary
-----------|-----------|-----------|--------------|--------
1 | John | DOE | 1 | 66000.00
2 | Jane | SMITH | 2 | 62000.00
3 | kristal | Johnson | 1 | 65000.00
4 | Ram | Williams | 2 | 62000.00

Note:

4. DELETE

Q: What is the DELETE statement?

The DELETE statement removes rows from a table based on a WHERE condition. Without WHERE, all rows are deleted.

Syntax:

DELETE FROM table_name WHERE condition;

Respects foreign key constraints (may require ON DELETE CASCADE or manual deletion).

Q: Can you give an example of DELETE?

-- Delete specific rows
DELETE FROM Employees
WHERE Salary < 60000.00; -- Delete with subquery
DELETE FROM Employees
WHERE DepartmentID IN ( SELECT DepartmentID FROM Departments WHERE DepartmentName = 'HR'
); -- Verify remaining data
SELECT * FROM Employees;

Output (SELECT * FROM Employees):

EmployeeID | FirstName | LastName | DepartmentID | Salary
-----------|-----------|----------|--------------|--------
1 | John | DOE | 1 | 66000.00
3 | kristal | Johnson | 1 | 65000.00

Note:

5. TRUNCATE

Q: What is the TRUNCATE statement?

The TRUNCATE statement removes all rows from a table without a WHERE clause, resetting the table to its initial state. It is a DDL command, not DML, and is faster than DELETE for clearing tables.

Syntax:

TRUNCATE TABLE table_name;

Key Differences from DELETE:

Support: Available in MySQL, PostgreSQL, SQL Server; SQLite uses DELETE FROM table_name instead.

Q: Can you give an example of TRUNCATE?

-- Truncate the Employees table
TRUNCATE TABLE Employees; -- Verify table is empty
SELECT * FROM Employees; -- Re-insert a row to confirm table structure
INSERT INTO Employees (EmployeeID, FirstName, LastName, DepartmentID, Salary)
VALUES (1, 'New', 'Employee', 1, 70000.00); -- Verify new data
SELECT * FROM Employees;

Output (SELECT * FROM Employees after TRUNCATE):

EmployeeID | FirstName | LastName | DepartmentID | Salary
-----------|-----------|----------|--------------|--------
(empty result set)

Output (SELECT * FROM Employees after INSERT):

EmployeeID | FirstName | LastName | DepartmentID | Salary
-----------|-----------|----------|--------------|--------
1 | New | Employee | 1 | 70000.00

Note:TRUNCATE clears all rows and resets EmployeeID to start from 1.

6. Comprehensive Example Combining All Concepts

Q: Can you provide a comprehensive example using INSERT INTO, UPDATE, DELETE, TRUNCATE, and prior concepts (WHERE, subqueries)?

-- Create sample tables
CREATE TABLE Employees ( EmployeeID INT PRIMARY KEY, FirstName VARCHAR(50), LastName VARCHAR(50), DepartmentID INT, Salary DECIMAL(10, 2)
); CREATE TABLE Departments ( DepartmentID INT PRIMARY KEY, DepartmentName VARCHAR(50)
); -- Insert initial data
INSERT INTO Departments (DepartmentID, DepartmentName)
VALUES (1, 'IT'), (2, 'HR'); INSERT INTO Employees (EmployeeID, FirstName, LastName, DepartmentID, Salary)
VALUES (1, 'John', 'Doe', 1, 60000.00), (2, 'Jane', 'Smith', 2, 55000.00), (3, 'kristal', 'Johnson', 1, 65000.00), (4, 'Ram', 'Williams', 2, 55000.00); -- INSERT: Add new employee using subquery
INSERT INTO Employees (EmployeeID, FirstName, LastName, DepartmentID, Salary)
SELECT 5, 'hari', 'Brown', DepartmentID, 70000.00
FROM Departments
WHERE DepartmentName = 'IT'; -- UPDATE: Give 10% raise to employees in IT with above-average salary
UPDATE Employees
SET Salary = Salary * 1.1
WHERE DepartmentID = (SELECT DepartmentID FROM Departments WHERE DepartmentName = 'IT') AND Salary > (SELECT AVG(Salary) FROM Employees WHERE Salary IS NOT NULL); -- DELETE: Remove employees with salary below 60000
DELETE FROM Employees
WHERE Salary < 60000.00; -- Verify data before TRUNCATE
SELECT * FROM Employees; -- TRUNCATE: Clear the Employees table
TRUNCATE TABLE Employees; -- Verify table is empty
SELECT * FROM Employees; -- Re-insert a row to confirm structure
INSERT INTO Employees (EmployeeID, FirstName, LastName, DepartmentID, Salary)
VALUES (1, 'New', 'Employee', 1, 70000.00); -- Verify final data
SELECT * FROM Employees;

Output (SELECT * FROM Employees after DELETE):

EmployeeID | FirstName | LastName | DepartmentID | Salary
-----------|-----------|----------|--------------|--------
3 | kristal | Johnson | 1 | 71500.00
5 | hari | Brown | 1 | 77000.00

Output (SELECT * FROM Employees after TRUNCATE):

EmployeeID | FirstName | LastName | DepartmentID | Salary
-----------|-----------|----------|--------------|--------
(empty result set)

Output (SELECT * FROM Employees after final INSERT):

EmployeeID | FirstName | LastName | DepartmentID | Salary
-----------|-----------|----------|--------------|--------
1 | New | Employee | 1 | 70000.00

Description:

7. Common Mistakes and Best Practices

Q: What are common mistakes when modifying data in SQL?

INSERT INTO:

UPDATE:

DELETE:

TRUNCATE:

Q: What are best practices for modifying data in SQL?

INSERT INTO:

UPDATE:

DELETE:

TRUNCATE:

General:

Frequently asked questions

What are data modification operations in SQL?

Data modification operations in SQL (DML) include INSERT INTO (add rows), UPDATE (modify rows), DELETE (remove rows), and TRUNCATE (remove all rows efficiently).

What is the INSERT INTO statement in SQL?

INSERT INTO adds new rows to a table. It supports single/multiple row insertion and INSERT ... SELECT from another table/query.

How do you use UPDATE in SQL?

UPDATE modifies existing rows with SET clause and optional WHERE to target specific rows. Always use WHERE to avoid updating all rows.

What is the DELETE statement in SQL?

DELETE removes rows based on a WHERE condition. Without WHERE, it deletes all rows. Respects foreign key constraints.

What is TRUNCATE in SQL and how does it differ from DELETE?

TRUNCATE removes all rows quickly, resets auto-increment counters, and is a DDL command. Unlike DELETE, it cannot use WHERE and is minimally logged.

How to combine INSERT, UPDATE, DELETE, and TRUNCATE?

Use them together with subqueries, transactions, and conditional logic for complex data workflows, as shown in comprehensive examples.

What are common mistakes in SQL data modification?

Forgetting WHERE in UPDATE/DELETE, violating constraints in INSERT, using TRUNCATE when selective deletion is needed, and ignoring transactions.

What are best practices for SQL data modification?

Always specify columns in INSERT, test UPDATE/DELETE with SELECT first, use transactions, comment code, and backup before TRUNCATE.