INSERT INTO Statement in SQL | Complete Guide Part 1

INSERT INTO Statement in SQL

After creating a database and table, the next important task is adding data to the table. SQL provides the INSERT INTO statement for this purpose. The INSERT INTO command allows users to add new records into database tables. Without data insertion, tables remain empty and cannot be used for meaningful operations such as searching, updating, reporting, or analysis.

The INSERT INTO statement is one of the most frequently used SQL commands because every database application requires data storage. Whether it is a student management system, hospital database, banking application, e-commerce platform, or social media website, data is continuously inserted into tables using this command.

Understanding INSERT INTO is essential for students, developers, database administrators, and data analysts because it forms the foundation of database operations.


What is INSERT INTO?

INSERT INTO is a Data Manipulation Language (DML) command used to insert new records into an existing table. Each execution of the INSERT statement adds one or more rows to a table.

The command allows users to:


Why is INSERT INTO Important?

Databases are designed to store information. Without inserting data, database tables have no practical value.


Basic Syntax of INSERT INTO

The most common syntax is:

INSERT INTO TableName
(Column1, Column2, Column3)
VALUES
(Value1, Value2, Value3);

Here:


Creating a Sample Table

Before inserting data, a table must exist.

CREATE TABLE Student (
StudentID INT,
StudentName VARCHAR(100),
Course VARCHAR(50),
Age INT
);

The Student table is now ready to receive records.


Inserting a Single Record

The simplest use of INSERT INTO is adding one row at a time.

INSERT INTO Student
(StudentID, StudentName, Course, Age)
VALUES
(101, 'Rahul', 'BCA', 20);

This statement inserts one student record into the table.

Result

StudentID StudentName Course Age
101 Rahul BCA 20

Inserting Multiple Records

SQL allows multiple rows to be inserted using a single statement.

INSERT INTO Student
(StudentID, StudentName, Course, Age)
VALUES
(102, 'Priya', 'B.Tech', 21),
(103, 'Amit', 'MCA', 22),
(104, 'Neha', 'B.Sc', 19);

This command inserts three records simultaneously.

Advantages


Viewing Inserted Data

After inserting records, the SELECT statement can be used to verify the stored data.

SELECT * FROM Student;

Output:

StudentID StudentName Course Age
101 Rahul BCA 20
102 Priya B.Tech 21
103 Amit MCA 22
104 Neha B.Sc 19

Inserting Data Without Specifying Column Names

If values are supplied in the exact order of the table columns, column names may be omitted.

INSERT INTO Student
VALUES
(105, 'Karan', 'BCA', 20);

Although valid, this approach is generally not recommended because future table changes may cause errors.


Why Specifying Column Names is Recommended


Inserting Data into Selected Columns

It is not mandatory to provide values for every column.

INSERT INTO Student
(StudentID, StudentName)
VALUES
(106, 'Riya');

Only StudentID and StudentName receive values.

Other columns receive NULL values if allowed.


Understanding NULL Values

NULL represents missing or unknown information.

Example:

INSERT INTO Student
(StudentID, StudentName, Course)
VALUES
(107, 'Ankit', NULL);

The Course value is currently unavailable and therefore stored as NULL.


Inserting Numeric Values

Numeric values are inserted without quotation marks.

INSERT INTO Product
(ProductID, Price)
VALUES
(1, 4999);

Numbers can be integers, decimals, or floating-point values depending on the column data type.


Inserting Character Data

Text values must be enclosed within single quotes.

INSERT INTO Student
(StudentName)
VALUES
('Rohit');

SQL treats text enclosed within quotes as string data.


Inserting Date Values

Date values must follow the format supported by the database system.

INSERT INTO Student
(StudentID, StudentName, AdmissionDate)
VALUES
(108, 'Pooja', '2026-07-21');

The date is stored in the AdmissionDate column.


Inserting Boolean Values

Boolean columns store TRUE or FALSE values.

INSERT INTO Users
(UserID, IsActive)
VALUES
(1, TRUE);

Some database systems internally store TRUE as 1 and FALSE as 0.


Using Default Values During Insert

Columns can be configured with default values.

CREATE TABLE Employee (
EmployeeID INT,
EmployeeName VARCHAR(100),
Country VARCHAR(50) DEFAULT 'India'
);

Insert statement:

INSERT INTO Employee
(EmployeeID, EmployeeName)
VALUES
(1, 'Aman');

Country automatically receives the value 'India'.


Real-World Example: Student Management System

A college admission system stores student information whenever a new student registers.

INSERT INTO Student
(StudentID, StudentName, Course, Age)
VALUES
(201, 'Rahul Sharma', 'B.Tech', 19);

The database immediately records the student's details.


Real-World Example: Hospital Database

When a patient visits a hospital, patient information is inserted into the database.

INSERT INTO Patient
(PatientID, PatientName, Disease)
VALUES
(501, 'Anjali', 'Fever');

The patient record becomes available for future treatment and reporting.


Real-World Example: Online Shopping Website

Every time a customer places an order, order information is inserted into the Orders table.

INSERT INTO Orders
(OrderID, CustomerName, Amount)
VALUES
(1001, 'Vikas', 2500);

The order is stored permanently for processing and tracking.


Common Errors While Using INSERT INTO


Example of an Incorrect INSERT Statement

INSERT INTO Student
(StudentID, StudentName)
VALUES
('ABC', 123);

This statement may fail because StudentID expects a numeric value while StudentName expects text.


INSERT INTO SELECT Statement

Sometimes data already exists in one table and needs to be copied into another table. Instead of manually inserting records one by one, SQL provides the INSERT INTO SELECT statement.

This command copies data from one table and inserts it into another table.

Syntax

INSERT INTO NewTable (Column1, Column2)
SELECT Column1, Column2
FROM OldTable;

Example

INSERT INTO StudentBackup
(StudentID, StudentName)

SELECT StudentID, StudentName
FROM Student;

This query copies student records from the Student table to the StudentBackup table.


Using WHERE Clause with INSERT INTO SELECT

Specific records can also be copied using conditions.

INSERT INTO TopStudents
(StudentID, StudentName)

SELECT StudentID, StudentName
FROM Student
WHERE Marks > 80;

Only students scoring more than 80 marks are copied into the TopStudents table.


Creating Backup Tables Using INSERT INTO

Database administrators frequently create backup tables before performing major updates.

CREATE TABLE EmployeeBackup
AS
SELECT * FROM Employee
WHERE 1=0;

After creating the structure:

INSERT INTO EmployeeBackup
SELECT * FROM Employee;

The backup table now contains all employee records.


Auto Increment Columns and INSERT INTO

Many tables contain an AUTO_INCREMENT or IDENTITY column. These columns automatically generate unique values.

Example Table

CREATE TABLE Student (
StudentID INT AUTO_INCREMENT PRIMARY KEY,
StudentName VARCHAR(100)
);

Insert statement:

INSERT INTO Student
(StudentName)
VALUES
('Rahul');

The database automatically assigns StudentID.

StudentID StudentName
1 Rahul

Inserting Large Volumes of Data

Large organizations often insert thousands or millions of records daily. Efficient insertion techniques become extremely important.

Example

INSERT INTO Sales
(ProductID, Quantity)

VALUES
(101,10),
(102,5),
(103,12),
(104,20),
(105,8);

Using a single INSERT statement for multiple rows is generally faster than executing many separate INSERT commands.


Bulk Data Insertion

Bulk insertion techniques are commonly used when importing data from CSV files, spreadsheets, or external applications.

Benefits include:


INSERT INTO with Calculated Values

SQL allows insertion of values generated through calculations.

INSERT INTO EmployeeSalary
(EmployeeID, TotalSalary)

VALUES
(101, 25000 + 5000);

The calculated result is stored in the database.


INSERT INTO with Functions

Built-in SQL functions can also be used while inserting data.

Current Date Example

INSERT INTO Orders
(OrderID, OrderDate)

VALUES
(1001, CURRENT_DATE);

The current system date is automatically inserted.

Current Timestamp Example

INSERT INTO LoginHistory
(UserID, LoginTime)

VALUES
(101, CURRENT_TIMESTAMP);

The current date and time are stored automatically.


INSERT INTO with NULL Values

NULL represents missing or unknown information.

INSERT INTO Employee
(EmployeeID, EmployeeName, Phone)

VALUES
(1, 'Amit', NULL);

The phone number is currently unavailable and therefore stored as NULL.


How Constraints Affect INSERT Operations

Constraints validate data before insertion.

PRIMARY KEY Constraint

CREATE TABLE Student (
StudentID INT PRIMARY KEY,
StudentName VARCHAR(100)
);

Duplicate StudentID values cannot be inserted.

Example

INSERT INTO Student
VALUES (1,'Rahul');

INSERT INTO Student
VALUES (1,'Amit');

The second statement generates an error because the primary key already exists.


UNIQUE Constraint and INSERT

CREATE TABLE Users (
UserID INT,
Email VARCHAR(100) UNIQUE
);

Duplicate email addresses cannot be inserted.


NOT NULL Constraint and INSERT

CREATE TABLE Employee (
EmployeeID INT,
EmployeeName VARCHAR(100) NOT NULL
);

The EmployeeName column must always contain a value.


CHECK Constraint and INSERT

CREATE TABLE Student (
StudentID INT,
Age INT CHECK (Age >= 18)
);

The database rejects values that violate the condition.


Common INSERT INTO Errors

1. Duplicate Primary Key

INSERT INTO Student
VALUES (1,'Rahul');

If ID 1 already exists, insertion fails.

2. Data Type Mismatch

INSERT INTO Student
VALUES ('ABC','Rahul');

Numeric columns cannot store invalid text values.

3. Missing Required Values

INSERT INTO Employee
(EmployeeID)
VALUES (1);

If EmployeeName is NOT NULL, the insertion fails.


Performance Tips for INSERT INTO


Real-World Example: College Management System

Whenever a student takes admission, the system stores information in the Student table.

INSERT INTO Student
(StudentID, StudentName, Course)

VALUES
(501,'Rahul Sharma','B.Tech');

The student becomes part of the institutional database.


Real-World Example: Banking System

When a customer opens a new account, the banking application inserts customer information.

INSERT INTO Customer
(CustomerID, CustomerName)

VALUES
(1001,'Anjali Verma');

The customer account is immediately available for banking operations.


Real-World Example: Hospital Management System

New patient records are inserted during registration.

INSERT INTO Patient
(PatientID, PatientName)

VALUES
(3001,'Vikas Kumar');

Doctors and staff can now access the patient's information.


Real-World Example: E-Commerce Platform

When customers place orders, order details are inserted into the Orders table.

INSERT INTO Orders
(OrderID, Amount)

VALUES
(9001, 2500);

The order becomes available for shipping and tracking.


Advantages of INSERT INTO


Limitations of INSERT INTO


Best Practices for INSERT INTO


SQL INSERT INTO Interview Questions and Answers

1. What is the purpose of INSERT INTO in SQL?

INSERT INTO is used to add new records into a database table.

2. Which SQL category does INSERT belong to?

INSERT belongs to Data Manipulation Language (DML).

3. Can INSERT add multiple records?

Yes, multiple rows can be inserted using a single statement.

4. Is it mandatory to specify column names?

No, but it is highly recommended.

5. What happens if column names are omitted?

Values must be supplied in the exact order of table columns.

6. Can INSERT store NULL values?

Yes, if the column allows NULL values.

7. What is INSERT INTO SELECT?

It copies data from one table and inserts it into another table.

8. Can functions be used inside INSERT?

Yes, functions such as CURRENT_DATE and CURRENT_TIMESTAMP can be used.

9. What happens if a primary key value already exists?

The insertion operation fails.

10. How does AUTO_INCREMENT work?

The database automatically generates unique numeric values.

11. Can INSERT modify existing rows?

No, UPDATE is used to modify existing rows.

12. Which command displays inserted data?

SELECT * FROM TableName;

13. What is a bulk insert?

Bulk insert is the process of inserting large amounts of data efficiently.

14. Can constraints affect insertion?

Yes, constraints validate data before insertion.

15. What is the difference between INSERT and UPDATE?

INSERT creates new records, while UPDATE modifies existing records.


Complete Practical Example

CREATE TABLE Student (
StudentID INT PRIMARY KEY,
StudentName VARCHAR(100),
Course VARCHAR(50),
Age INT
);

INSERT INTO Student
(StudentID, StudentName, Course, Age)

VALUES
(101,'Rahul','BCA',20),
(102,'Priya','B.Tech',21),
(103,'Amit','MCA',22);

SELECT * FROM Student;

The above example creates a table, inserts records, and displays stored data.


Conclusion

The INSERT INTO statement is one of the most important SQL commands because it enables data storage within database tables. It supports single-row insertion, multi-row insertion, NULL values, default values, auto-increment columns, functions, and data transfer between tables using INSERT INTO SELECT.

Mastering INSERT INTO helps developers build efficient database applications, maintain data integrity, and handle real-world business operations effectively. A strong understanding of insertion techniques is essential for SQL interviews, academic studies, and professional database development.

← Previous: TRUNCATE TABLE Next: SELECT Statement →
Home Visit Our YouTube Channel