Data Manipulation Language (DML) commands are used to add, modify, and remove data stored in database tables. These commands directly affect the data inside tables but do not change the table structure. DML is the most frequently used part of SQL in real-world applications.
The INSERT statement is used to add new rows into a table. You can insert a single row, multiple rows, or data retrieved from another table.
INSERT INTO employees
(employee_id, first_name, last_name, email, hire_date, salary, department)
VALUES
(101, 'John', 'Smith', 'john.smith@company.com', '2024-01-15', 55000.00, 'IT');
employees tableINSERT INTO employees
(employee_id, first_name, last_name, email, hire_date, salary, department)
VALUES
(102, 'Jane', 'Doe', 'jane.doe@company.com', '2024-01-20', 60000.00, 'Sales'),
(103, 'Bob', 'Johnson', 'bob.j@company.com', '2024-02-01', 52000.00, 'Marketing'),
(104, 'Alice', 'Williams', 'alice.w@company.com', '2024-02-15', 58000.00, 'IT');
INSERT INTO high_performers (employee_id, first_name, last_name, salary)
SELECT employee_id, first_name, last_name, salary
FROM employees
WHERE salary > 70000;
The UPDATE statement is used to change existing records in a table.
UPDATE employees
SET salary = 60000
WHERE employee_id = 101;
UPDATE employees
SET
salary = salary * 1.10,
department = 'Senior IT'
WHERE employee_id = 101;
UPDATE employees
SET salary = salary * 1.05
WHERE department = 'Sales';
UPDATE employees
SET salary = salary * 1.10
WHERE department IN (
SELECT department_name
FROM departments
WHERE location = 'New York'
);
BEGIN, ROLLBACK)The DELETE statement is used to remove rows from a table.
DELETE FROM employees
WHERE employee_id = 101;
DELETE FROM employees
WHERE hire_date < '2015-01-01';
DELETE FROM employees
WHERE department IN (
SELECT department_name
FROM departments
WHERE location = 'Closed Office'
);
-- Deletes rows one by one
DELETE FROM employees;
-- Removes all rows instantly
TRUNCATE TABLE employees;
| Feature | DELETE | TRUNCATE |
|---|---|---|
| WHERE clause | Supported | Not supported |
| Rollback | Possible | Not possible |
| Speed | Slower | Faster |
| Logging | Row-level | Minimal |
| Resets auto-increment | No | Yes |
BEGIN;
UPDATE employees
SET salary = salary * 1.10
WHERE department = 'IT';
ROLLBACK; -- Undo changes
-- OR
COMMIT; -- Save changes