This is a premium alert message you can set from Layout! Get Now!

Inserting Data into Tables (INSERT)

Inserting Data into Tables ( INSERT )
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
Tags

Post a Comment

0 Comments
Post a Comment
To Top