A database becomes useful only when it can store information. After creating a table, the next step is usually to add records to it. SQL provides the INSERT INTO statement for adding new rows to an existing table.
INSERT INTO is a Data Manipulation Language (DML) statement. It is commonly used in applications such as student management systems, banking software, hospital systems, e-commerce websites, employee databases, and many other systems where new information needs to be stored.
In this tutorial, you will learn how INSERT INTO works, how to insert one or multiple records, how to handle NULL and default values, how to copy records using INSERT INTO SELECT, common insertion errors, and important interview questions.
INSERT INTO is an SQL statement used to add one or more new rows to an existing database table.
For example, suppose a table named Student contains information about students. When a new student joins the college, an INSERT statement can be used to store that student's details.
INSERT INTO Student (StudentID, StudentName, Course) VALUES (101, 'Rahul', 'BCA');
The statement creates a new record in the Student table.
INSERT INTO is required whenever new information needs to be stored in a database.
In a real application, an INSERT operation may happen every time a user submits a registration form, places an order, creates an account, or enters new information.
The recommended form of the INSERT statement specifies the column names explicitly.
INSERT INTO TableName (Column1, Column2, Column3) VALUES (Value1, Value2, Value3);
For example:
INSERT INTO Student (StudentID, StudentName, Course) VALUES (101, 'Rahul', 'BCA');
| Part | Meaning |
|---|---|
| INSERT INTO | Specifies that a new record will be added. |
| Student | Name of the target table. |
| StudentID, StudentName, Course | Columns that will receive values. |
| VALUES | Introduces the values being inserted. |
| 101, Rahul, BCA | Actual data stored in the selected columns. |
Before inserting records, the destination table must already exist.
CREATE TABLE Student (
StudentID INT PRIMARY KEY,
StudentName VARCHAR(100),
Course VARCHAR(50),
Age INT
);
Now the Student table is ready to receive records.
The simplest form of INSERT INTO adds one record to a table.
INSERT INTO Student (StudentID, StudentName, Course, Age) VALUES (101, 'Rahul', 'BCA', 20);
After execution, the table contains the following record:
| StudentID | StudentName | Course | Age |
|---|---|---|---|
| 101 | Rahul | BCA | 20 |
When several records need to be added, multiple rows can be supplied in the same INSERT statement.
INSERT INTO Student (StudentID, StudentName, Course, Age) VALUES (102, 'Priya', 'B.Tech', 21), (103, 'Amit', 'MCA', 22), (104, 'Neha', 'B.Sc', 19);
Three records are inserted by one SQL statement.
The SELECT statement can be used to verify the data after insertion.
SELECT * FROM Student;
Example result:
| StudentID | StudentName | Course | Age |
|---|---|---|---|
| 101 | Rahul | BCA | 20 |
| 102 | Priya | B.Tech | 21 |
| 103 | Amit | MCA | 22 |
| 104 | Neha | B.Sc | 19 |
SQL also allows values to be inserted without explicitly writing column names.
INSERT INTO Student VALUES (105, 'Karan', 'BCA', 20);
In this form, the values must follow the table's column order.
Although this syntax is valid in many SQL systems, explicitly listing the columns is usually better because it makes the statement clearer and less dependent on the current column order.
Using column names makes an INSERT statement easier to understand and maintain.
Recommended approach:
INSERT INTO Student (StudentID, StudentName, Course) VALUES (106, 'Riya', 'BCA');
You do not always need to provide a value for every column. If omitted columns allow NULL or have a default value, they can be left out.
INSERT INTO Student (StudentID, StudentName) VALUES (107, 'Ankit');
Here, Course and Age are not supplied. Their final values depend on the table definition. If the columns allow NULL and have no default, NULL may be stored.
NULL represents the absence of a known value. It is different from zero, an empty string, or the word "NULL".
INSERT INTO Student (StudentID, StudentName, Course, Age) VALUES (108, 'Pooja', NULL, NULL);
This example indicates that the course and age are currently unavailable.
A column defined with NOT NULL cannot accept NULL through a normal INSERT operation.
Text values are normally written inside single quotation marks, while numeric values are generally written without quotes.
INSERT INTO Product (ProductID, ProductName, Price) VALUES (1, 'Laptop', 55000);
Here:
Date syntax can vary between database systems, so the format recommended by the particular DBMS should be followed.
A commonly used ISO-style date representation is:
INSERT INTO Student (StudentID, StudentName, AdmissionDate) VALUES (109, 'Pooja', '2026-07-21');
The database stores the supplied date according to the definition of the AdmissionDate column.
A column can have a predefined DEFAULT value. When that column is omitted from an INSERT statement, the database can use its default value.
CREATE TABLE Employee (
EmployeeID INT,
EmployeeName VARCHAR(100),
Country VARCHAR(50) DEFAULT 'India'
);
Now insert an employee without specifying Country:
INSERT INTO Employee (EmployeeID, EmployeeName) VALUES (1, 'Aman');
The Country column receives its defined default value.
Many database systems provide automatically generated key values. The exact syntax differs between DBMS products.
For example, in MySQL:
CREATE TABLE Student (
StudentID INT AUTO_INCREMENT PRIMARY KEY,
StudentName VARCHAR(100)
);
A record can then be inserted without supplying StudentID:
INSERT INTO Student
(StudentName)
VALUES
('Rahul');
The database generates the identifier automatically.
Other systems may use features such as IDENTITY or sequences instead, so auto-generated key behavior should always be checked for the specific DBMS being used.
INSERT INTO is not limited to manually supplied VALUES. Data can also be copied from one table to another using INSERT INTO SELECT.
INSERT INTO TargetTable (Column1, Column2) SELECT Column1, Column2 FROM SourceTable;
INSERT INTO StudentBackup (StudentID, StudentName) SELECT StudentID, StudentName FROM Student;
The selected records from Student are inserted into StudentBackup.
A WHERE condition can be used with the SELECT part to copy only the required records.
INSERT INTO TopStudents (StudentID, StudentName) SELECT StudentID, StudentName FROM Student WHERE Marks > 80;
Only students satisfying the condition are copied.
This technique is useful for data migration, reporting tables, archiving, and transferring selected records between tables.
Database constraints help prevent invalid data from entering a table. An INSERT statement must satisfy the applicable constraints.
CREATE TABLE Student (
StudentID INT PRIMARY KEY,
StudentName VARCHAR(100)
);
Two rows cannot normally have the same primary key value.
INSERT INTO Student VALUES (1, 'Rahul'); INSERT INTO Student VALUES (1, 'Amit');
The second INSERT violates the primary key rule because the value 1 already exists.
CREATE TABLE Employee (
EmployeeID INT,
EmployeeName VARCHAR(100) NOT NULL
);
EmployeeName must receive a value when a row is inserted unless the database definition provides another valid mechanism.
CREATE TABLE Users (
UserID INT,
Email VARCHAR(100) UNIQUE
);
A duplicate value may be rejected when it violates the UNIQUE constraint, subject to the DBMS's NULL and constraint behavior.
CREATE TABLE Student (
StudentID INT,
Age INT CHECK (Age >= 18)
);
An INSERT containing an age that violates the CHECK condition can be rejected by the database.
An INSERT statement can fail for many reasons. Some of the most common problems are:
INSERT INTO Student
(StudentID, StudentName)
VALUES
('ABC', 123);
If StudentID is numeric and StudentName is character data, the values do not match the intended column types and the DBMS may reject the statement or perform conversion according to its rules.
A foreign key ensures that a value in one table refers to an appropriate record in another table.
For example, suppose Employee contains DepartmentID as a foreign key referencing Department.
INSERT INTO Employee (EmployeeID, EmployeeName, DepartmentID) VALUES (101, 'Amit', 50);
If DepartmentID 50 does not exist in the referenced Department table, the INSERT may fail because the foreign key relationship would be violated.
When a student completes admission registration, the application can store the student's details in the database.
INSERT INTO Student (StudentID, StudentName, Course) VALUES (501, 'Rahul Sharma', 'B.Tech');
The new student is now available for later operations such as attendance, examination, fee management, and reporting.
When a customer places an order, the application may create a new record in an Orders table.
INSERT INTO Orders (OrderID, CustomerName, Amount) VALUES (9001, 'Vikas', 2500);
The stored order can then be processed by other parts of the application.
A hospital registration system can insert a new patient record when the patient is registered.
INSERT INTO Patient (PatientID, PatientName, Disease) VALUES (3001, 'Anjali', 'Fever');
The record can subsequently be associated with appointments, prescriptions, billing, and other hospital operations.
INSERT and UPDATE are both DML statements, but they perform different operations.
| Feature | INSERT | UPDATE |
|---|---|---|
| Purpose | Adds new rows | Changes existing rows |
| Creates a new record | Yes | No |
| Can use WHERE | Not with VALUES syntax | Yes |
| Typical use | Store new information | Modify existing information |
| Feature | INSERT ... VALUES | INSERT ... SELECT |
|---|---|---|
| Source of data | Explicit values | Result of a SELECT query |
| Useful for | New records | Copying or transferring records |
| Can use WHERE | No | Yes, through SELECT |
| Example | VALUES (...) | SELECT ... FROM ... |
When INSERT statements are generated from application input, user-provided values should not be directly concatenated into SQL strings.
Applications should use parameterized queries or prepared statements. This helps separate SQL instructions from user-supplied data and reduces the risk of SQL injection.
This is especially important in registration forms, login systems, online shopping applications, and other systems that accept data from users.
If an existing record needs to be changed, use UPDATE rather than INSERT.
The following example demonstrates a basic workflow: creating a table, inserting multiple records, and checking the result.
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 CREATE TABLE statement creates the structure, INSERT INTO adds the records, and SELECT verifies the stored data.
INSERT INTO is a SQL statement used to add new records to an existing table.
INSERT is generally classified as a Data Manipulation Language (DML) statement.
Yes. Many SQL systems support inserting multiple rows using one INSERT statement.
Not always. However, explicitly specifying column names is recommended for clarity and maintainability.
Yes, provided the target column permits NULL.
The database normally rejects the INSERT because a primary key must remain unique.
It inserts the result of a SELECT query into another table.
The normal INSERT ... VALUES syntax does not use WHERE. A WHERE condition can be used with INSERT ... SELECT.
INSERT adds new rows, whereas UPDATE modifies values in existing rows.
A DEFAULT provides a predefined value when a column is not supplied during insertion.
Yes, when the database is configured to generate its value automatically.
Common causes include invalid data types, duplicate keys, missing required values, foreign key violations, and other constraint violations.
SELECT * FROM Student;
They help safely handle application input and reduce the risk of SQL injection.
INSERT should be used when a new record needs to be created. UPDATE should be used when an existing record needs to be modified.
The INSERT INTO statement is one of the fundamental SQL commands for storing new information in a database. It can be used to add a single row, insert multiple rows, populate selected columns, work with NULL and DEFAULT values, and transfer records between tables through INSERT INTO SELECT.
A good understanding of INSERT INTO also requires knowledge of primary keys, foreign keys, NOT NULL, UNIQUE, CHECK constraints, data types, and safe application practices. Once these concepts are clear, inserting reliable data into relational databases becomes much easier.