INSERT INTO Statement in SQL
INSERT INTO Statement in SQL
The INSERT INTO statement in SQL is used to add new records into a table. It is one of the most frequently used commands in SQL because every database application requires inserting new data.
In this beginner-friendly SQL tutorial, you will learn:
What INSERT INTO does
Syntax of INSERT INTO
Insert single row
Insert multiple rows
Insert data into selected columns
Insert using SELECT
Real-world examples
Best practices
The INSERT INTO statement adds new rows to a database table.
Two main ways to use INSERT:
Insert into all columns
Insert into specific columns
INSERT INTO table_name
VALUES (value1, value2, value3, ...);
INSERT INTO table_name (column1, column2, column3)
VALUES (value1, value2, value3);
| id | name | city | |
|---|---|---|---|
| 1 | Amit | amit@example.com | Delhi |
| 2 | Neha | neha@example.com | Mumbai |
INSERT INTO users (name, email, city)
VALUES ('Rahul', 'rahul@example.com', 'Bengaluru');
(Useful only if you know the correct order of columns.)
INSERT INTO users
VALUES (3, 'Sara', 'sara@example.com', 'Pune');
INSERT INTO users (name, email, city)
VALUES
('Karan', 'karan@example.com', 'Delhi'),
('Megha', 'megha@example.com', 'Hyderabad'),
('Ravi', 'ravi@example.com', 'Kolkata');
If a column has a default value:
INSERT INTO users (name, email)
VALUES ('Shubham', 'shubham@example.com');
The city column will use its default value.
You can insert data from one table into another.
INSERT INTO backup_users (name, email, city)
SELECT name, email, city
FROM users
WHERE city = 'Delhi';
This copies selected users into another table.
INSERT INTO users (name, email, city)
VALUES ('Unknown', NULL, 'Unknown');
INSERT INTO users (name, email, password)
VALUES ('John Doe', 'john@example.com', 'hashed_pass');
INSERT INTO products (product_name, price, stock)
VALUES ('Laptop', 55000, 10);
INSERT INTO orders (user_id, product, amount)
VALUES (1, 'Mobile', 20000);
INSERT INTO users VALUES ('Amit', 'amit@example.com');
Fix: Match number of table columns OR specify column names.
INSERT INTO employees (salary)
VALUES ('abc'); -- wrong
Fix: Insert valid numeric values.
β Always specify column names
β Use multiple-row inserts for better performance
β Avoid inserting hardcoded IDs when using AUTO_INCREMENT
β Validate data before inserting
β Use transactions for bulk inserts
In this SQL INSERT INTO tutorial, you learned:
How to insert data into tables
Insert single/multiple rows
Insert into specific columns
Insert using SELECT
Common errors and best practices
INSERT INTO is one of the most essential SQL commands for working with any database-driven application.