Welcome back! In the last chapter, we learned how to
create databases and tables using SQL. Now that you have a table ready (like our
students table), it's time to
insert actual data into it — this is where the magic begins!Let’s dive into the SQL
INSERT statement, which allows us to add data rows to our table.
๐งพ What is the INSERT Statement?
The
INSERT statement in SQL is used to
add new records (rows) into a table.
๐น Syntax:
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
๐น Example Using Our students Table:
Let's say we have this table from the last chapter:
CREATE TABLE students
( id INT PRIMARY KEY,
name VARCHAR ( 50 ),
age INT ,
course VARCHAR ( 30 ),
marks DECIMAL ( 5 , 2 ) );
Now we’ll insert a student:
INSERT INTO students
(id, name, age, course, marks)
VALUES ( 1 , 'Rohit Sharma' , 21 , 'SQL' , 87.50 );
✅ This command will add a new row into the
students table.
๐ง๐ Inserting Multiple Records
You can also insert
multiple rows at once like this:
INSERT INTO students (id, name, age, course, marks)
VALUES ( 2 , 'Priya Verma' , 22 , 'SQL' , 91.00 ),
( 3 , 'Amit Raj' , 20 , 'SQL' , 76.75 ),
( 4 , 'Sneha Mehta' , 23 , 'SQL' , 89.20 );
⚡ Pro Tip: This is faster than inserting one row at a time!
๐ Insert Without Column Names (Not Recommended)
You can insert data
without mentioning column names , but the order
must match the table structure exactly:
INSERT INTO students
VALUES ( 5 , 'Karan Yadav' , 20 , 'SQL' , 93.10 );
⚠️
Not recommended — Always include column names for clarity and safety.
๐ Real-World Use Case Example
Let’s continue with our blog theme:
Table: students
| id |
name |
age |
course |
marks |
| 1 |
Rohit Sharma |
21 |
SQL |
87.50 |
| 2 |
Priya Verma |
22 |
SQL |
91.00 |
| 3 |
Amit Raj |
20 |
SQL |
76.75 |
These entries can now be used in future queries — for
SELECT ,
UPDATE ,
DELETE , etc.
๐จ Common Errors and How to Fix Them
| Error Message |
Reason |
Solution |
Duplicate entry |
Inserting a row with an existing PRIMARY KEY |
Make sure the id is unique |
Column count doesn’t match |
Mismatch between number of columns and values |
Double-check the number and order |
Data type mismatch |
Giving wrong type of data (e.g., string in numeric column) |
Use correct data types (quotes for strings) |
๐ก Best Practices
- Always mention column names while inserting.
- Use consistent formatting (especially for
VARCHAR values in quotes).
- Validate data before inserting — especially with user inputs.
๐ง Practice Task
Try inserting the following student into your database:
Student:
- ID:
5
- Name:
Anjali Singh
- Age:
21
- Course:
SQL
- Marks:
88.40
✍️ SQL Query:
INSERT INTO students (id, name, age, course, marks)
VALUES ( 5 , 'Anjali Singh' , 21 , 'SQL' , 88.40 );
✅ Summary
| Task |
SQL Statement Example |
| Insert one record |
INSERT INTO students (...) VALUES (...); |
| Insert multiple records |
INSERT INTO students (...) VALUES (...), (...); |
| Avoid common mistakes |
Always match column types and order |