SQL Practice Logo

SQLPractice Online

INSERT, UPDATE, DELETE Statements: Examples

Module: SQL Fundamentals

INSERT New Employee

basic

HR adds new employee to database

INSERT INTO employees (employee_id, first_name, last_name, email, salary, department, hire_date)

VALUES (101, 'John', 'Doe', 'john.doe@company.com', 75000, 'Engineering', '2024-01-15');

1 row inserted.

INSERT adds new row with specified values. Column list is optional but recommended for clarity and safety.

All

UPDATE Employee Salary

basic

Give 10% raise to Engineering department

UPDATE employees

SET salary = salary * 1.10

WHERE department = 'Engineering';

15 rows updated.

UPDATE modifies existing rows matching WHERE condition. Without WHERE, ALL rows would be updated!

All

DELETE Inactive Users

intermediate

Remove users inactive for over 2 years

DELETE FROM users

WHERE last_login_date < CURRENT_DATE - INTERVAL '2 years'

AND status = 'inactive';

47 rows deleted.

DELETE removes rows matching WHERE condition. Always test with SELECT first to verify which rows will be deleted.

All

Batch INSERT Multiple Rows

intermediate

Import multiple products from CSV file efficiently

INSERT INTO products (product_name, category, price, stock_quantity)

VALUES

('Laptop Pro', 'Electronics', 1299.99, 50),

('Wireless Mouse', 'Electronics', 29.99, 200),

('Office Chair', 'Furniture', 199.99, 25),

('Desk Lamp', 'Furniture', 49.99, 100);

-- Alternative: INSERT from SELECT

INSERT INTO products_backup

SELECT * FROM products

WHERE created_date >= '2024-01-01';

4 rows inserted.