SQL Topics
INSERT Data
title: INSERT Data
Creating a database and tables is only the beginning of database management. A table without data has little practical value. To make a database useful, records must be added so that information can be stored, processed, and retrieved.
SQL provides the INSERT INTO statement for this purpose.
The INSERT statement allows developers to add new records into database tables. Whether storing customer information, employee details, product data, student records, or financial transactions, INSERT is one of the most frequently used SQL commands.
Every application that collects information from users eventually performs INSERT operations. Registration forms, online orders, login systems, inventory management applications, and banking systems all rely on INSERT statements to store data.
In this lesson, you will learn how INSERT works, how to insert single and multiple records, use default values, handle NULL values, and follow professional best practices.
What is INSERT INTO?
The INSERT INTO statement is used to add new rows into a database table.
Example:
INSERT INTO Students
VALUES (1, 'Rahul', 20);This statement adds a new record to the Students table.
Result:
| StudentID | Name | Age |
|---|---|---|
| 1 | Rahul | 20 |
The data becomes part of the database and can be retrieved later using SQL queries.
Why is INSERT Important?
Without INSERT, a database cannot store information.
Consider a school management system.
New students join every semester.
Each student must be added to the database.
Example:
Student Registration Form
↓
INSERT Statement
↓
Database Record CreatedSimilarly:
- E-commerce stores add products.
- Banks add customer accounts.
- Hospitals add patient records.
- Companies add employee details.
INSERT is the foundation of data entry operations.
Basic INSERT Syntax
The general syntax is:
INSERT INTO TableName
VALUES (Value1, Value2, Value3);Example:
INSERT INTO Students
VALUES (1, 'Rahul', 20);Here:
- Students is the table name.
- Values correspond to table columns.
Creating a Sample Table
Example:
CREATE TABLE Students (
StudentID INT,
Name VARCHAR(100),
Age INT
);Current table:
| StudentID | Name | Age |
|---|---|---|
| Empty | Empty | Empty |
The table exists but contains no records.
Inserting a Single Record
Example:
INSERT INTO Students
VALUES (1, 'Rahul', 20);Result:
| StudentID | Name | Age |
|---|---|---|
| 1 | Rahul | 20 |
A new row is added.
Inserting Multiple Records
SQL allows multiple rows to be inserted in a single statement.
Example:
INSERT INTO Students
VALUES
(1, 'Rahul', 20),
(2, 'Priya', 21),
(3, 'Amit', 19);Result:
| StudentID | Name | Age |
|---|---|---|
| 1 | Rahul | 20 |
| 2 | Priya | 21 |
| 3 | Amit | 19 |
This approach is faster than executing multiple INSERT statements.
Specifying Column Names
Although SQL allows inserting values without column names, specifying columns is considered a best practice.
Example:
INSERT INTO Students
(StudentID, Name, Age)
VALUES
(1, 'Rahul', 20);Benefits:
- Improved readability
- Reduced errors
- Better maintainability
Professional developers almost always specify column names.
Inserting Partial Data
You do not need to provide values for every column.
Example table:
CREATE TABLE Students (
StudentID INT,
Name VARCHAR(100),
Email VARCHAR(255)
);Insert:
INSERT INTO Students
(StudentID, Name)
VALUES
(1, 'Rahul');Result:
| StudentID | Name | |
|---|---|---|
| 1 | Rahul | NULL |
Email receives a NULL value because no data was supplied.
Inserting NULL Values
NULL represents missing or unknown information.
Example:
INSERT INTO Students
VALUES
(1, 'Rahul', NULL);Result:
| StudentID | Name | Age |
|---|---|---|
| 1 | Rahul | NULL |
This indicates that the age is unknown.
Using Default Values
Tables can define default values.
Example:
CREATE TABLE Users (
UserID INT,
Name VARCHAR(100),
Status VARCHAR(20) DEFAULT 'Active'
);Insert:
INSERT INTO Users
(UserID, Name)
VALUES
(1, 'Rahul');Result:
| UserID | Name | Status |
|---|---|---|
| 1 | Rahul | Active |
The default value is automatically assigned.
Inserting Current Date
Many database systems provide functions for current dates.
Example:
INSERT INTO Orders
(OrderID, OrderDate)
VALUES
(1, CURRENT_DATE);Result:
Today's Date StoredUseful for:
- Orders
- Registrations
- Transactions
- Logs
Inserting Data from Another Table
INSERT can copy data from another table.
Example:
INSERT INTO GraduateStudents
SELECT *
FROM Students
WHERE Age >= 21;This copies matching records into another table.
Useful for:
- Archiving
- Migration
- Reporting
Real-World Example
Imagine an e-commerce application.
Products table:
CREATE TABLE Products (
ProductID INT,
ProductName VARCHAR(100),
Price DECIMAL(10,2)
);Add a product:
INSERT INTO Products
VALUES
(101, 'Laptop', 59999.99);Result:
| ProductID | ProductName | Price |
|---|---|---|
| 101 | Laptop | 59999.99 |
The product is now available in the system.
Common Errors
Mismatch Between Columns and Values
Wrong:
INSERT INTO Students
VALUES (1, 'Rahul');If the table requires three values:
Column Count Doesn't Match Value CountError occurs.
Invalid Data Types
Wrong:
INSERT INTO Students
VALUES ('ABC', 'Rahul', 20);If StudentID expects INT:
Invalid Data TypeError occurs.
Duplicate Primary Key
Example:
INSERT INTO Students
VALUES (1, 'Rahul', 20);Again:
INSERT INTO Students
VALUES (1, 'Priya', 21);Result:
Duplicate Primary Key ErrorNOT NULL Constraint Violation
Example:
INSERT INTO Students
VALUES (1, NULL, 20);If Name is NOT NULL:
Constraint ViolationError occurs.
Best Practices
Always Specify Column Names
Preferred:
INSERT INTO Students
(StudentID, Name, Age)
VALUES
(1, 'Rahul', 20);Validate Data Before Insertion
Check:
- Data types
- Length limits
- Required fields
Use Meaningful Values
Avoid inserting random or meaningless test data into production databases.
Handle NULL Carefully
Ensure NULL values are appropriate for the business requirement.
Use Transactions for Critical Inserts
Large systems often wrap INSERT operations inside transactions.
This helps maintain data integrity.
Common Interview Questions
What is the purpose of INSERT INTO?
It adds new records into a database table.
Can multiple rows be inserted at once?
Yes.
INSERT INTO Students
VALUES
(1, 'Rahul', 20),
(2, 'Priya', 21);Why specify column names?
It improves readability and prevents errors when table structures change.
What happens if a NOT NULL column receives NULL?
The database generates an error.
Summary
The INSERT INTO statement is one of the most fundamental SQL commands. It allows applications to store information inside database tables and forms the foundation of data entry operations.
In this lesson, you learned:
- What INSERT INTO is
- Why it is important
- Basic syntax
- Single-row insertion
- Multiple-row insertion
- Column-specific insertion
- NULL values
- Default values
- Inserting from another table
- Best practices
- Common mistakes
Understanding INSERT operations is essential because every database application depends on storing information efficiently and accurately.
Next Step
Continue to the next lesson:
SELECT Data →
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for INSERT Data.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this SQL topic.
Search Terms
sql, sql complete guide, sql tutorial, sql notes, complete, guide, crud, insert
Related SQL Topics