3 min read•august 6, 2024
SQL's Data Manipulation Language (DML) lets you modify database contents. You'll learn how to add new records with , change existing data using , and remove unwanted information through statements.
These operations form the backbone of data management in SQL. By mastering INSERT, UPDATE, and DELETE, you'll be able to keep your database current and accurate, reflecting real-world changes in your data.
INSERT INTO table_name (column1, column2, ...) [VALUES](https://www.fiveableKeyTerm:values) (value1, value2, ...)
INSERT INTO employees (employee_id, first_name, last_name) VALUES (1, 'John', 'Doe')
INSERT INTO products (product_id, name, price) VALUES (1, 'Phone', 500), (2, 'Laptop', 1000)
INSERT INTO new_customers (customer_id, name) SELECT customer_id, name FROM customers [WHERE](https://www.fiveableKeyTerm:where) country = 'USA'
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition
UPDATE employees SET salary = salary * 1.1 WHERE department = 'Sales'
UPDATE orders SET status = 'Shipped' WHERE order_id IN (SELECT order_id FROM order_details WHERE quantity > 10)
DELETE FROM table_name WHERE condition
DELETE FROM customers WHERE customer_id = 10
DELETE FROM orders WHERE customer_id IN (SELECT customer_id FROM customers WHERE country = 'USA')
MERGE INTO target_table USING source_table ON (condition) WHEN MATCHED THEN UPDATE SET ... WHEN NOT MATCHED THEN INSERT VALUES ...
MERGE INTO customers c USING new_customers nc ON (c.customer_id = nc.customer_id) WHEN MATCHED THEN UPDATE SET c.email = nc.email WHEN NOT MATCHED THEN INSERT VALUES (nc.customer_id, nc.name, nc.email)
UPDATE employees e SET e.salary = (SELECT AVG(salary) FROM employees WHERE department = e.department) WHERE e.employee_id IN (SELECT employee_id FROM performance_ratings WHERE rating = 'Excellent')